Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create singleton empty CodeGenResult instance #725

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ object EntitiesRepresentationTypeGeneratorUtils {
representationGenerator: RepresentationGenerator
): CodeGenResult {
if (config.skipEntityQueries) {
return CodeGenResult()
return CodeGenResult.EMPTY
}
val representationName = toRepresentationName(definition)
if (representationName in generatedRepresentations) {
return CodeGenResult()
return CodeGenResult.EMPTY
}

val directiveArg =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,23 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document

val rootProjection =
it.type.findTypeDefinition(document, true)?.let { typeDefinition -> createRootProjection(typeDefinition, it.name.capitalized()) }
?: CodeGenResult()
?: CodeGenResult.EMPTY
CodeGenResult(javaQueryTypes = listOf(javaFile)).merge(rootProjection)
}.fold(CodeGenResult()) { total, current -> total.merge(current) }
}.fold(CodeGenResult.EMPTY) { total, current -> total.merge(current) }
}

fun generateEntities(definitions: List<ObjectTypeDefinition>): CodeGenResult {
if (config.skipEntityQueries) {
return CodeGenResult()
return CodeGenResult.EMPTY
}

var entitiesRootProjection = CodeGenResult()
// generate for federation types, if present
val federatedTypes = definitions.filter { it.hasDirective("key") }
if (federatedTypes.isNotEmpty()) {
// create entities root projection
entitiesRootProjection = createEntitiesRootProjection(federatedTypes)
return createEntitiesRootProjection(federatedTypes)
}
return CodeGenResult().merge(entitiesRootProjection)
return CodeGenResult.EMPTY
}

private fun createQueryClass(it: FieldDefinition, operation: String, methodNames: MutableSet<String>): JavaFile {
Expand Down Expand Up @@ -270,7 +269,7 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document
.build()
)

if (generatedClasses.contains(clazzName)) return CodeGenResult() else generatedClasses.add(clazzName)
if (generatedClasses.contains(clazzName)) return CodeGenResult.EMPTY else generatedClasses.add(clazzName)

val fieldDefinitions = type.fieldDefinitions() + document.definitions.filterIsInstance<ObjectTypeExtensionDefinition>().filter { it.name == type.name }.flatMap { it.fieldDefinitions }

Expand Down Expand Up @@ -317,7 +316,7 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document
1
)
}
.fold(CodeGenResult()) { total, current -> total.merge(current) }
.fold(CodeGenResult.EMPTY) { total, current -> total.merge(current) }

fieldDefinitions.filterSkipped().forEach {
val objectTypeDefinition = it.type.findTypeDefinition(document)
Expand Down Expand Up @@ -399,7 +398,7 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document
.build()
)

if (generatedClasses.contains(clazzName)) return CodeGenResult() else generatedClasses.add(clazzName)
if (generatedClasses.contains(clazzName)) return CodeGenResult.EMPTY else generatedClasses.add(clazzName)

val codeGenResult = federatedTypes.map { objTypeDef ->
val projectionName = "Entities${objTypeDef.name.capitalized()}KeyProjection"
Expand All @@ -419,7 +418,7 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document
)
val processedEdges = mutableSetOf<Pair<String, String>>()
createFragment(objTypeDef, javaType.build(), javaType.build(), "Entities${objTypeDef.name.capitalized()}Key", processedEdges, 0)
}.fold(CodeGenResult()) { total, current -> total.merge(current) }
}.fold(CodeGenResult.EMPTY) { total, current -> total.merge(current) }

val javaFile = JavaFile.builder(getPackageName(), javaType.build()).build()
return CodeGenResult(clientProjections = listOf(javaFile)).merge(codeGenResult)
Expand All @@ -432,9 +431,9 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document
}
concreteTypes.map {
addFragmentProjectionMethod(javaType, root, prefix, it, processedEdges, queryDepth)
}.fold(CodeGenResult()) { total, current -> total.merge(current) }
}.fold(CodeGenResult.EMPTY) { total, current -> total.merge(current) }
} else {
CodeGenResult()
CodeGenResult.EMPTY
}
}

