Write GraphQL Queries in Anypoint DataGraph

This brief tutorial introduces key concepts for writing GraphQL queries in Anypoint DataGraph.

Learn how to use the Anypoint DataGraph query editor to:

  1. Discover and add a query method

  2. Add input parameters to a query

  3. Discover which fields are available to query

  4. Add nested types

  5. Add a query alias

Discover and Add a Query Method

In GraphQL, query methods serve as the entry points to retrieve the data you want. Start by using the schema explorer in the Anypoint DataGraph query editor to discover which query methods are available for an API schema.

For example, the Catalyst Order API schema, used in the DataGraph quick start guide, has two query methods associated with it: ordersCustomerByCustomerId and ordersByOrderId:

Two query methods listed in the schema explorer

Consider ordersByOrderId: This query method returns data about the object type OrderResponse and requires an argument for orderID in the form of a string:

ordersByOrderId (orderId:String!):OrderResponse

In GraphQL, you add the query method to start a query:

{
  ordersByOrderId(orderId:String!)
}

Add Input Parameters

Notice that the input parameter for the query method orderID is a non-nullable field, as indicated by the exclamation point (!). This field requires a string data type that is not a null value.

In this case, you need to add an order ID to the query:

{
  ordersByOrderId(orderId: "51c0ba3a-7e64-11e7-bb31-be2e44b06b3")
}

Add Fields

Fields represent a type’s dataset that is returned in a query response, and you can choose whatever fields you need when writing a query. For example, as previously mentioned, the ordersByOrderID query method returns the OrderResponse object type, which returns the following fields:

Available fields listed in the schema explorer

As you write a query, you can add or remove any of these fields. The following query a uses some of the available fields:

{
  ordersByOrderId(orderId: "51c0ba3a-7e64-11e7-bb31-be2e44b06b3") {
    customerId
    status
    subtotal
    taxPrice
    total
    trackingNumber
  }
}

OrderResponse is a Level-1 type, meaning you can access it directly with a query method, as in the previous example.

Add Fields for Nested Types

Many of the fields returned by the OrderResponse object type, such as billingAddress, are object types themselves that contain additional data that can make your query result more valuable. These additional types are known as nested types.

The billingAddress object type returns the PostalAddress object type, which itself returns several additional fields. To query billingAddress, you must specify which fields to return from PostalAddress. For example:

{
  ordersByOrderId(orderId: "51c0ba3a-7e64-11e7-bb31-be2e44b06b3") {
    billingAddress {
      state
      city
      postalCode
    }
  }
}

As a result, your final query looks like:

{
  ordersByOrderId(orderId: "51c0ba3a-7e64-11e7-bb31-be2e44b06b3") {
    customerId
    status
    subtotal
    taxPrice
    total
    trackingNumber
    billingAddress {
      state
      city
      postalCode
    }
  }
}

For this query, Anypoint DataGraph returns:

{
  "data": {
    "ordersByOrderId": {
      "customerId": "1964401a-a8b3-40c1-b86e-d8b9f75b5842",
      "status": "Draft",
      "subtotal": "100.21",
      "taxPrice": "15.22",
      "total": "125.43",
      "trackingNumber": "",
      "billingAddress": {
        "state": "CA",
        "city": "San Francisco",
        "postalCode": "94210"
      }
    }
  }
}

Add a Query Aliases

You can use aliases to rename individual keys in the query response.

For example, you can rename the total field to totalPrice and the status field to orderStatus in only your query response.

{
  ordersByOrderId(orderId: "51c0ba3a-7e64-11e7-bb31-be2e44b06b3") {
    billingAddress {
      state
      city
      postalCode
    }
    totalPrice : total
    orderStatus : status
  }
}

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub