As you learned in the previous section, the Query type defines the object you will be interacting with. A GraphQL query is used to read or fetch values. In the definitions you saw previously, Query is referencing patient information. When you execute a query, you are searching for data. In cases where you need to write data, you utilize a type called a mutation. Mutations follow the same SDL format as queries and other types.
So, you have learned that queries fetch data and mutations, as well as update data, but what you haven’t learned is how the data access is initiated. That is accomplished with a resolver.
A resolver is a function that’s responsible for populating the data for a single field in your schema. Refer to the following code:
const resolvers = {
Query: {
patient: () => patient,
patientById: (parent, args, context, info) => {
return patient.filter(item=> item.id === args.id)[0];
}
}
};
Each field on each type is supported by a resolver. The resolver traverses through the fields, returning all the objects until it completes all the resolvers. Once completed, your graph is fully populated with the data you requested from the backend.
Note – v10.0.1.5
Subscriptions are GraphQL read operations that can update their results whenever a particular server-side event occurs. Results are pushed from the GraphQL server to subscribing clients. In earlier versions of API Connect, GraphQL subscriptions were not supported but now, in v10.0.1.5, GraphQL subscriptions are supported in the payload and query requests. See what’s new in the latest release to learn more about GraphQL enhancements: https://www.ibm.com/docs/en/api-connect/10.0.x?topic=overview-whats-new-in-latest-release-version-10030#overview_whatsnew_revised__section_apiconsumers.
Now that you have developed an understanding of what is being defined and implemented on the GraphQL server, it’s time to build a GraphQL API in API Connect. But first, you will need to have a working GraphQL server.
Installing a GraphQL Express server
To try out the GraphQL API proxy in API Connect, you will need a running instance of a GraphQL server. One has been provided for in this book’s GitHub repository:
The tar file named chap09.tar contains an Express Node.js server that contains GraphQL. It can be deployed on Mac and Linux.
Perform the following steps:
- Untar chap09.tar to find a graphql-express-demo.zip file.
- Unzip graphql-express-demo.zip.
- In a Terminal/command-line window, change to the graphql-express-demo directory.
Install Node.js on your Linux or Windows environment:
$sudo install node.js
You can also visit the following website for installation instructions:
https://docs.npmjs.com/downloading-and-installing-node-js-and-npm.
4. Install Express on your Linux environment, as follows:
$sudo dnf install express
5. The installation will be looking for an environment variable set, which specifies which set of code to execute. In this example, please set an environment variable for NODE_ENV=production:
$sudo export NODE_ENV=production
You can also refer to the following website for more details on how to install Express.js:
https://expressjs.com/en/starter/installing.html.
6. Update your /etc/hosts file with your IP graphql.apicisoa.com.
7. If you haven’t allowed port 443 in your firewall, you should run the following firewall-cmd commands:
firewall-cmd –permanent –add-port=443/tcp –zone=external
firewall-cmd –reload
8. Start your Express server:
node index.js
Server ready at https://graphql.apicisoa.com:443/graphql
If everything works properly, you should be able to begin using the GraphQL server.
Next, you will walk through how to create a GraphQL API with API Connect. Along the way, you will be introduced to some features that allow you to run GraphQL APIs successfully while avoiding pitfalls.
In the next section, you will be implementing GraphQL in API Connect using an FHIR Patient resource example and learning about the GraphQL capabilities within API Connect.
Creating a GraphQL API
So far, you have created various types of API proxies using API Connect, so creating a new API should be easy. When you create a GraphQL API, there are a few different checkpoints that you will become familiar with. In this section, you will be working with a GraphQL server that is providing FHIR Patient resource data. A few of the things you will be learning about are as follows:
- How nested calls are triggered by a single API
- How throttling prevents usage spikes
- How to estimate the cost of running a complex query
- Where to establish rate limits since GraphQL APIs are not like traditional APIs
The following diagram shows how the client interacts with API Connect’s GraphQL implementation and with the GraphQL server:

Figure 9.2 – High-level GraphQL flow
The preceding diagram also shows you how a query is passed in and goes through a series of checks before API Connect forwards the query to the GraphQL server. In addition, you can see how the GraphQL user interface allows you to inspect how the data will be returned based on the schema provided from the GraphQL server.
With that in mind, it’s time to build your GraphQL API.