linkedin

Anypoint Datagraph REST GraphQL: REST to GraphQL with MuleSoft

Anypoint Datagraph REST GraphQL Overview

anypoint-datagraph-rest-to-graphql

In this blog, I have tried to showcase a step-by-step demo on how to create a Datagraph API using a REST API with the Anypoint Datagraph in the MuleSoft Anypoint Platform. Specifically, the focus is on Anypoint Datagraph REST GraphQL integration, highlighting how REST converges with Anypoint Datagraph to enable GraphQL functionalities.

anypoint-datagraph-rest-to-graphql

Before diving in, read our previous blog to learn what Datagraph is and how Anypoint Datagraph REST GraphQL can be used effectively. Additionally, explore more on blending Anypoint and GraphQL through Datagraph REST.

Now, let’s start by configuring Anypoint Datagraph REST and GraphQL integration.

Prerequisites & Setup

  1. Use the Datagraph Demo Github Link and use the table_schema.md file to create the tables in your mysql database.
  2. Create the corresponding RAML specification in an Anypoint Platform account. You could use a new trial account to focus on enhancing Datagraph with REST and GraphQL.
  3. Implement each API in Anypoint Studio and get the details from the underlying database. Please refer to the jar file provided in the above Github link.
  4. Manage the published API in API Manager with basic-endpoint as the managing type. Use the auto-discovery-id in your implementation and deploy the implementation to Cloudhub. This step is crucial for Anypoint Datagraph setup with REST and GraphQL, emphasizing integration with Datagraph’s capabilities.
  5. For this blog you can take any one schema and implement it, say Customers API. I’ve included three additional APIs in the screenshots below, which I’ll use in the next blog to demonstrate collaboration, linking, and merging.
  6. After completing these steps, you should see the APIs reflected as shown in the screenshots below.
Design center
Exchange
Runtime Manager
API Manager

Adding APIs to the Unified Schema

Add the Customers API to the DataGraph Unified Schema, thereby illustrating how REST APIs merge seamlessly with Anypoint Datagraph’s GraphQL.

  • Navigate to DataGraph
Start building
  • Click Add API
  • Then, select the Customers API from the list of APIs in Exchange and click on Next: Configure URL.
Add API
  • After confirming, select the version of the API you want to add to DataGraph and click on Confirm selection.
Customers API
  • The URL for the selected API instance will be pre-populated for you. However, if you don’t have an API instance in API Manager, you’ll need to select the Add a new URL option and provide the implementation URL by clicking on the Edit URL button.
Customers API

Authentication and Schema Preview

  • Click on Next: Provide authentication for effective Anypoint Datagraph integration with REST and GraphQL.
  • Since the APIs managed in API Manager don’t have authentication configured, therefore select No Auth from the dropdown and click Next: Preview Schema.
Authentication
  • At this stage, click on Preview Schema.
Schema created
  • Then, observe the Customer type and its method names and click on Next: Edit Schema.
anypoint-datagraph-rest-to-graphql
Sandbox
  • Click on the Customer type under level 1 types.
  • Ensure that the Customer type’s desired type is set to visible.
anypoint-datagraph-rest-to-graphql

Enable Collaboration

  • In order to allow other types in the local API schema or unified schema to link and merge with this type, you must enable collaboration to create a robust unified schema. Accordingly, click on Enable Collaboration.
anypoint-datagraph-rest-to-graphql
  • When prompted, define the default query method by selecting customerByID, and then click Next.
anypoint-datagraph-rest-to-graphql
  • Afterwards, select the primary key as ID (string!) and click Confirm.
anypoint-datagraph-rest-to-graphql
  • Once confirmed, you’ll see the collaboration permissions configured for the Customer type.
anypoint-datagraph-rest-to-graphql
  • Now, click Next: Add to unified schema to proceed. At this point, a message appears—simply click Proceed to unified schema. As a result, this enhances Anypoint connectivity between Datagraph REST and GraphQL.
anypoint-datagraph-rest-to-graphql
  • As mentioned earlier, it may take some time for the new data types to reflect in the Unified Schema.
anypoint-datagraph-rest-to-graphql

Testing the REST to GraphQL API Using Anypoint Datagraph

  • Once the schema updates, the Run a Query button becomes enabled. Then, click on Run a Query to leverage Anypoint Datagraph’s REST and GraphQL API features.
anypoint-datagraph-rest-to-graphql
  • Next, create a client application by selecting Create a new application and use it immediately, and then click Next.
anypoint-datagraph-rest-to-graphql
  • After providing an application name, click Next.
anypoint-datagraph-rest-to-graphql
  • At this stage, you’ll see a query builder window where you can write GraphQL queries to fetch customer information.
anypoint-datagraph-rest-to-graphql

Sample Query and Output

anypoint-datagraph-rest-to-graphql
  • Query
        {
          customers
          {
            firstName
            lastName
            phone
            email
            address
          }
          customersByID(ID: "C1"){
            firstName
            lastName
            phone
            email
            address
          }
        }

Finally, you have successfully created a Datagraph REST to GraphQL API from the Customers REST API. In the next blog, we’ll show how to create a link between two types using a primary key, further demonstrating how Datagraph integrates REST and GraphQL capabilities.

Â