Skip to content

Commit 778a45e

Browse files
committed
Refactor
1 parent ff1f2af commit 778a45e

File tree

1 file changed

+92
-87
lines changed

1 file changed

+92
-87
lines changed

src/main/kotlin/graphql/kickstart/tools/SchemaParser.kt

Lines changed: 92 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -122,28 +122,30 @@ class SchemaParser internal constructor(
122122
.definition(objectDefinition)
123123
.description(getDocumentation(objectDefinition, options))
124124
.withAppliedDirectives(*buildAppliedDirectives(objectDefinition.directives))
125-
126-
objectDefinition.implements.forEach { implementsDefinition ->
127-
val interfaceName = (implementsDefinition as TypeName).name
128-
builder.withInterface(interfaces.find { it.name == interfaceName }
129-
?: throw SchemaError("Expected interface type with name '$interfaceName' but found none!"))
130-
}
131-
132-
objectDefinition.getExtendedFieldDefinitions(extensionDefinitions).forEach { fieldDefinition ->
133-
builder.field { field ->
134-
createField(field, fieldDefinition, inputObjects)
135-
codeRegistryBuilder.dataFetcher(
136-
FieldCoordinates.coordinates(objectDefinition.name, fieldDefinition.name),
137-
fieldResolversByType[objectDefinition]?.get(fieldDefinition)?.createDataFetcher()
138-
?: throw SchemaError("No resolver method found for object type '${objectDefinition.name}' and field '${fieldDefinition.name}', this is most likely a bug with graphql-java-tools")
139-
)
140-
141-
val wiredField = field.build()
142-
GraphQLFieldDefinition.Builder(wiredField)
143-
.clearArguments()
144-
.arguments(wiredField.arguments)
125+
.apply {
126+
objectDefinition.implements.forEach { implementsDefinition ->
127+
val interfaceName = (implementsDefinition as TypeName).name
128+
withInterface(interfaces.find { it.name == interfaceName }
129+
?: throw SchemaError("Expected interface type with name '$interfaceName' but found none!"))
130+
}
131+
}
132+
.apply {
133+
objectDefinition.getExtendedFieldDefinitions(extensionDefinitions).forEach { fieldDefinition ->
134+
field { field ->
135+
createField(field, fieldDefinition, inputObjects)
136+
codeRegistryBuilder.dataFetcher(
137+
FieldCoordinates.coordinates(objectDefinition.name, fieldDefinition.name),
138+
fieldResolversByType[objectDefinition]?.get(fieldDefinition)?.createDataFetcher()
139+
?: throw SchemaError("No resolver method found for object type '${objectDefinition.name}' and field '${fieldDefinition.name}', this is most likely a bug with graphql-java-tools")
140+
)
141+
142+
val wiredField = field.build()
143+
GraphQLFieldDefinition.Builder(wiredField)
144+
.clearArguments()
145+
.arguments(wiredField.arguments)
146+
}
147+
}
145148
}
146-
}
147149

148150
return directiveWiringHelper.wireObject(builder.build())
149151
}
@@ -152,28 +154,31 @@ class SchemaParser internal constructor(
152154
referencingInputObjects: MutableSet<String>): GraphQLInputObjectType {
153155
val extensionDefinitions = inputExtensionDefinitions.filter { it.name == definition.name }
154156

157+
referencingInputObjects.add(definition.name)
158+
155159
val builder = GraphQLInputObjectType.newInputObject()
156160
.name(definition.name)
157161
.definition(definition)
158162
.extensionDefinitions(extensionDefinitions)
159163
.description(getDocumentation(definition, options))
160164
.withAppliedDirectives(*buildAppliedDirectives(definition.directives))
161-
162-
referencingInputObjects.add(definition.name)
163-
164-
(extensionDefinitions + definition).forEach {
165-
it.inputValueDefinitions.forEach { inputDefinition ->
166-
val fieldBuilder = GraphQLInputObjectField.newInputObjectField()
167-
.name(inputDefinition.name)
168-
.definition(inputDefinition)
169-
.description(getDocumentation(inputDefinition, options))
170-
.apply { inputDefinition.defaultValue?.let { v -> defaultValueLiteral(v) } }
171-
.apply { getDeprecated(inputDefinition.directives)?.let { deprecate(it) } }
172-
.type(determineInputType(inputDefinition.type, inputObjects, referencingInputObjects))
173-
.withAppliedDirectives(*buildAppliedDirectives(inputDefinition.directives))
174-
builder.field(fieldBuilder.build())
165+
.apply {
166+
(extensionDefinitions + definition).forEach { typeDefinition ->
167+
typeDefinition.inputValueDefinitions.forEach { fieldDefinition ->
168+
field(
169+
GraphQLInputObjectField.newInputObjectField()
170+
.name(fieldDefinition.name)
171+
.definition(fieldDefinition)
172+
.description(getDocumentation(fieldDefinition, options))
173+
.apply { fieldDefinition.defaultValue?.let { v -> defaultValueLiteral(v) } }
174+
.apply { getDeprecated(fieldDefinition.directives)?.let { deprecate(it) } }
175+
.type(determineInputType(fieldDefinition.type, inputObjects, referencingInputObjects))
176+
.withAppliedDirectives(*buildAppliedDirectives(fieldDefinition.directives))
177+
.build()
178+
)
179+
}
180+
}
175181
}
176-
}
177182

178183
return directiveWiringHelper.wireInputObject(builder.build())
179184
}
@@ -189,57 +194,59 @@ class SchemaParser internal constructor(
189194
.definition(definition)
190195
.description(getDocumentation(definition, options))
191196
.withAppliedDirectives(*buildAppliedDirectives(definition.directives))
192-
193-
definition.enumValueDefinitions.forEach { enumDefinition ->
194-
val enumName = enumDefinition.name
195-
val enumValue = type.unwrap().enumConstants.find { (it as Enum<*>).name == enumName }
196-
?: throw SchemaError("Expected value for name '$enumName' in enum '${type.unwrap().simpleName}' but found none!")
197-
198-
val enumValueAppliedDirectives = buildAppliedDirectives(enumDefinition.directives)
199-
val enumValueDefinition = GraphQLEnumValueDefinition.newEnumValueDefinition()
200-
.name(enumName)
201-
.description(getDocumentation(enumDefinition, options))
202-
.value(enumValue)
203-
.apply { getDeprecated(enumDefinition.directives)?.let { deprecationReason(it) } }
204-
.withAppliedDirectives(*enumValueAppliedDirectives)
205-
.definition(enumDefinition)
206-
.build()
207-
208-
builder.value(enumValueDefinition)
209-
}
197+
.apply {
198+
definition.enumValueDefinitions.forEach { valueDefinition ->
199+
val enumName = valueDefinition.name
200+
val enumValue = type.unwrap().enumConstants.find { (it as Enum<*>).name == enumName }
201+
?: throw SchemaError("Expected value for name '$enumName' in enum '${type.unwrap().simpleName}' but found none!")
202+
203+
value(
204+
GraphQLEnumValueDefinition.newEnumValueDefinition()
205+
.name(enumName)
206+
.description(getDocumentation(valueDefinition, options))
207+
.value(enumValue)
208+
.apply { getDeprecated(valueDefinition.directives)?.let { deprecationReason(it) } }
209+
.withAppliedDirectives(*buildAppliedDirectives(valueDefinition.directives))
210+
.definition(valueDefinition)
211+
.build()
212+
)
213+
}
214+
}
210215

211216
return directiveWiringHelper.wireEnum(builder.build())
212217
}
213218

214219
private fun createInterfaceObject(interfaceDefinition: InterfaceTypeDefinition, inputObjects: List<GraphQLInputObjectType>): GraphQLInterfaceType {
215-
val name = interfaceDefinition.name
216220
val builder = GraphQLInterfaceType.newInterface()
217-
.name(name)
221+
.name(interfaceDefinition.name)
218222
.definition(interfaceDefinition)
219223
.description(getDocumentation(interfaceDefinition, options))
220224
.withAppliedDirectives(*buildAppliedDirectives(interfaceDefinition.directives))
221-
222-
interfaceDefinition.fieldDefinitions.forEach { fieldDefinition ->
223-
builder.field { field -> createField(field, fieldDefinition, inputObjects) }
224-
}
225-
226-
interfaceDefinition.implements.forEach { implementsDefinition ->
227-
val interfaceName = (implementsDefinition as TypeName).name
228-
builder.withInterface(GraphQLTypeReference(interfaceName))
229-
}
225+
.apply {
226+
interfaceDefinition.fieldDefinitions.forEach { fieldDefinition ->
227+
field { field -> createField(field, fieldDefinition, inputObjects) }
228+
}
229+
}
230+
.apply {
231+
interfaceDefinition.implements.forEach { implementsDefinition ->
232+
val interfaceName = (implementsDefinition as TypeName).name
233+
withInterface(GraphQLTypeReference(interfaceName))
234+
}
235+
}
230236

231237
return directiveWiringHelper.wireInterFace(builder.build())
232238
}
233239

234240
private fun createUnionObject(definition: UnionTypeDefinition, types: List<GraphQLObjectType>): GraphQLUnionType {
235-
val name = definition.name
236241
val builder = GraphQLUnionType.newUnionType()
237-
.name(name)
242+
.name(definition.name)
238243
.definition(definition)
239244
.description(getDocumentation(definition, options))
240245
.withAppliedDirectives(*buildAppliedDirectives(definition.directives))
246+
.apply {
247+
getLeafUnionObjects(definition, types).forEach { possibleType(it) }
248+
}
241249

242-
getLeafUnionObjects(definition, types).forEach { builder.possibleType(it) }
243250
return directiveWiringHelper.wireUnion(builder.build())
244251
}
245252

@@ -264,34 +271,34 @@ class SchemaParser internal constructor(
264271
}
265272

266273
private fun createField(field: GraphQLFieldDefinition.Builder, fieldDefinition: FieldDefinition, inputObjects: List<GraphQLInputObjectType>): GraphQLFieldDefinition.Builder {
267-
field
274+
return field
268275
.name(fieldDefinition.name)
269276
.description(getDocumentation(fieldDefinition, options))
270277
.definition(fieldDefinition)
271278
.apply { getDeprecated(fieldDefinition.directives)?.let { deprecate(it) } }
272279
.type(determineOutputType(fieldDefinition.type, inputObjects))
273280
.withAppliedDirectives(*buildAppliedDirectives(fieldDefinition.directives))
274-
275-
fieldDefinition.inputValueDefinitions.forEach { argumentDefinition ->
276-
val argumentBuilder = GraphQLArgument.newArgument()
277-
.name(argumentDefinition.name)
278-
.definition(argumentDefinition)
279-
.description(getDocumentation(argumentDefinition, options))
280-
.type(determineInputType(argumentDefinition.type, inputObjects, setOf()))
281-
.apply { getDeprecated(argumentDefinition.directives)?.let { deprecate(it) } }
282-
.apply { argumentDefinition.defaultValue?.let { defaultValueLiteral(it) } }
283-
.withAppliedDirectives(*buildAppliedDirectives(argumentDefinition.directives))
284-
285-
field.argument(argumentBuilder.build())
286-
}
287-
288-
return field
281+
.apply {
282+
fieldDefinition.inputValueDefinitions.forEach { argumentDefinition ->
283+
argument(
284+
GraphQLArgument.newArgument()
285+
.name(argumentDefinition.name)
286+
.definition(argumentDefinition)
287+
.description(getDocumentation(argumentDefinition, options))
288+
.type(determineInputType(argumentDefinition.type, inputObjects, setOf()))
289+
.apply { getDeprecated(argumentDefinition.directives)?.let { deprecate(it) } }
290+
.apply { argumentDefinition.defaultValue?.let { defaultValueLiteral(it) } }
291+
.withAppliedDirectives(*buildAppliedDirectives(argumentDefinition.directives))
292+
.build()
293+
)
294+
}
295+
}
289296
}
290297

291298
private fun createDirective(definition: DirectiveDefinition, inputObjects: List<GraphQLInputObjectType>): GraphQLDirective {
292299
val locations = definition.directiveLocations.map { Introspection.DirectiveLocation.valueOf(it.name) }.toTypedArray()
293300

294-
val graphQLDirective = GraphQLDirective.newDirective()
301+
return GraphQLDirective.newDirective()
295302
.name(definition.name)
296303
.description(getDocumentation(definition, options))
297304
.definition(definition)
@@ -312,8 +319,6 @@ class SchemaParser internal constructor(
312319
}
313320
}
314321
.build()
315-
316-
return graphQLDirective
317322
}
318323

319324
private fun buildAppliedDirectives(directives: List<Directive>): Array<GraphQLAppliedDirective> {

0 commit comments

Comments
 (0)