Class GraphqlUtils

java.lang.Object
com.graphql_java_generator.util.GraphqlUtils

@Component public class GraphqlUtils extends Object
Author:
etienne-sf
  • Field Details

    • graphqlUtils

      public static GraphqlUtils graphqlUtils
      This singleton is usable in default method, within interfaces
    • JAVA_KEYWORD_PREFIX

      public static Character JAVA_KEYWORD_PREFIX
  • Constructor Details

    • GraphqlUtils

      public GraphqlUtils()
  • Method Details

    • getRuntimeVersion

      public String getRuntimeVersion()
      Returns the version of the runtime, that is used to check that the runtime's version is the same as the Maven or Gradle plugin's version.
    • getCamelCase

      public String getCamelCase(String name)
      Convert the given name, to a camel case name. Currently very simple : it puts the first character in lower case.
      Returns:
    • getPascalCase

      public String getPascalCase(String name)
      Convert the given name, which is supposed to be in camel case (for instance: thisIsCamelCase) to a pascal case string (for instance: ThisIsCamelCase).
      Returns:
    • iterableToList

      public <I> List<I> iterableToList(Iterable<I> iterable)
      Transform an Iterable (which can be a List), into a List of items of the same type. It's usefull to transform the native type from Spring Data repositories (which needs concrete class to map into) into the list of relevant GraphQL interface
      Type Parameters:
      I -
      Parameters:
      iterable -
      Returns:
    • iterableConcreteClassToListInterface

      public <I> List<I> iterableConcreteClassToListInterface(Iterable<? extends I> iterable)
      Transform an Iterable (which can be a List) of a concrete class, into a List of the I interface or superclass. It's usefull to transform the native type from Spring Data repositories (which needs concrete class to map into) into the list of relevant GraphQL interface
      Type Parameters:
      I -
      Parameters:
      iterable -
      Returns:
    • optionalToObject

      public <T> T optionalToObject(Optional<T> optional)
      Transform an Optional, as returned by Spring Data repositories, into a standard Java, which is null if there is no value.
      Parameters:
      optional -
      Returns:
    • orderList

      public <T> List<T> orderList(List<?> keys, List<T> unorderedList, String keyFieldName)
      Reads a non ordered list, and return the same content sorted according the keys list. This method is used for batch loader method: they must return their result in the exact same order as the provided keys, so that the returned values are properly dispatched in the server's response.
      Type Parameters:
      T - The type of items in these list
      Parameters:
      keys - The list which ordered must be respected.
      unorderedList - A list of items in any order. Each item in this list must have a key which is in the keys list.
      There may be missing values (for instance if a key doesn't match an item in the database). In this case, this value is replaced by a null value.
      keyFieldName - The name of the field, that contain the key, that is: the T's attribute the can be matched against the keys list. For instance: "id"
      Returns:
      A list of T instances coming from the unorderedList, where the key (retrieved by the getter method) of these instances is in the exact same order as the keys list. Missing values in the unorderedList list are replaced by null.
    • getFieldType

      public Class<?> getFieldType(Class<?> owningClass, String fieldName, boolean returnIsMandatory) throws GraphQLRequestPreparationException
      Retrieves the class of the fieldName field of the owningClass class.
      Parameters:
      owningClass -
      fieldName -
      returnIsMandatory - If true, a GraphQLRequestPreparationException is thrown if the field is not found.
      Returns:
      The class of the field. Or null of the field doesn't exist, and returnIdMandatory is false
      Throws:
      GraphQLRequestPreparationException
    • 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
    • getDeclaredField

      public Field getDeclaredField(Class<?> owningClass, String fieldName, boolean mustFindField) throws GraphQLRequestPreparationException
      Returns a Field from the given class.
      Parameters:
      owningClass - The class that should contain this field. If the class's name finishes by Response, as an empty XxxResponse class is created for each Query/Mutation/Subscription (to be compatible with previsous version), then this method also looks in the owningClass's superclass.
      fieldName - The name of the searched field
      mustFindField - If true and the field is not found, a GraphQLRequestPreparationException is thrown.
      If false an the field is not found, the method returns null
      Returns:
      Throws:
      GraphQLRequestPreparationException
    • getSetter

      public <T> Method getSetter(Class<T> clazz, Field field)
      Retrieves the setter for the given field on the given field
      Type Parameters:
      T -
      Parameters:
      t -
      field -
      Returns:
    • getGetter

      public Method getGetter(Class<?> clazz, Field field)
      Retrieves the getter for the given field on the given field
      Type Parameters:
      T -
      Parameters:
      t -
      field -
      Returns:
    • invokeGetter

      public Object invokeGetter(Object object, String fieldName)
      Invoke the getter for the given field name, on the given object. All check exceptions are hidden in a RuntimeException
      Parameters:
      object -
      fieldName -
      Returns:
      the field's value for the given object
      Throws:
      RuntimeException - If any exception occurs
    • invokeSetter

      public void invokeSetter(Object object, Field field, Object value)
      Invoke the setter for the given field, on the given object. All check exceptions are hidden in a RuntimeException
      Parameters:
      object -
      field -
      value -
      Throws:
      RuntimeException - If any exception occurs
    • invokeSetter

      public void invokeSetter(Object object, String fieldName, Object value)
      Invoke the setter for the Field of the given name, on the given object. All check exceptions are hidden in a RuntimeException
      Parameters:
      object -
      fieldName -
      value -
      Throws:
      RuntimeException - If any exception occurs
    • addImport

      public void addImport(Set<String> imports, String targetPackageName, String classname, boolean useJakartaEE9)
      Adds, if necessary the import calculated from the given parameters, into the given set of imports.
      Parameters:
      imports - The set of import, in which the import for the given parameters is to be added
      targetPackageName - The package in which is the class that will contain this import
      classname - the full classname of the class to import
      useJakartaEE9 - If true, thejavax imports must be replaced by jakarta imports
    • getMethod

      public Method getMethod(String methodName, Class<?> clazz, Class<?>... parameterTypes)
      Retrieves the asked method, from its name, class and parameters. This method hides the exception that could be thrown, into a RuntimeException
      Type Parameters:
      T -
      Parameters:
      t -
      field -
      Returns:
      Throws:
      RuntimeException - When an exception occurs while getting the method
    • invokeMethod

      public Object invokeMethod(String methodName, Object object, Object... args)
      Calls the 'methodName' method on the given object.
      Parameters:
      methodName - The name of the method. This method should have no parameter
      object - The given object, on which the 'methodName' method is to be called
      Returns:
    • invokeMethod

      public Object invokeMethod(Method method, Object o, Object... args)
      Invoke the given setter on the given object, with the given value. This method hides the exception that could be thrown, into a RuntimeException
      Parameters:
      method -
      o -
      value -
      Throws:
      RuntimeException - When an exception occurs while accessing the setter
    • invokeStaticMethod

      public Object invokeStaticMethod(String methodName, Class<?> clazz)
      Calls the 'methodName' method on the given class.
      Parameters:
      methodName - The name of the method. This method should have no parameter
      clazz - The given class, on which the 'methodName' method is to be called
      Returns:
    • getJavaName

      public String getJavaName(String name)
      Returns a valid java identifier for the given name.
      Parameters:
      name -
      Returns:
      If name is a default java keyword (so it is not a valid java identifier), then the return prefixed by a JAVA_KEYWORD_PREFIX. Otherwise (which is generally the case), the name is valid, and returned as is the given name
    • isJavaReservedWords

      public boolean isJavaReservedWords(String name)
      Returns true if name is a reserved java keyword
      Parameters:
      name -
      Returns:
    • getClassSimpleName

      public String getClassSimpleName(String classFullNameParam)
      Extract the simple name for a class (without the package name), from its full class name (with the package name)
      Parameters:
      classFullName - The full class name, for instance java.util.Date
      Returns:
      The simple class name (in the above sample: Date)
    • getPackageName

      public String getPackageName(String classFullNameParam)
      Extract the package name for a class, from its full class name (with the package name)
      Parameters:
      classFullName - The full class name, for instance java.util.Date
      Returns:
      The simple class name (in the above sample: java.util)
    • concatStreams

      @SafeVarargs public final <T> Stream<T> concatStreams(Class<T> clazz, boolean parallelStreams, T t1, T t2, T t3, List<? extends T>... lists)
      Concatenate a non limited number of lists into a stream.
      Type Parameters:
      T -
      Parameters:
      clazz - The T class
      parallelStreams - true if the returned stream should be a parallel one
      t1 - An optional item, that'll be added to the returned stream (if not null)
      t2 - An optional item, that'll be added to the returned stream (if not null)
      t3 - An optional item, that'll be added to the returned stream (if not null)
      lists -
      Returns:
    • getValueAsText

      public String getValueAsText(graphql.language.Value<?> value)
      Returns the given value, as text, as it can be written into a generated GraphQL schema.
      A str string default value will be returned as "str",a JEDI enum value will be returned as JEDI, ...
      Returns:
    • getValueAsString

      public String getValueAsString(graphql.language.Value<?> value)
      Returns the given value, as string, as it can be written into the GraphQLDirective.parameterValues() of the GraphQLDirective java annotation.
      A str string default value will be returned as "str",a JEDI enum value will be returned as "JEDI", an object will be returned as "{name:\"specific name\",appearsIn:[NEWHOPE,EMPIRE],type:\"Human\"}"...
      Returns:
    • 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.
    • stringToEnumValue

      public Object stringToEnumValue(Object enumValue, Class<?> enumClass)
      Returns the given enumValue given as a String, into the enum value from the generated POJO of the given class.
      This can not be a parameterized method as the return value may be either an enum, a list of enums...
      Parameters:
      enumValue - May be null, a String, a list of String, a list of lists of String...
      enumClass - The POJO class of the enum
      Returns:
      The same kind of list, but all String representations are replaced by the relevant enum value, based on the GraphQL schema.
    • getLastModified

      public Long getLastModified(File fileOrFolder, boolean maxValue)
      Returns the maximum or minimum value for the lastModified of the given file, or of all the files (not folders) contained into this folder.
      Parameters:
      fileOrFolder - A file or a folder
      maxValue - If true and fileOrFolder is a folder, then this method returns the maximum File.lastModified() found for all its files. If false, then the minimum value is returned.
      Returns:
      if fileOrFolder doesn't exist, then returns null. If fileOrFolder is a file, then returns its File.lastModified() value. Otherwise its a folder. Then it loops into this folder, its subfolders (and so on), and returns the maximum or the minimum (depending on the value of maxValue) lastModified date found for the files found. The date of the directories are ignored.
    • getQuotedScanBasePackages

      public String getQuotedScanBasePackages(String scanBasePackages)