Class FormatCodeBlocks

java.lang.Object
no.sikt.graphitron.generators.codebuilding.FormatCodeBlocks

public class FormatCodeBlocks extends Object
Class containing various helper methods for constructing code with javapoet.
  • Constructor Details

    • FormatCodeBlocks

      public FormatCodeBlocks()
  • Method Details

    • declareRecord

      public static CodeBlock declareRecord(String name, RecordObjectSpecification<?> input, boolean isIterable, boolean isResolver)
      Parameters:
      name - Name of a field that should be declared as a record. This will be the name of the variable.
      input - Input type that should be declared as a record.
      isIterable - Is this record wrapped in a list?
      isResolver - Is this declaration to be used in a resolver?
      Returns:
      CodeBlock that declares a new record variable and that attaches context configuration if needed.
    • recordTransformPart

      public static CodeBlock recordTransformPart(String transformerName, String varName, String typeName, boolean isJava, boolean isInput)
    • ifNotNull

      @NotNull public static @NotNull CodeBlock ifNotNull(String name)
      Returns:
      CodeBlock that contains an if statement with a null check on the provided name.
    • addToList

      @NotNull public static @NotNull CodeBlock addToList(String addTarget, String addition)
      Parameters:
      addTarget - Name of updatable collection to add something to.
      addition - The name of the content that should be added.
      Returns:
      CodeBlock that adds something to an updatable collection.
    • addToList

      @NotNull public static @NotNull CodeBlock addToList(String addTarget)
      Parameters:
      addTarget - Name of updatable collection to add something to, as well as what is added. For the collection a "List" suffix is assumed.
      Returns:
      CodeBlock that adds something to an updatable collection.
    • addToList

      @NotNull public static @NotNull CodeBlock addToList(String addTarget, CodeBlock codeAddition)
      Parameters:
      addTarget - Name of updatable collection to add something to.
      codeAddition - The CodeBlock that provides something that should be added.
      Returns:
      CodeBlock that adds something to an updatable collection.
    • listOf

      @NotNull public static @NotNull CodeBlock listOf()
      Returns:
      CodeBlock that creates an empty List.
    • listOf

      @NotNull public static @NotNull CodeBlock listOf(CodeBlock code)
      Returns:
      CodeBlock that wraps the supplied CodeBlock in a List.
    • listOf

      @NotNull public static @NotNull CodeBlock listOf(String variable)
      Returns:
      CodeBlock that wraps the supplied variable name in a List.
    • listOfIf

      @NotNull public static @NotNull CodeBlock listOfIf(CodeBlock code, boolean condition)
      Returns:
      CodeBlock that wraps the provided CodeBlock in a Java list provided the condition is true.
    • setOf

      @NotNull public static @NotNull CodeBlock setOf()
      Returns:
      CodeBlock that creates an empty Set.
    • setOf

      @NotNull public static @NotNull CodeBlock setOf(CodeBlock code)
      Returns:
      CodeBlock that wraps the supplied CodeBlock in a Set.
    • setOf

      @NotNull public static @NotNull CodeBlock setOf(String variable)
      Returns:
      CodeBlock that wraps the supplied variable name in a Set.
    • mapOf

      @NotNull public static @NotNull CodeBlock mapOf()
      Returns:
      CodeBlock that creates an empty Map.
    • asMethodCall

      public static CodeBlock asMethodCall(String method)
      Returns:
      CodeBlock that wraps this method name in a method call format.
    • asMethodCall

      public static CodeBlock asMethodCall(String source, String method)
      Returns:
      CodeBlock that wraps this method name in a method call format after the specified source.
    • asMethodCall

      public static CodeBlock asMethodCall(TypeName source, String method)
      Returns:
      CodeBlock that wraps this method name in a static method call format after the specified source.
    • asCast

      public static CodeBlock asCast(TypeName type, String variable)
      Returns:
      CodeBlock that wraps this variable in a Java cast.
    • asCast

      public static CodeBlock asCast(TypeName type, CodeBlock code)
      Returns:
      CodeBlock that wraps this code in a Java cast.
    • collectToList

      @NotNull public static @NotNull CodeBlock collectToList()
      Returns:
      CodeBlock that adds a collect to List call to be used on a Stream.
    • findFirst

      @NotNull public static @NotNull CodeBlock findFirst()
      Returns:
      CodeBlock that adds a findFirst call to be used on a collection.
    • nullIfNullElse

      @NotNull public static @NotNull CodeBlock nullIfNullElse(CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock name in a simple null check.
    • nullIfNullElseThis

      @NotNull public static @NotNull CodeBlock nullIfNullElseThis(CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock name in a simple null check.
    • listedNullCheck

      @NotNull public static @NotNull CodeBlock listedNullCheck(String variable, CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock name in a mapping null check.
    • addStringIfNotEmpty

      @NotNull public static @NotNull CodeBlock addStringIfNotEmpty(String target, String addition)
      Returns:
      CodeBlock that adds something to a String if it is not empty.
    • selectionSetLookup

      @NotNull public static @NotNull CodeBlock selectionSetLookup(String path, boolean atResolver, boolean useArguments)
      Returns:
      CodeBlock that checks whether a path is in use.
    • setValue

      @NotNull public static @NotNull CodeBlock setValue(String container, MethodMapping mapping, CodeBlock value, boolean isResolverKey)
      Returns:
      CodeBlock that sets a value through a mapping.
    • setValue

      @NotNull public static @NotNull CodeBlock setValue(String container, MethodMapping mapping, CodeBlock value)
      Returns:
      CodeBlock that sets a value through a mapping.
    • getValue

      @NotNull public static @NotNull CodeBlock getValue(String container, MethodMapping mapping)
      Returns:
      CodeBlock that gets a value through a mapping.
    • newDataFetcher

      @NotNull public static @NotNull CodeBlock newDataFetcher()
      Returns:
      CodeBlock that creates a data fetcher object.
    • newServiceDataFetcherWithTransform

      @NotNull public static @NotNull CodeBlock newServiceDataFetcherWithTransform()
      Returns:
      CodeBlock that creates a service data fetcher through a transform object.
    • declareTransform

      @NotNull public static @NotNull CodeBlock declareTransform()
      Returns:
      CodeBlock that declares a resolver transformer.
    • continueCheck

      @NotNull public static @NotNull CodeBlock continueCheck(String value)
      Returns:
      CodeBlock does a null check on the variable and runs continue if it is.
    • countFunction

      @NotNull public static @NotNull CodeBlock countFunction(String queryLocation, String queryMethodName, String inputList, boolean isService)
      Returns:
      CodeBlock consisting of a function for a count DB call.
    • getQueryClassName

      public static ClassName getQueryClassName(String queryLocation)
    • queryFunction

      @NotNull public static @NotNull CodeBlock queryFunction(String queryLocation, String queryMethodName, String inputList, boolean hasKeyValues, boolean usesKeyValues, boolean isService)
      Returns:
      CodeBlock consisting of a function for a generic DB call.
    • connectionFunction

      public static CodeBlock connectionFunction(ConnectionObjectDefinition connectionType, ObjectDefinition pageInfoType)
      Returns:
      CodeBlock for a function that maps relay connection types.
    • getNodeQueryCallBlock

      @NotNull public static @NotNull CodeBlock getNodeQueryCallBlock(GenerationField field, String variableName, CodeBlock path, boolean atResolver)
      Returns:
      CodeBlock consisting of a function for an ID fetch DB call.
    • declarePageSize

      @NotNull public static @NotNull CodeBlock declarePageSize(int defaultFirst)
      Returns:
      CodeBlock consisting of a declaration of the page size variable through a method call.
    • wrapNotNull

      @NotNull public static @NotNull CodeBlock wrapNotNull(String valueToCheck, CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in an if not null check.
    • wrapFor

      @NotNull public static @NotNull CodeBlock wrapFor(String variable, CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in a for loop.
    • wrapForIndexed

      @NotNull public static @NotNull CodeBlock wrapForIndexed(String variable, CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in an indexed for loop.
    • wrapRow

      @NotNull public static @NotNull CodeBlock wrapRow(CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in a jOOQ row.
    • wrapObjectRow

      @NotNull public static @NotNull CodeBlock wrapObjectRow(CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in a jOOQ row.
    • wrapCoalesce

      @NotNull public static @NotNull CodeBlock wrapCoalesce(CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in a jOOQ coalesce.
    • inline

      @NotNull public static @NotNull CodeBlock inline(CodeBlock code)
      Returns:
      CodeBlock that wraps the provided CodeBlock in a jOOQ inline.
    • indentIfMultiline

      @NotNull public static @NotNull CodeBlock indentIfMultiline(CodeBlock code)
      Returns:
      Add appropriate indentation if this code has multiple lines.
    • makeEnumMapBlock

      public static CodeBlock makeEnumMapBlock(CodeBlock inputVariable, CodeBlock valueLists)
      Returns:
      CodeBlock that sends this variable through an enum mapping.
    • makeEnumMapBlock

      public static CodeBlock makeEnumMapBlock(String inputVariable, CodeBlock valueLists)
      Returns:
      CodeBlock that sends this variable through an enum mapping.
    • toJOOQEnumConverter

      public static CodeBlock toJOOQEnumConverter(String enumType, ProcessedSchema schema)
      Returns:
      Code block containing the enum conversion method call with anonymous function declarations.
    • toGraphEnumConverter

      public static CodeBlock toGraphEnumConverter(String enumType, CodeBlock field, boolean toRecord, ProcessedSchema schema)
      Returns:
      Code block containing the enum conversion method call.
    • applyGlobalTransforms

      public static CodeBlock applyGlobalTransforms(String recordName, TypeName recordTypeName, TransformScope scope)
      Parameters:
      recordName - Name of the record to transform.
      scope - The scope of transforms that should be applied. Currently only TransformScope.ALL_MUTATIONS is supported.
      Returns:
      CodeBlock where all defined global transforms are applied to the record.
    • applyTransform

      public static CodeBlock applyTransform(String recordName, TypeName recordTypeName, Method transform)
      Parameters:
      recordName - Name of the record to transform.
      transform - The method that should transform the record.
      Returns:
      CodeBlock where the transform is applied to the record.
    • makeResponses

      public static CodeBlock makeResponses(MapperContext context, ObjectField field, ProcessedSchema schema, InputParser parser)
      Returns:
      Code for constructing any structure of response types.
    • getIDMappingCode

      public static CodeBlock getIDMappingCode(MapperContext context, ObjectField field, ProcessedSchema schema, InputParser parser)
    • fetchMapping

      public static CodeBlock fetchMapping(boolean iterable)
    • returnWrap

      @NotNull public static @NotNull CodeBlock returnWrap(String variable)
      Returns:
      CodeBlock that returns the provided name.
    • returnWrap

      @NotNull public static @NotNull CodeBlock returnWrap(CodeBlock code)
      Returns:
      CodeBlock that returns the provided code.
    • returnCompletedFuture

      @NotNull public static @NotNull CodeBlock returnCompletedFuture(String variable)
      Returns:
      CodeBlock that wraps and returns the provided variable in a CompletableFuture.
    • returnCompletedFuture

      @NotNull public static @NotNull CodeBlock returnCompletedFuture(CodeBlock code)
      Returns:
      CodeBlock that wraps and returns the provided CodeBlock in a CompletableFuture.
    • inResolverKeysBlock

      public static CodeBlock inResolverKeysBlock(String resolverKeyParamName, FetchContext context)
    • getSelectKeyColumnRow

      public static CodeBlock getSelectKeyColumnRow(org.jooq.Key<?> key, String tableName, String aliasVariableName)
      Returns the select code for the columns of a key.
      Parameters:
      key - The key
      aliasVariableName - The variable name for the table alias
      Returns:
      Select code for the columns in the key
    • getSelectKeyColumnRow

      public static CodeBlock getSelectKeyColumnRow(FetchContext context)
      Returns codeblock for selecting key columns for the resolver key
      Parameters:
      context - The fetching context
      Returns:
      Select code for the columns in the resolver key
    • getSelectKeyColumn

      public static CodeBlock getSelectKeyColumn(org.jooq.Key<?> key, String tableName, String aliasVariableName)
    • getSelectKeyColumn

      public static CodeBlock getSelectKeyColumn(FetchContext context)
    • createNodeIdBlock

      public static CodeBlock createNodeIdBlock(RecordObjectSpecification<?> obj, String targetAlias)
    • createNodeIdBlockForRecord

      public static CodeBlock createNodeIdBlockForRecord(RecordObjectSpecification<?> obj, String recordVariableName)
    • hasIdBlock

      public static CodeBlock hasIdBlock(CodeBlock id, RecordObjectSpecification<?> obj, String targetAlias)
    • hasIdsBlock

      public static CodeBlock hasIdsBlock(RecordObjectSpecification<?> obj, String targetAlias)
    • hasIdOrIdsBlock

      public static CodeBlock hasIdOrIdsBlock(CodeBlock idOrRecordParamName, RecordObjectSpecification<?> obj, String targetAlias, CodeBlock mappedFkFields, boolean isMultiple)
    • nodeIdColumnsBlock

      public static CodeBlock nodeIdColumnsBlock(RecordObjectSpecification<?> obj)
    • referenceNodeIdColumnsBlock

      public static CodeBlock referenceNodeIdColumnsBlock(RecordObjectSpecification<?> container, RecordObjectSpecification<?> target, org.jooq.ForeignKey<?,?> fk)
    • referenceNodeIdColumnsBlock

      public static CodeBlock referenceNodeIdColumnsBlock(RecordObjectSpecification<?> container, RecordObjectSpecification<?> target, org.jooq.ForeignKey<?,?> fk, CodeBlock tableReference)
    • nodeIdColumnsWithAliasBlock

      public static CodeBlock nodeIdColumnsWithAliasBlock(String targetAlias, RecordObjectSpecification<?> obj)
    • getPrimaryKeyFieldsWithTableAliasBlock

      public static CodeBlock getPrimaryKeyFieldsWithTableAliasBlock(String targetAlias)