Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
Showing results for 
Search instead for 
Did you mean: 
Active Contributor

In this blog I will try to compile the steps to be able to add the new SAP GraphQL feature in your CAPs.

But first, what is SAP GraphQL?

The official definition is as follows:
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.


So let's get on with it!

We will start with the basic example that we have in the Business Application Studio the Book Shop.

Create a new entity and dependencies

We will create a second entity to add the author to the book and thus also have the books of each author:

  • We will edit the file: "/db/data-model.cds"

entity Authors {
key ID: Integer;
name: String;

  • Now we need to create a dependencies between Books and Authors. In the same file, we will modify the entities with these changes:

entity Books {
key ID : Integer;
title : String;
stock : Integer;
author: Association to Authors;
entity Authors {
key ID: Integer;
name: String;
books: Composition of many Books
on = $self;

To gain access to the relation between books and authors we will add this entities in the service, so, we need to modify the file "/srv/cat-service.cds" with the new entity "Authors"
using my.bookshop as my from '../db/data-model';

service CatalogService {
@readonly entity Books as projection on my.Books;
@readonly entity Authors as projection on my.Authors;


Adding data to the model

With the basic example from Business Application Studio we will have the file to load some data to the entity Books.

First of all we need to modify this file with the new column "author" in the file "/db/data/my.bookshop-Books.csv":
1;Wuthering Heights;100;1
2;Jane Eyre;500;1
3;Hello world;500;2

Then we will create a new file for the Authors entity in the folder: "/db/data/" with the name "/db/data/my.bookshop-Authors.csv":

And this will be the result in our project:

At this moment we will not have GraphQL functionality ready but we can check the result with "cds watch" and adding in the URL "/catalog/Books(1)?$expand=author", this will be the result:
{"@odata.context":"$metadata#Books(author())/$entity","ID":1,"title":"Wuthering Heights","stock":100,"author_ID":1,"author":{"ID":1,"name":"Pepe"}}

Adding GraphQL feature

First of all, we need to install some library with this command:
npm i -g @sap/cds-dk

Then we add the project dependencies:
npm add graphql express-graphql @graphql-tools/schema

We have the project ready. Now, we will activate the feature adding this code in the package.json:
    "features": {
"graphql": true

This will be the result:

Checking the feature

It's time to check the results, first of all, when we ejecute the command "cds watch" we will see the folloging:

If we access the "/graphql" link, we will be able to interact with the graphql console:

  • First we will obtain the catalogue of books, as if it were an OData:

  • We will now request the author associated with the book:

  • As we can see, we now have the list of books and authors. But with little effort we can do the request putting the Author as a main entity:

  • We can even make a single request for both entities and their relationships to optimise the number of calls:

So far the example, as you can see thanks to GraphQL we can shape the data request according to our needs.

What do you think? Do you prefer to make calls with OData or with GraphQL's? In my case, I consider this new way of obtaining data with CAPm to be much better.
Labels in this area