Class DocumentParser

java.lang.Object
com.graphql_java_generator.plugin.DocumentParser
All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean
Direct Known Subclasses:
GenerateCodeDocumentParser, GenerateGraphQLSchemaDocumentParser

public abstract class DocumentParser extends Object implements org.springframework.beans.factory.InitializingBean
This class generates the Java classes, from the documents. These documents are read from the graphql-spring-boot-starter code, in injected here thanks to spring's magic.
There is no validity check: we trust the information in the Document, as it is read by the GraphQL Parser.
The graphQL-java library maps both FieldDefinition and InputValueDefinition in very similar structures, which are actually trees. These structures are too hard too read in a Velocity template, and we need to parse down to a properly structures way for that.
Author:
etienne-sf
  • Field Details

    • DEFAULT_QUERY_NAME

      protected final String DEFAULT_QUERY_NAME
      See Also:
    • DEFAULT_MUTATION_NAME

      protected final String DEFAULT_MUTATION_NAME
      See Also:
    • DEFAULT_SUBSCRIPTION_NAME

      protected final String DEFAULT_SUBSCRIPTION_NAME
      See Also:
    • configuration

      @Autowired protected CommonConfiguration configuration
      This instance is responsible for providing all the configuration parameters from the project configuration (Maven, Gradle...)
    • addRelayConnections

      @Autowired protected AddRelayConnections addRelayConnections
      A utility that adds Relay Connection capabilities to the read schema. It is called if the CommonConfiguration.isAddRelayConnections() is true
    • graphqlUtils

      @Autowired protected GraphqlUtils graphqlUtils
      Various utilities, grouped in a dedicated class
    • schemaStringProvider

      @Autowired protected ResourceSchemaStringProvider schemaStringProvider
      This Spring Bean is responsible for finding and loading all the GraphQL schema files, based on the project configuration.
    • directives

      protected List<Directive> directives
      List of all the directives that have been read in the GraphQL schema
    • schemaDirectives

      protected List<AppliedDirective> schemaDirectives
      List of the directives that have declared for the schema in the GraphQL schema
    • queryTypeName

      protected String queryTypeName
      The name of the type that implements the query operation. The default name is "Query". It is overridden in the readSchemaDefinition() method, if defined in the provided GraphQL schema.
    • queryType

      protected ObjectType queryType
      The Query root operation for this Document
    • mutationTypeName

      protected String mutationTypeName
      The name of the type that implements the mutation operation. The default name is "Mutation". It is overridden in the readSchemaDefinition() method, if defined in the provided GraphQL schema.
    • mutationType

      protected ObjectType mutationType
      The Mutation root operation for this Document, if defined (that is: if this schema implements one or more mutations)
    • subscriptionTypeName

      protected String subscriptionTypeName
      The name of the type that implements the subscription operation. The default name is "Subscription". It is overridden in the readSchemaDefinition() method, if defined in the provided GraphQL schema.
    • subscriptionType

      protected ObjectType subscriptionType
      The Subscription root operation for this Document, if defined (that is: if this schema implements one or more subscriptions)
    • interfaceTypes

      protected List<InterfaceType> interfaceTypes
      All the InterfaceTypeDefinition which have been read during the reading of the documents
    • unionTypes

      protected List<UnionType> unionTypes
      All the UnionTypeDefinition which have been read during the reading of the documents
    • enumTypes

      protected List<EnumType> enumTypes
      All the ObjectType which have been read during the reading of the documents
    • scalarTypes

      protected List<ScalarType> scalarTypes
      maps for all scalers, when they are mandatory. The key is the type name. The value is the class to use in the java code
    • customScalars

      protected List<CustomScalarType> customScalars
      All the CustomScalarType which have been read during the reading of the documents
    • types

      protected Map<String,Type> types
      All the Types that have been parsed, added by the default scalars. So it contains the query, the mutation (if defined), the subscription (if defined), the types, the input types, all the scalars (including the default ones), the interfaces, the unions and the enums.
      The key is the type's name. The value is the Type.
  • Constructor Details

    • DocumentParser

      public DocumentParser()
  • Method Details

    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • initScalarTypes

      protected void initScalarTypes(Class<?> IDclass)
      This method initializes the scalarTypes list. This list depends on the use case
      Parameters:
      IDclass -
    • parseGraphQLSchemas

      public int parseGraphQLSchemas() throws IOException
      The main method of the class: it graphqlUtils.executes the generation of the GraphQL schemas, as defined in the project configuration
      Returns:
      Throws:
      IOException - When an error occurs, during the parsing of the GraphQL schemas
    • fillTypesMap

      public void fillTypesMap()
      Fill the types map, from all the types (object, interface, enum, scalars) that are valid for this schema. This allow to get the properties from their type, as only their type's name is known when parsing the schema.
    • readDirectiveDefinition

      public Directive readDirectiveDefinition(graphql.language.DirectiveDefinition node)
      Reads a directive definition, and stores its informations into the DirectiveImpl for further processing
      Parameters:
      node -
      Returns:
    • getDirectiveDefinition

      public Directive getDirectiveDefinition(String name)
    • readObjectTypeDefinition

      public ObjectType readObjectTypeDefinition(graphql.language.ObjectTypeDefinition node)
      Read an object type from its GraphQL definition
      Parameters:
      node -
      Returns:
    • readEnumType

      public EnumType readEnumType(EnumType enumType, graphql.language.EnumTypeDefinition node)
      Reads an enum definition. It can be called from either an EnumTypeDefinition or an EnumTypeExtensionDefinition
      Parameters:
      enumType - The enum for which the definition is read.
      node -
      Returns:
    • getType

      public Type getType(String typeName)
      Returns the type for the given name
      Parameters:
      typeName -
      Returns:
      Throws:
      RuntimeException - if the type could not be found
    • getType

      public Type getType(String typeName, boolean throwExceptionIfNotFound)
      Returns the type for the given name
      Parameters:
      typeName -
      throwExceptionIfNotFound - If true, a RuntimeException is thrown when the type is not found. If false and the type is not found, null is returned.
      Returns:
      The found type, or null if the type is not found and throwExceptionIfNotFound is false
      Throws:
      RuntimeException - if throwExceptionIfNotFound is true and the type could not be found
    • getType

      public <T> T getType(String typeName, Class<T> classOfType, boolean throwExceptionIfNotFound)
      Returns the type for the given name, where the found type is of an expected kind.
      Parameters:
      typeName -
      classOfType - The expected class for this type (typically one of ScalarType, ObjectType...)
      throwExceptionIfNotFound - If true, a RuntimeException is thrown when the type is not found. If false and the type is not found, null is returned.
      Returns:
      The found type, or null if the type is not found and throwExceptionIfNotFound is false
      Throws:
      RuntimeException - if throwExceptionIfNotFound is true and the type could not be found
    • getUtilPackageName

      protected String getUtilPackageName()
      Returns the name of the package for utility classes.
      In this class, it always return the result of CommonConfiguration#getPackageName()
      Returns: