Class GraphqlServerUtils

java.lang.Object
com.graphql_java_generator.server.util.GraphqlServerUtils

@Component public class GraphqlServerUtils extends Object
A class that contains utility method for the server mode
Author:
etienne-sf
  • Field Details

  • Constructor Details

    • GraphqlServerUtils

      public GraphqlServerUtils()
  • Method Details

    • classNameExtractor

      public String classNameExtractor(Class<?> cls)
      Implementation of a ClassNameTypeResolver to manage the possible prefix and suffix on the generated POJOs.
      Parameters:
      cls - The class which name must be retrieved
      Returns:
      The GraphQL type name that matches this class
    • getArgument

      public Object getArgument(Object jsonParsedValue, String graphQLTypeName, String javaTypeForIDType, Class<?> clazz)
      This method returns a GraphQL argument into the relevant Java object, within a data fetcher, from what has been parsed by the graphql-java engine from the incoming JSON request
      Type Parameters:
      T - The class expected to be returned
      Parameters:
      jsonParsedValue - The value, read from the JSON in the GraphQL request. Only the part of the JSON map, related to the expected class is sent. It can be:
      • A Map. This map will be transformed into an input object, as defined in the GraphQL schema, from the Map that has been read from the JSON object sent to the server.
      • A List. In this case, returns a list of instances of the given clazz type.
      • Otherwise, the value is a scalar. At this stage, Custom Scalars have already been transformed into the relevant Java Type. So it must be a standard scalar. It is then mapped to the asked java type
      graphQLTypeName - The name of the GraphQL type, as defined in the GraphQL schema. This can be guessed from the given class for input types and objects, but not for scalars. So it must be provided.
      javaTypeForIDType - Value of the plugin parameter of the same name. This is necessary to properly manage fields of the ID GraphQL type, which must be transformed to this java type. This is useful only when mapping into input types.
      clazz - The class of the expected type. A new instance of this type will be returned, with its fields having been set by this method from the value in the map
      Returns:
      An instance of the expected class. If the map is null, null is returned. Of the map is empty, anew instance is returned, with all its fields are left empty
    • enumValueToString

      public Object enumValueToString(Object enumValue)
      Returns the given enumValue transformed a String, based on the String representation for this enumValues.
      This method is based on the graphQlValue() generated for each enum POJO.
      Parameters:
      enumValue - May be null, a value of an enum POJO generated from the GraphQL schema, a list of values of any depth (for instance [[[Episode]]] would be a list of Episode enums of depth 3). The item of the list may be either enums (generated by the plugin) or Optional<? extends Enum> (where the content of the Optional is an enum generated by the plugin)
      Returns:
      The same kind of list, but all enum values are replaced by the relevant String representation, based on the GraphQL schema.
    • mapArgumentToRelevantPojoOrScalar

      public Object mapArgumentToRelevantPojoOrScalar(Object o, Class<?> clazz, int listDepth, String entityName, String argumentName)
      Maps an object received from spring-graphql to an ObjectNode (or a list of ObjectNode of any depth). This is used to manage the JSON custom scalar: it's mapped to the java type ObjectNode. But spring-graphql can't map an incoming parameter to ObjectNode, as it has no default constructor. One has to use an ObjectMapper and read the incoming object to do so: this is the aim of this method.
      This method expects that the provided data is correct. For instance, if the expected return type is in GraphQL [Type1], then:
      • o is an array.
      • Each item of the array is a Map that maps to the GraphQL Type1.
      • The custom scalar fields, if any, have been properly resolved by spring-graphql before, and the relevant value is available in the received Object
      Note: this method manages GraphQL field arguments. So the given clazz parameter is expected to a list, an enum, a scalar or an input type. It may not be an interface, an output type or a union.
      Parameters:
      o - The object (or subobject, if we've already recursed into this method) that should be mapped into the given GraphQL type, with the correct listDepth
      clazz - The target class for the o object. If it maps to a GraphQL type defined in the GraphQL schema, this class should be one generated by the plugin so that the GraphQLInputType annotation is properly set.
      listDepth - The depth of the list for the given o object: 0 if this field is not a list, and 2, for instance, if the field's type is "[[Int]]"
      entityName - The name of the entity which field is initially expected (may be different from the clazz in case of nested types). It is used only to display a more explicit error message.
      fieldName - The name of the field is initially expected. It is one of the entityName fields. It is used only to display a more explicit error message.
      Returns: