Package org.mockserver.graphql
Class GraphQLResponseSynthesizer
java.lang.Object
org.mockserver.graphql.GraphQLResponseSynthesizer
Synthesizes schema-valid GraphQL responses for a matched query, given a registered
GraphQL schema. This is the GraphQL analogue of the OpenAPI
ExampleBuilder: it
walks the requested selection set against the schema's type system and emits a JSON
{"data": {...}} envelope with deterministic, type-correct placeholder values —
no hand-authored response JSON required.
Supported schema input:
- SDL text, e.g.
"type Query { hello: String }" - An introspection JSON result — either the full
{"data": {"__schema": ...}}envelope or the bare{"__schema": ...}object
Synthesis rules mirror GraphQL execution semantics:
- only requested fields (the selection set) appear in the response
- scalars produce deterministic placeholders (String, Int, Float, Boolean, ID and common custom scalars)
- enums produce their first declared value
- list types produce a single-element array
- object types recurse into their sub-selection
- non-null wrappers are unwrapped; nullable fields with no sub-selection on an object
type yield
null(only scalars/enums/lists are auto-populated as leaves) - inline fragments and named fragment spreads are flattened into the selection
Instances are immutable and thread-safe once constructed.
-
Constructor Summary
ConstructorsConstructorDescriptionGraphQLResponseSynthesizer(String schemaDefinition) Build a synthesizer from a GraphQL schema definition. -
Method Summary
Modifier and TypeMethodDescriptionsynthesizeResponse(String requestBody) Synthesize a schema-valid response for the supplied GraphQL request body.
-
Constructor Details
-
GraphQLResponseSynthesizer
Build a synthesizer from a GraphQL schema definition.- Parameters:
schemaDefinition- SDL text or an introspection JSON result- Throws:
GraphQLSchemaException- if the schema cannot be parsed
-
-
Method Details
-
synthesizeResponse
Synthesize a schema-valid response for the supplied GraphQL request body.- Parameters:
requestBody- the raw request body — either a JSON-wrapped GraphQL-over-HTTP body ({"query": "..."}) or a raw GraphQL document- Returns:
- a JSON string of the form
{"data": {...}} - Throws:
GraphQLSchemaException- if the query cannot be parsed against the schema
-