Compare commits
4 Commits
refactor/s
...
graphql
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9536a7f34c | ||
|
|
079620463f | ||
|
|
d53d4de105 | ||
|
|
b13ed5cc8d |
1
graphql/.env
Normal file
1
graphql/.env
Normal file
@@ -0,0 +1 @@
|
|||||||
|
PORT = 4007
|
||||||
4
graphql/db/data/sap.capire.graphql-Chapters.csv
Normal file
4
graphql/db/data/sap.capire.graphql-Chapters.csv
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
book_ID;number;title
|
||||||
|
201;1;Chapter 1
|
||||||
|
201;2;Chapter 2
|
||||||
|
201;3;Chapter 3
|
||||||
|
26
graphql/db/schema.cds
Normal file
26
graphql/db/schema.cds
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using {
|
||||||
|
cuid,
|
||||||
|
managed
|
||||||
|
} from '@sap/cds/common';
|
||||||
|
using {sap.capire.bookshop} from '@capire/bookshop';
|
||||||
|
|
||||||
|
namespace sap.capire.graphql;
|
||||||
|
|
||||||
|
extend bookshop.Books with {
|
||||||
|
chapters : Composition of many Chapters
|
||||||
|
on chapters.book = $self;
|
||||||
|
}
|
||||||
|
|
||||||
|
entity Chapters : managed {
|
||||||
|
key book : Association to bookshop.Books;
|
||||||
|
key number : Integer;
|
||||||
|
title : String;
|
||||||
|
}
|
||||||
|
|
||||||
|
entity Orders : cuid, managed {
|
||||||
|
@mandatory
|
||||||
|
book : Association to bookshop.Books;
|
||||||
|
@mandatory
|
||||||
|
@assert.range : [ 1, 5 ]
|
||||||
|
quantity : Integer;
|
||||||
|
}
|
||||||
7
graphql/examples.http
Normal file
7
graphql/examples.http
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# 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)
|
||||||
16
graphql/examples.md
Normal file
16
graphql/examples.md
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
1. open `http://localhost:4007/graphql`
|
||||||
|
2. paste into left field:
|
||||||
|
```graphql
|
||||||
|
{
|
||||||
|
BookshopService {
|
||||||
|
Books {
|
||||||
|
title
|
||||||
|
chapters {
|
||||||
|
number
|
||||||
|
title
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
3. press play button
|
||||||
21
graphql/package.json
Normal file
21
graphql/package.json
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"name": "@capire/graphql",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"dependencies": {
|
||||||
|
"@capire/bookshop": "*",
|
||||||
|
"@graphql-tools/schema": "^8.3.1",
|
||||||
|
"@sap/cds": "^5.6",
|
||||||
|
"express-graphql": "^0.12.0",
|
||||||
|
"graphql": "^16.0.1"
|
||||||
|
},
|
||||||
|
"cds": {
|
||||||
|
"features": {
|
||||||
|
"graphql": true
|
||||||
|
},
|
||||||
|
"requires": {
|
||||||
|
"auth": {
|
||||||
|
"kind": "dummy-auth"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
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`)
|
||||||
|
})
|
||||||
|
}
|
||||||
2642
package-lock.json
generated
2642
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -9,11 +9,12 @@
|
|||||||
"@capire/bookshop": "./bookshop",
|
"@capire/bookshop": "./bookshop",
|
||||||
"@capire/common": "./common",
|
"@capire/common": "./common",
|
||||||
"@capire/fiori": "./fiori",
|
"@capire/fiori": "./fiori",
|
||||||
|
"@capire/graphql": "./graphql",
|
||||||
"@capire/hello": "./hello",
|
"@capire/hello": "./hello",
|
||||||
"@capire/media": "./media",
|
"@capire/media": "./media",
|
||||||
"@capire/orders": "./orders",
|
"@capire/orders": "./orders",
|
||||||
"@capire/reviews": "./reviews",
|
"@capire/reviews": "./reviews",
|
||||||
"@sap/cds": "^5.5.3"
|
"@sap/cds": "^5.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"chai": "^4.3.4",
|
"chai": "^4.3.4",
|
||||||
|
|||||||
Reference in New Issue
Block a user