Class QueryGenerator


  • @ExperimentalApi
    public class QueryGenerator
    extends java.lang.Object
    Generates a GraphQL query string based on the provided operation field path, operation name, arguments, and type classifier.

    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.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String generateQuery​(java.lang.String operationFieldPath, @Nullable java.lang.String operationName, @Nullable java.lang.String arguments, @Nullable java.lang.String typeClassifier)
      Generates a GraphQL query string based on the provided operation field path, operation name, arguments, and type classifier.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • QueryGenerator

        public QueryGenerator​(GraphQLSchema schema,
                              QueryGeneratorOptions options)
        Constructor for QueryGenerator.
        Parameters:
        schema - the GraphQL schema
        options - the options for query generation
    • Method Detail

      • generateQuery

        public java.lang.String generateQuery​(java.lang.String operationFieldPath,
                                              @Nullable java.lang.String operationName,
                                              @Nullable java.lang.String arguments,
                                              @Nullable java.lang.String typeClassifier)
        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.

        typeClassifier 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)")
        typeClassifier - optional: the type classifier for union or interface types (e.g., "FirstPartyUser")
        Returns:
        the generated GraphQL query string