Skip to content

Commit 2bec6da

Browse files
committed
Extended supported types for directives
1 parent 04e2733 commit 2bec6da

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/main/kotlin/com/coxautodev/graphql/tools/SchemaParser.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, privat
134134
.definition(definition)
135135
.description(getDocumentation(definition))
136136

137-
val directiveDefinitions = setOf<GraphQLDirective>()
138-
builder.withDirectives(*buildDirectives(definition.directives, directiveDefinitions, Introspection.DirectiveLocation.OBJECT))
137+
builder.withDirectives(*buildDirectives(definition.directives, setOf(), Introspection.DirectiveLocation.OBJECT))
139138

140139
definition.implements.forEach { implementsDefinition ->
141140
val interfaceName = (implementsDefinition as TypeName).name
@@ -177,6 +176,8 @@ class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, privat
177176
.definition(definition)
178177
.description(getDocumentation(definition))
179178

179+
builder.withDirectives(*buildDirectives(definition.directives, setOf(), Introspection.DirectiveLocation.INPUT_OBJECT))
180+
180181
definition.inputValueDefinitions.forEach { inputDefinition ->
181182
builder.field { field ->
182183
field.name(inputDefinition.name)
@@ -187,7 +188,7 @@ class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, privat
187188
}
188189
}
189190

190-
return builder.build()
191+
return directiveGenerator.onInputObject(builder.build(), DirectiveBehavior.Params(runtimeWiring))
191192
}
192193

193194
private fun createEnumObject(definition: EnumTypeDefinition): GraphQLEnumType {
@@ -200,6 +201,8 @@ class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, privat
200201
.definition(definition)
201202
.description(getDocumentation(definition))
202203

204+
builder.withDirectives(*buildDirectives(definition.directives, setOf(), Introspection.DirectiveLocation.ENUM))
205+
203206
definition.enumValueDefinitions.forEach { enumDefinition ->
204207
val enumName = enumDefinition.name
205208
val enumValue = type.unwrap().enumConstants.find { (it as Enum<*>).name == enumName } ?: throw SchemaError("Expected value for name '$enumName' in enum '${type.unwrap().simpleName}' but found none!")
@@ -211,7 +214,7 @@ class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, privat
211214
}
212215
}
213216

214-
return builder.build()
217+
return directiveGenerator.onEnum(builder.build(), DirectiveBehavior.Params(runtimeWiring))
215218
}
216219

217220
private fun createInterfaceObject(definition: InterfaceTypeDefinition): GraphQLInterfaceType {
@@ -222,11 +225,13 @@ class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, privat
222225
.description(getDocumentation(definition))
223226
.typeResolver(TypeResolverProxy())
224227

228+
builder.withDirectives(*buildDirectives(definition.directives, setOf(), Introspection.DirectiveLocation.INTERFACE))
229+
225230
definition.fieldDefinitions.forEach { fieldDefinition ->
226231
builder.field { field -> createField(field, fieldDefinition) }
227232
}
228233

229-
return builder.build()
234+
return directiveGenerator.onInterface(builder.build(), DirectiveBehavior.Params(runtimeWiring))
230235
}
231236

232237
private fun createUnionObject(definition: UnionTypeDefinition, types: List<GraphQLObjectType>): GraphQLUnionType {
@@ -237,8 +242,10 @@ class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, privat
237242
.description(getDocumentation(definition))
238243
.typeResolver(TypeResolverProxy())
239244

245+
builder.withDirectives(*buildDirectives(definition.directives, setOf(), Introspection.DirectiveLocation.UNION))
246+
240247
getLeafUnionObjects(definition, types).forEach { builder.possibleType(it) }
241-
return builder.build()
248+
return directiveGenerator.onUnion(builder.build(), DirectiveBehavior.Params(runtimeWiring))
242249
}
243250

244251
private fun getLeafUnionObjects(definition: UnionTypeDefinition, types: List<GraphQLObjectType>): List<GraphQLObjectType> {

src/main/kotlin/graphql/schema/idl/DirectiveBehavior.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package graphql.schema.idl
22

3+
import graphql.schema.GraphQLArgument
4+
import graphql.schema.GraphQLEnumType
5+
import graphql.schema.GraphQLEnumValueDefinition
36
import graphql.schema.GraphQLFieldDefinition
7+
import graphql.schema.GraphQLInputObjectField
8+
import graphql.schema.GraphQLInputObjectType
9+
import graphql.schema.GraphQLInterfaceType
410
import graphql.schema.GraphQLObjectType
11+
import graphql.schema.GraphQLScalarType
12+
import graphql.schema.GraphQLUnionType
513

614
class DirectiveBehavior {
715

@@ -15,6 +23,30 @@ class DirectiveBehavior {
1523
return directiveHelper.onField(element, params.toParameters())
1624
}
1725

26+
fun onInterface(element: GraphQLInterfaceType, params: Params): GraphQLInterfaceType =
27+
directiveHelper.onInterface(element, params.toParameters())
28+
29+
fun onUnion(element: GraphQLUnionType, params: Params): GraphQLUnionType =
30+
directiveHelper.onUnion(element, params.toParameters())
31+
32+
fun onScalar(element: GraphQLScalarType, params: Params): GraphQLScalarType =
33+
directiveHelper.onScalar(element, params.toParameters())
34+
35+
fun onEnum(element: GraphQLEnumType, params: Params): GraphQLEnumType =
36+
directiveHelper.onEnum(element, params.toParameters())
37+
38+
fun onEnumValue(element: GraphQLEnumValueDefinition, params: Params): GraphQLEnumValueDefinition =
39+
directiveHelper.onEnumValue(element, params.toParameters())
40+
41+
fun onArgument(element: GraphQLArgument, params: Params): GraphQLArgument =
42+
directiveHelper.onArgument(element, params.toParameters())
43+
44+
fun onInputObject(element: GraphQLInputObjectType, params: Params): GraphQLInputObjectType =
45+
directiveHelper.onInputObjectType(element, params.toParameters())
46+
47+
fun onInputObjectField(element: GraphQLInputObjectField, params: Params): GraphQLInputObjectField =
48+
directiveHelper.onInputObjectField(element, params.toParameters())
49+
1850
data class Params(val runtimeWiring: RuntimeWiring) {
1951
internal fun toParameters() = SchemaGeneratorDirectiveHelper.Parameters(null, runtimeWiring, null, null)
2052
}

0 commit comments

Comments
 (0)