Define REST resolver servers for each query type in a GraphQLResolverMap Gloo custom resource. Then, map fields from your schema definition to resolvers in a GraphQLSchema CR.

Step 1: REST resolvers

Define your REST resolvers in a GraphQLResolverMap CR.

Optional: Define variables for use in resolvers

You can define variables that you want to use in the variables section of a resolver in the resolver map. For example, you might define variables for the following uses:

  • Schema arguments: Define variables to retrieve arguments defined in your schema.
  • Authorization: Define common authorization variables, such as requestHeader: "Authorization".
  • jq filters: Define variables in this section that you want to use in jq filters for the resolver request or result. For more information, see Applying jq filters.

Note that some variables have defined names and guidelines for values. For more information about each type of variable, see the API documentation for resolver variables.

For example, you might define the following variables for the Reviews service:

  ...
Product:
  fields:
    reviews:
      variables:
        parentVar:
          graphqlParent: {}
        authorizationVar:
          requestHeader: "Authorization"
  

In the same resolver map resource, you can then call these variables in the resolver request headers or body, such as the following headers. Note that you can also specify individual values that are not declared in variables.

  ...
resolvers:
- restResolver:
    destinations:
      ...
    request:
      headers:
        Authorization:
          variable: authorizationVar
        :path:
          variable: parentVar
          value: "[1,2,3]"
  

Optional: Apply jq filters

To transform the JSON content of a GraphQL resolver request or result, you can provide jq filters in your GraphQL resolver map.

You can define variables that you want to use in the jq transformation filter in the variables section of a resolver in the resolver map. For example, a variable named “userIdHeader” can be used in a jq filter as .userIdHeader. Then, you can apply jq filters to resolver requests (resolver.restResolver.request) or results (resolver.restResolver.resolverResultTransform) that call these variables.

For more information, see the API documentation for jq transformation. For information about jq syntax and how to construct your jq filter, see the jq development manual.

For example, you might define the following variables for the Reviews service:

  ...
Product:
  fields:
    reviews:
      variables:
        parentVar:
          graphqlParent: {}
        authorizationVar:
          requestHeader: "Authorization"
  

You can then call these variables in the resolver request headers or body:

  ...
resolvers:
- restResolver:
    destinations:
      ...
    request:
      headers:
        :path:
          jq: '"/reviews/" + .parentVar.id'
        customAuth:
          jq: '"Bearer " + .authorizationVar'
  

You can also use variables in jq filters for resolver results. For example, if the data that the resolver fetched from your upstream API is not formatted in a way that the client can understand according to your schemas, you can apply a jq filter to the result data.

The fetched data from your API might be formatted like the following:

  {  "userIdHeader": "john_doe123",  "resolverResultVar": { "data": {"name": "John Doe"} } }
  

First, you define the following variables.

  ...
variables:
  userIdHeader:
    requestHeader: x-user-id
  resolverResultVar:
    resolverResult: {}
  

You then call those variables in a resolver.resolverResultTransform jq filter. Note that the resolverResultVar variable can only be used in the resolverResultTransform field, and not in a request field.

  ...
resolvers:
- restResolver:
    destinations:
      ...
    request:
      ...
  resolverResultTransform:
    jq: '"User: " + .userIdHeader + ", Name: " + .resolverResultVar.data.name'
  

The result of the jq transformation is as follows:

  "User: john_doe123, Name: John Doe"
  

Step 2: Map schema to resolvers

Map the types and fields from your schema definition (ApiDoc) to the resolver servers (GraphQLResolverMap) in a GraphQLSchema Gloo CR. The GraphQLSchema CR ensures that the GraphQL resolver services can access the field information for each type.

Reference

For more information, see the Gloo Mesh Gateway API reference for the GraphQLResolverMap CR and GraphQLSchema CR.

Next steps

Set up routing to resolvers and apply traffic policies.