Mock resolvers

For testing scenarios, you can configure a mock resolver in a GraphQLResolverMap Gloo custom resource. Mock resolvers do not require resolver servers to exist, and instead can immediately return a mocked response. Then, map fields from your schema definition to resolvers in a GraphQLSchema CR.

Step 1: Mock resolvers

Define your mock resolvers in a GraphQLResolverMap CR. Note that you can specify only one of asyncResponse, errorResponse, or syncResponse.

apiVersion: apimanagement.gloo.solo.io/v2
kind: GraphQLResolverMap
metadata:
  name: 
  namespace: 
spec:
  # Resolver map
  types:
    Query:
      fields:
        # Replace 'nameOfField' with the name of the field that you 
        # previously defined for this query
        <nameOfField>:
          # Configuration for generating outgoing requests to a gRPC API
          resolvers:
            # You can specify only one type of mock resolver: asyncResponse, errorResponse, or syncResponse
            - mockResolver:
                # The JSON object response to immediately return to the GraphQL
                # execution engine. Can be templated using variables from the # variables field
                asyncResponse:
                # An error response to return for this field
                errorResponse:
                # The JSON object response to immediately return to the GraphQL
                # execution engine. Can be templated using variables from the # variables field
                syncResponse:

In this example resolver map, a mock resolver is defined for the productsForHome field of the top-level query type. Two JSON objects in the syncResponse are defined in order to send example responses to test queries.

apiVersion: apimanagement.gloo.solo.io/v2
kind: GraphQLResolverMap
metadata:
  name: bookinfo-mock-resolvermap
  namespace: bookinfo
spec:
  types:
    Query:
      fields:
        productsForHome:
          resolvers:
            - mockResolver:
                syncResponse:
                  json:
                    # To be properly converted from YAML to JSON, the entry content
                    # must be on a separate line after the hyphen (-).
                    -
                      id: 1
                      author: Bill
                      title: My Life
                      descriptionHtml: a book description
                      pages: 100
                      year: 1998
                      reviews:
                      -
                        reviewer: Sam
                        text: "The best!"
                    -
                      id: 2
                      author: George
                      title: Bill's Life
                      descriptionHtml: the real story of Bill's life
                      pages: 200
                      year: 1999
                      reviews:
                      -
                        reviewer: Bill
                        text: "Meh ... read my book instead"

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.

apiVersion: apimanagement.gloo.solo.io/v2
kind: GraphQLSchema
metadata:
  name: 
  namespace: 
spec:
  # Allow GraphQL servers that you define in GraphQLResolverMap CRs to resolve queries locally
  resolved:
    # Additional options on the schema
    options:
      # Enable introspection for the schema. Defaults to false.
      enableIntrospection:
      # Limit the maximum nesting on a query that runs against this schema.
      # Any GraphQL operation that queries past the max depth adds an error message to the response and returns as null.
      # If not configured, or the value is 0, the query depth is unbounded.
      maxDepth:
    # References to GraphQLResolverMap resources for resolvers.
    # Resolver maps that are higher in this list have a higher priority
    # over lower items when tie-breaking field resolver configurations exist.
    resolverMapRefs:
      - clusterName: 
        name: 
        namespace: 
  # Reference to ApiDoc resource for schema definitions
  schemaRef:
    clusterName: 
    name: 
    namespace: 

In this example GraphQLSchema resource, the resolvers from bookinfo-mock-resolvermap are mapped to the schema definition from bookinfo-rest-apidoc.

apiVersion: apimanagement.gloo.solo.io/v2
kind: GraphQLSchema
metadata:
  name: bookinfo-mock-graphqlschema
  namespace: bookinfo
spec:
  resolved:
    options: {}
    resolverMapRefs:
    - clusterName: $CLUSTER_NAME
      name: bookinfo-mock-resolvermap
      namespace: bookinfo
  schemaRef:
    clusterName: $CLUSTER_NAME
    name: bookinfo-rest-apidoc
    namespace: bookinfo

Reference

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

Next steps

Set up routing to resolvers and apply traffic policies.