Skip to content

Commit c1249b5

Browse files
gscheibelrickfast
authored andcommitted
GraphQL ID example (#55)
* Support GraphQLID for input types * Add example of query and mutation using Scalar ID type
1 parent 522516b commit c1249b5

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

example/src/main/kotlin/com.expedia.graphql.sample/query/CustomScalarQuery.kt

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.expedia.graphql.sample.query
2+
3+
import com.expedia.graphql.annotations.GraphQLDescription
4+
import com.expedia.graphql.annotations.GraphQLID
5+
import com.expedia.graphql.sample.mutation.Mutation
6+
import org.springframework.stereotype.Component
7+
import java.util.UUID
8+
9+
/**
10+
* Simple query that exposes custom scalar.
11+
*/
12+
@Component
13+
class ScalarQuery: Query {
14+
15+
@GraphQLDescription("generates random UUID")
16+
fun generateRandomUUID() = UUID.randomUUID()
17+
18+
fun findPersonById(id: Int) = Person(id, "Nelson")
19+
}
20+
21+
@Component
22+
class ScalarMutation : Mutation {
23+
fun addPerson(person: Person): Person = person
24+
}
25+
26+
data class Person(
27+
@property:GraphQLID
28+
val id: Int,
29+
30+
val name: String
31+
)

src/main/kotlin/com/expedia/graphql/schema/generator/SchemaGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ internal class SchemaGenerator(
265265

266266
builder.description(prop.graphQLDescription())
267267
builder.name(prop.name)
268-
builder.type(graphQLTypeOf(prop.returnType, true) as GraphQLInputType)
268+
builder.type(graphQLTypeOf(prop.returnType, true, prop.isGraphQLID()) as GraphQLInputType)
269269

270270
return builder.build()
271271
}

src/test/kotlin/com/expedia/graphql/schema/generator/SchemaGeneratorTest.kt

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.expedia.graphql.schema.extensions.deepName
1010
import com.expedia.graphql.schema.testSchemaConfig
1111
import com.expedia.graphql.toSchema
1212
import graphql.GraphQL
13+
import graphql.Scalars
1314
import graphql.schema.GraphQLNonNull
1415
import graphql.schema.GraphQLObjectType
1516
import org.junit.jupiter.api.Assertions.assertNull
@@ -248,10 +249,10 @@ class SchemaGeneratorTest {
248249
val schema = toSchema(queries = listOf(TopLevelObjectDef(QueryWithId())), config = testSchemaConfig)
249250

250251
val placeType = schema.getObjectType("PlaceOfIds")
251-
assertEquals(graphql.Scalars.GraphQLID, (placeType.getFieldDefinition("intId").type as? GraphQLNonNull)?.wrappedType)
252-
assertEquals(graphql.Scalars.GraphQLID, (placeType.getFieldDefinition("longId").type as? GraphQLNonNull)?.wrappedType)
253-
assertEquals(graphql.Scalars.GraphQLID, (placeType.getFieldDefinition("stringId").type as? GraphQLNonNull)?.wrappedType)
254-
assertEquals(graphql.Scalars.GraphQLID, (placeType.getFieldDefinition("uuid").type as? GraphQLNonNull)?.wrappedType)
252+
assertEquals(Scalars.GraphQLID, (placeType.getFieldDefinition("intId").type as? GraphQLNonNull)?.wrappedType)
253+
assertEquals(Scalars.GraphQLID, (placeType.getFieldDefinition("longId").type as? GraphQLNonNull)?.wrappedType)
254+
assertEquals(Scalars.GraphQLID, (placeType.getFieldDefinition("stringId").type as? GraphQLNonNull)?.wrappedType)
255+
assertEquals(Scalars.GraphQLID, (placeType.getFieldDefinition("uuid").type as? GraphQLNonNull)?.wrappedType)
255256
}
256257

257258
@Test
@@ -263,6 +264,15 @@ class SchemaGeneratorTest {
263264
assertEquals("Person is not a valid ID type, only [kotlin.Int, kotlin.String, kotlin.Long, java.util.UUID] are accepted", exception.message)
264265
}
265266

267+
@Test
268+
fun `SchemaGenerator supports Scalar GraphQLID for input types`() {
269+
val schema = toSchema(mutations = listOf(TopLevelObjectDef(MutationWithId())), config = testSchemaConfig)
270+
271+
val furnitureType = schema.getObjectType("Furniture")
272+
val serialField = furnitureType.getFieldDefinition("serial").type as? GraphQLNonNull
273+
assertEquals(Scalars.GraphQLID, serialField?.wrappedType)
274+
}
275+
266276
class QueryObject {
267277
@GraphQLDescription("A GraphQL query method")
268278
fun query(@GraphQLDescription("A GraphQL value") value: Int): Geography = Geography(value, GeoType.CITY, listOf())
@@ -407,4 +417,13 @@ class SchemaGeneratorTest {
407417
class QueryWithInvalidId {
408418
fun query(): InvalidIds = InvalidIds(Person("person id not a valid type id"))
409419
}
420+
421+
class MutationWithId {
422+
fun mutate(furniture: Furniture): Furniture = furniture
423+
}
424+
425+
data class Furniture(
426+
@property:GraphQLID val serial: UUID,
427+
val type: String
428+
)
410429
}

0 commit comments

Comments
 (0)