5

CAP: GraphQL as new protocol

 1 year ago
source link: https://blogs.sap.com/2023/03/18/cap-graphql-as-new-protocol/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
March 18, 2023 1 minute read

CAP: GraphQL as new protocol

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.

capgraphql.png

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
graphiql-testing-2.png
  • Testing via Postman client
postman-testing.png

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!~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK