SAP Community Call
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using {managed} from '@sap/cds/common';
|
||||
using {
|
||||
cuid,
|
||||
managed
|
||||
} from '@sap/cds/common';
|
||||
using {sap.capire.bookshop} from '@capire/bookshop';
|
||||
|
||||
namespace sap.capire.graphql;
|
||||
@@ -13,3 +16,11 @@ entity Chapters : managed {
|
||||
key number : Integer;
|
||||
title : String;
|
||||
}
|
||||
|
||||
entity Orders : cuid, managed {
|
||||
@mandatory
|
||||
book : Association to bookshop.Books;
|
||||
@mandatory
|
||||
@assert.range : [ 1, 5 ]
|
||||
quantity : Integer;
|
||||
}
|
||||
|
||||
@@ -1 +1,7 @@
|
||||
GET http://localhost:4007/graphql?query={AdminService{Books{title,chapters{number,title}}}}
|
||||
# GraphQL
|
||||
GET http://localhost:4007/graphql?query={BookshopService{Books{title,author{name},chapters{number,title}}}}
|
||||
|
||||
###
|
||||
|
||||
# OData
|
||||
GET http://localhost:4007/bookshop/Books?$select=title&$expand=author($select=name),chapters($select=number,title)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
2. paste into left field:
|
||||
```graphql
|
||||
{
|
||||
AdminService {
|
||||
BookshopService {
|
||||
Books {
|
||||
title
|
||||
chapters {
|
||||
|
||||
20
graphql/server.js
Normal file
20
graphql/server.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const cds = require('@sap/cds')
|
||||
|
||||
// redeploy the db incl. initial data for demo purposes
|
||||
cds.on('bootstrap', app => {
|
||||
app.post('/redeploy', async (req, res) => {
|
||||
await cds.deploy('*')
|
||||
res.status(204).end()
|
||||
})
|
||||
})
|
||||
|
||||
// fix cds.context not being set correctly in GraphQL adapter
|
||||
cds.on('serving', srv => {
|
||||
const { dispatch } = srv
|
||||
srv.dispatch = function(req) {
|
||||
if (!(cds.context instanceof cds.Request)) cds.context = this
|
||||
return dispatch.call(this, req)
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = cds.server
|
||||
@@ -1,5 +0,0 @@
|
||||
using {sap.capire.graphql} from '../db/schema';
|
||||
|
||||
extend service AdminService with {
|
||||
entity Chapters as projection on graphql.Chapters;
|
||||
}
|
||||
11
graphql/srv/bookshop-service.cds
Normal file
11
graphql/srv/bookshop-service.cds
Normal file
@@ -0,0 +1,11 @@
|
||||
using {
|
||||
sap.capire.bookshop,
|
||||
sap.capire.graphql
|
||||
} from '../db/schema';
|
||||
|
||||
service BookshopService {
|
||||
entity Books as projection on bookshop.Books;
|
||||
entity Authors as projection on bookshop.Authors;
|
||||
entity Chapters as projection on graphql.Chapters;
|
||||
entity Orders as projection on graphql.Orders;
|
||||
}
|
||||
11
graphql/srv/bookshop-service.js
Normal file
11
graphql/srv/bookshop-service.js
Normal file
@@ -0,0 +1,11 @@
|
||||
module.exports = function() {
|
||||
const { Orders, Books } = this.entities
|
||||
|
||||
this.before('CREATE', Orders, async function(req) {
|
||||
const { book_ID, quantity } = req.data
|
||||
|
||||
// reduce the stock, if enough are available, else reject the order
|
||||
const applied = await UPDATE(Books, book_ID).set({ stock: { '-=': quantity } }).where({ stock: { '>=': quantity }})
|
||||
if (!applied) req.reject(400, `Sorry, ${quantity} are not in stock`)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user