SAP Community Call

This commit is contained in:
sjvans
2022-02-24 08:30:26 +01:00
parent b13ed5cc8d
commit d53d4de105
8 changed files with 62 additions and 2763 deletions

View File

@@ -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;
}

View File

@@ -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)

View File

@@ -2,7 +2,7 @@
2. paste into left field:
```graphql
{
AdminService {
BookshopService {
Books {
title
chapters {

20
graphql/server.js Normal file
View 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

View File

@@ -1,5 +0,0 @@
using {sap.capire.graphql} from '../db/schema';
extend service AdminService with {
entity Chapters as projection on graphql.Chapters;
}

View 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;
}

View 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`)
})
}

2755
package-lock.json generated

File diff suppressed because it is too large Load Diff