Expand All @@ -443,9 +442,9 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document
val memberTypes = type.memberTypes.mapNotNull { it.findTypeDefinition(document, true) }.toList()
memberTypes.map {
addFragmentProjectionMethod(javaType, rootType, prefix, it, processedEdges, queryDepth)
}.fold(CodeGenResult()) { total, current -> total.merge(current) }
}.fold(CodeGenResult.EMPTY) { total, current -> total.merge(current) }
} else {
CodeGenResult()
CodeGenResult.EMPTY
}
}

Expand Down Expand Up @@ -475,7 +474,7 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document

private fun createFragment(type: ObjectTypeDefinition, parent: TypeSpec, root: TypeSpec, prefix: String, processedEdges: Set<Pair<String, String>>, queryDepth: Int): CodeGenResult {
val subProjection = createSubProjectionType(type, parent, root, prefix, processedEdges, queryDepth)
?: return CodeGenResult()
?: return CodeGenResult.EMPTY
val javaType = subProjection.first
val codeGenResult = subProjection.second

Expand Down Expand Up @@ -518,7 +517,7 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document

private fun createSubProjection(type: TypeDefinition<*>, parent: TypeSpec, root: TypeSpec, prefix: String, processedEdges: Set<Pair<String, String>>, queryDepth: Int): CodeGenResult {
val subProjection = createSubProjectionType(type, parent, root, prefix, processedEdges, queryDepth)
?: return CodeGenResult()
?: return CodeGenResult.EMPTY
val javaType = subProjection.first
val codeGenResult = subProjection.second

Expand Down Expand Up @@ -609,7 +608,7 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document
queryDepth + 1
)
}
.fold(CodeGenResult()) { total, current -> total.merge(current) }
.fold(CodeGenResult.EMPTY) { total, current -> total.merge(current) }

