Class GraphQLExpectationGenerator

java.lang.Object
org.mockserver.graphql.GraphQLExpectationGenerator

public class GraphQLExpectationGenerator extends Object
Generates mock Expectations from a GraphQL schema document (SDL text or an introspection JSON result). This is the GraphQL analogue of the OpenAPI and WSDL importers: it turns a schema into ready-to-serve mock expectations without any hand-authored response JSON.

For each root operation type the schema defines (Query, Mutation, Subscription), one expectation is generated that:

  • matches any incoming GraphQL operation of that kind (POST to the configured GraphQL path), using SelectionSetMatchType.AST_SUBSET matching with an empty expected field set so every operation of the right kind matches;
  • carries the supplied schema on its GraphQLBody and has no response body, so GraphQLResponseSynthesizer synthesizes a schema-valid {"data": {...}} response from the actual query at request time.

The schema is validated up front: an unparseable schema produces an IllegalArgumentException so the import endpoint can report a 400, mirroring the OpenAPI and WSDL importers.

  • Constructor Details

    • GraphQLExpectationGenerator

      public GraphQLExpectationGenerator()
  • Method Details

    • generate

      public List<Expectation> generate(String schemaDefinition)
      Generate expectations from a GraphQL schema, matching GraphQL requests on the default /graphql path.
      Parameters:
      schemaDefinition - SDL text or an introspection JSON result
      Returns:
      one expectation per root operation type defined by the schema
      Throws:
      IllegalArgumentException - if the schema is null, blank, or cannot be parsed
    • generate

      public List<Expectation> generate(String schemaDefinition, String path)
      Generate expectations from a GraphQL schema, matching GraphQL requests on the supplied path.
      Parameters:
      schemaDefinition - SDL text or an introspection JSON result
      path - the request path to match (defaults to /graphql when blank)
      Returns:
      one expectation per root operation type defined by the schema
      Throws:
      IllegalArgumentException - if the schema is null, blank, or cannot be parsed