File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -22,3 +22,58 @@ enum MyEnumType {
2222 TWO
2323}
2424```
25+
26+ ## Converting a Java enum to a GraphQL Enum
27+
28+ If you want to use Java enums from another package, but you **don't** want
29+ include everything from that package using [`supportedPackages`][sp] or you want
30+ to customize the GraphQL type, you can use [schema generator hooks][hooks] to
31+ associate the Java enum with a runtime [`GraphQLEnumType`][javadoc].
32+
33+ [sp]:customizing-schemas/generator-config
34+ [hooks]:customizing-schemas/generator-config#schema-generator-hooks
35+ [javadoc]:https://javadoc.io/doc/com.graphql-java/graphql-java/latest/index.html
36+
37+ Step 1: Create a GraphQLEnumType using the Java enum values
38+
39+ ```java
40+ // in some other package
41+ public enum Status {
42+ APPROVED,
43+ DECLINED
44+ }
45+ ```
46+
47+ ```kotlin
48+ val statusEnumType = GraphQLEnumType.newEnum()
49+ .name(" Status ")
50+ .values(Status.values().map {
51+ GraphQLEnumValueDefinition.newEnumValueDefinition()
52+ .value(it.name)
53+ .build()
54+ })
55+ .build()
56+ ```
57+
58+ Step 2: Add a schema generation hook
59+
60+ ```kotlin
61+ class CustomSchemaGeneratorHooks : SchemaGeneratorHooks {
62+
63+ override fun willGenerateGraphQLType(type: KType): GraphQLType? {
64+ return when (type.classifier as? KClass<*>) {
65+ Status::class.java -> statusEnumType
66+ else -> super.willGenerateGraphQLType(type)
67+ }
68+ }
69+ }
70+ ```
71+
72+ Step 3. Use your Java enum anywhere in your schema
73+
74+ ```kotlin
75+ @Component
76+ class StatusQuery : Query {
77+ fun currentStatus: Status = getCurrentStatus()
78+ }
79+ ```
You can’t perform that action at this time.
0 commit comments