fieldDefinitions
.filterSkipped()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import com.netflix.graphql.dgs.codegen.generators.shared.SchemaExtensionsUtils.f
import com.netflix.graphql.dgs.codegen.generators.shared.SchemaExtensionsUtils.findInterfaceExtensions
import com.netflix.graphql.dgs.codegen.generators.shared.SchemaExtensionsUtils.findTypeExtensions
import com.netflix.graphql.dgs.codegen.generators.shared.excludeSchemaTypeExtension
import com.squareup.javapoet.ClassName
import com.squareup.javapoet.FieldSpec
import com.squareup.javapoet.JavaFile
import com.squareup.javapoet.TypeName
import com.squareup.javapoet.TypeSpec
import graphql.language.*
import javax.lang.model.element.Modifier
Expand All @@ -48,7 +48,11 @@ class ConstantsGenerator(private val config: CodeGenConfig, private val document
val extensions = findTypeExtensions(it.name, document.definitions)
val fields = it.fieldDefinitions + extensions.flatMap { ext -> ext.fieldDefinitions }

constantsType.addField(FieldSpec.builder(TypeName.get(String::class.java), "TYPE_NAME").addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer(""""${it.name}"""").build())
constantsType.addField(
FieldSpec.builder(ClassName.get(String::class.java), "TYPE_NAME")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("\$S", it.name).build()
)

fields.forEach { field ->
addFieldNameConstant(constantsType, field.name)
Expand All @@ -63,13 +67,21 @@ class ConstantsGenerator(private val config: CodeGenConfig, private val document
.excludeSchemaTypeExtension()
.forEach {
val constantsType = createConstantTypeBuilder(config, it.name)
constantsType.addField(FieldSpec.builder(TypeName.get(String::class.java), "TYPE_NAME").addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer(""""${it.name}"""").build())
constantsType.addField(
FieldSpec.builder(ClassName.get(String::class.java), "TYPE_NAME")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("\$S", it.name).build()
)

for (definition in it.inputValueDefinitions) {
addFieldNameConstant(constantsType, definition.name)
}

val extensions = findInputExtensions(it.name, document.definitions)
val fields = it.inputValueDefinitions + extensions.flatMap { ext -> ext.inputValueDefinitions }

fields.forEach { field ->
addFieldNameConstant(constantsType, field.name)
for (extension in extensions) {
for (definition in extension.inputValueDefinitions) {
addFieldNameConstant(constantsType, definition.name)
}
}

javaType.addType(constantsType.build())
Expand All @@ -81,13 +93,21 @@ class ConstantsGenerator(private val config: CodeGenConfig, private val document
.forEach {
val constantsType = createConstantTypeBuilder(config, it.name)

constantsType.addField(FieldSpec.builder(TypeName.get(String::class.java), "TYPE_NAME").addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer(""""${it.name}"""").build())
constantsType.addField(
FieldSpec.builder(ClassName.get(String::class.java), "TYPE_NAME")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("\$S", it.name).build()
)

val extensions = findInterfaceExtensions(it.name, document.definitions)
val merged = it.fieldDefinitions + extensions.flatMap { ext -> ext.fieldDefinitions }
for (definition in it.fieldDefinitions) {
addFieldNameConstant(constantsType, definition.name)
}

merged.forEach { field ->
addFieldNameConstant(constantsType, field.name)
val extensions = findInterfaceExtensions(it.name, document.definitions)
for (extension in extensions) {
for (definition in extension.fieldDefinitions) {
addFieldNameConstant(constantsType, definition.name)
}
}

javaType.addType(constantsType.build())
Expand All @@ -98,17 +118,33 @@ class ConstantsGenerator(private val config: CodeGenConfig, private val document
.excludeSchemaTypeExtension()
.forEach {
val constantsType = createConstantTypeBuilder(config, it.name)
constantsType.addField(FieldSpec.builder(TypeName.get(String::class.java), "TYPE_NAME").addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer(""""${it.name}"""").build())
constantsType.addField(
FieldSpec.builder(ClassName.get(String::class.java), "TYPE_NAME")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("\$S", it.name).build()
)
}

if (document.definitions.any { it is ObjectTypeDefinition && it.name == "Query" }) {
javaType.addField(FieldSpec.builder(TypeName.get(String::class.java), "QUERY_TYPE").addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer(""""Query"""").build())
javaType.addField(
FieldSpec.builder(ClassName.get(String::class.java), "QUERY_TYPE")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer(""""Query"""").build()
)
}
if (document.definitions.any { it is ObjectTypeDefinition && it.name == "Mutation" }) {
javaType.addField(FieldSpec.builder(TypeName.get(String::class.java), "MUTATION_TYPE").addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer(""""Mutation"""").build())
javaType.addField(
FieldSpec.builder(ClassName.get(String::class.java), "MUTATION_TYPE")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer(""""Mutation"""").build()
)
}
if (document.definitions.any { it is ObjectTypeDefinition && it.name == "Subscription" }) {
javaType.addField(FieldSpec.builder(TypeName.get(String::class.java), "SUBSCRIPTION_TYPE").addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer(""""Subscription"""").build())
javaType.addField(
FieldSpec.builder(ClassName.get(String::class.java), "SUBSCRIPTION_TYPE")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer(""""Subscription"""").build()
)
}

val javaFile = JavaFile.builder(config.packageName, javaType.build()).build()
Expand All @@ -132,10 +168,10 @@ class ConstantsGenerator(private val config: CodeGenConfig, private val document
private fun addFieldNameConstant(constantsType: TypeSpec.Builder, fieldName: String) {
constantsType.addField(
FieldSpec.builder(
TypeName.get(String::class.java),
ClassName.get(String::class.java),
ReservedKeywordSanitizer.sanitize(fieldName.capitalized())
)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer(""""$fieldName"""").build()
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer("\$S", fieldName).build()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class DataTypeGenerator(config: CodeGenConfig, document: Document) : BaseDataTyp

fun generate(definition: ObjectTypeDefinition, extensions: List<ObjectTypeExtensionDefinition>): CodeGenResult {
if (definition.shouldSkip(config)) {
return CodeGenResult()
return CodeGenResult.EMPTY
}

logger.info("Generating data type {}", definition.name)
Expand All @@ -80,7 +80,7 @@ class DataTypeGenerator(config: CodeGenConfig, document: Document) : BaseDataTyp

var useInterfaceType = false
var overrideGetter = false
var interfaceCodeGenResult = CodeGenResult()
var interfaceCodeGenResult = CodeGenResult.EMPTY

if (config.generateInterfaces) {
useInterfaceType = true
Expand Down Expand Up @@ -162,7 +162,7 @@ class InputTypeGenerator(config: CodeGenConfig, document: Document) : BaseDataTy
inputTypeDefinitions: List<InputObjectTypeDefinition>
): CodeGenResult {
if (definition.shouldSkip(config)) {
return CodeGenResult()
return CodeGenResult.EMPTY
}

logger.info("Generating input type {}", definition.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import graphql.language.EnumTypeDefinition
import graphql.language.FieldDefinition
import graphql.language.InterfaceTypeDefinition
import graphql.language.ObjectTypeDefinition
import org.slf4j.Logger
import org.slf4j.LoggerFactory

@Suppress("UNCHECKED_CAST")
Expand Down Expand Up @@ -59,9 +60,9 @@ class EntitiesRepresentationTypeGenerator(
keyFields: Map<String, Any>
): CodeGenResult {
if (generatedRepresentations.containsKey(representationName)) {
return CodeGenResult()
return CodeGenResult.EMPTY
}
var fieldsCodeGenAccumulator = CodeGenResult()
var fieldsCodeGenAccumulator = CodeGenResult.EMPTY
// generate representations of entity types that have @key, including the __typename field, and the key fields
val typeName = Field("__typename", ClassName.get(String::class.java), CodeBlock.of("\$S", definitionName))
val fieldDefinitions =
Expand All @@ -84,7 +85,7 @@ class EntitiesRepresentationTypeGenerator(
.replace(type.name, fieldTypeRepresentationName)

if (generatedRepresentations.containsKey(fieldTypeRepresentationName)) {
logger.trace("Representation fo $fieldTypeRepresentationName was already generated.")
logger.trace("Representation for {} was already generated.", fieldTypeRepresentationName)
} else {
logger.debug("Generating entity representation {} ...", fieldTypeRepresentationName)
val fieldTypeRepresentation = generateRepresentations(
Expand All @@ -106,7 +107,7 @@ class EntitiesRepresentationTypeGenerator(
val parentRepresentationCodeGen = super.generate(
name = representationName,
interfaces = emptyList(),
fields = fieldDefinitions.plus(typeName),
fields = fieldDefinitions + typeName,
description = null,
directives = emptyList()
)
Expand All @@ -116,7 +117,6 @@ class EntitiesRepresentationTypeGenerator(
}

companion object {
private val logger: org.slf4j.Logger =
LoggerFactory.getLogger(EntitiesRepresentationTypeGenerator::class.java)
private val logger: Logger = LoggerFactory.getLogger(EntitiesRepresentationTypeGenerator::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class EnumTypeGenerator(private val config: CodeGenConfig) {

fun generate(definition: EnumTypeDefinition, extensions: List<EnumTypeDefinition>): CodeGenResult {
if (definition.shouldSkip(config)) {
return CodeGenResult()
return CodeGenResult.EMPTY
}

logger.info("Generating enum type ${definition.name}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class InterfaceGenerator(private val config: CodeGenConfig, private val document
extensions: List<InterfaceTypeExtensionDefinition>
): CodeGenResult {
if (definition.shouldSkip(config)) {
return CodeGenResult()
return CodeGenResult.EMPTY
}

logger.info("Generating type ${definition.name}")
Expand Down
Loading
Loading