Class QueryGenerator
While this class is useful for testing purposes, such as ensuring that all fields from a certain type are being fetched correctly, it is important to note that generating GraphQL queries with all possible fields defeats one of the main purposes of a GraphQL API: allowing clients to be selective about the fields they want to fetch.
Callers can pass options to customize the query generation process, such as filtering fields or limiting the maximum number of fields.
-
Constructor Summary
ConstructorsConstructorDescriptionQueryGenerator
(GraphQLSchema schema) Constructor for QueryGenerator using default options.QueryGenerator
(GraphQLSchema schema, QueryGeneratorOptions options) Constructor for QueryGenerator. -
Method Summary
Modifier and TypeMethodDescriptiongenerateQuery
(String operationFieldPath, @Nullable String operationName, @Nullable String arguments, @Nullable String typeName) Generates a GraphQL query string based on the provided operation field path, operation name, arguments, and type classifier.
-
Constructor Details
-
QueryGenerator
Constructor for QueryGenerator using default options.- Parameters:
schema
- the GraphQL schema
-
QueryGenerator
Constructor for QueryGenerator.- Parameters:
schema
- the GraphQL schemaoptions
- the options for query generation
-
-
Method Details
-
generateQuery
public QueryGeneratorResult generateQuery(String operationFieldPath, @Nullable String operationName, @Nullable String arguments, @Nullable String typeName) Generates a GraphQL query string based on the provided operation field path, operation name, arguments, and type classifier.operationFieldPath is a string that represents the path to the field in the GraphQL schema. This method will generate a query that includes all fields from the specified type, including nested fields.
operationName is optional. When passed, the generated query will contain that value in the operation name.
arguments are optional. When passed, the generated query will contain that value in the arguments.
typeName is optional. It should not be passed in when the field in the path is an object type, and it **should** be passed when the field in the path is an interface or union type. In the latter case, its value should be an object type that is part of the union or implements the interface.
- Parameters:
operationFieldPath
- the operation field path (e.g., "Query.user", "Mutation.createUser", "Subscription.userCreated")operationName
- optional: the operation name (e.g., "getUser")arguments
- optional: the arguments for the operation in a plain text form (e.g., "(id: 1)")typeName
- optional: the type name for when operationFieldPath points to a field of union or interface types (e.g., "FirstPartyUser")- Returns:
- a QueryGeneratorResult containing the generated query string and additional information
-