I’ve been looking into SAP Graph and GraphQL for quite some time now, and out of curiosity, I tried to look into CAP if it already supports GraphQL, and lucky enough CAP just recently started supporting GraphQL by releasing @cap-js/graphql which acts as a GraphQL adapter.
In this blog post, I will be giving a quick introduction to GraphQL in CAP.
Set up GraphQL adapter
Technically the adapter is very easy to set up. It is very straightforward to follow the instructions mentioned in the @cap-js/graphql node module, but for the sake of completeness, I will mention it here as well.
- Add the GraphQL adapter to your CAP project:
> npm install @cap-js/graphql
NOTE: The adapter is relatively new (3 months old at the time this blog post is written) so make sure your cap node modules are up to date – @sap/cds and @sap/cds-dk. |
- Register GraphQL adapter in your projects
package.json
:
{
"cds": {
"protocols": {
"graphql": { "path": "/graphql", "impl": "@cap-js/graphql" }
}
}
}
Testing the GraphQL endpoint
Run the CAP application as usual using cds watch
terminal command and the GraphQL endpoint will be available at:
http://localhost:4004/graphql
- Testing via the built-in GraphiQL client
- Testing via Postman client
- To learn more about the query features of GraphQL, head to https://graphql.org.
I’m sharing here my CAP project if you want to follow exactly the testing samples I provided above:
Closing
GraphQL protocol is very much a welcome addition to the already formidable features offered by the CAP framework. It acts as a complementary to OData V4 as another option as a query language. And although it still missing some capabilities (as mentioned in the limitations section of the GraphQL adapter), it already looks promising to use productively especially if you prefer using GraphQL protocol.
~~~~~~~~~~~~~~~~
Appreciate it if you have any comments, suggestions, or questions. Cheers!~