Skip to content

Commit a3ae924

Browse files
authored
Merge pull request #21 from mstachniuk/ms2
#20 Add support for directives in schema
2 parents c819a63 + d201732 commit a3ae924

File tree

5 files changed

+954
-3
lines changed

5 files changed

+954
-3
lines changed

core/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@ dependencies {
3030
testCompile "org.junit.jupiter:junit-jupiter-params:$junitVersion"
3131
testRuntime "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
3232
testCompile 'org.json:json:20180813'
33-
testCompile 'com.graphql-java:graphql-java:2018-08-23T21-16-30-141b8e4'
33+
testCompile 'com.graphql-java:graphql-java:9.0'
3434
testCompile 'org.skyscreamer:jsonassert:1.5.0'
3535
ktlint "com.github.shyiko:ktlint:0.27.0"
3636
}
3737

38+
// graphql-java:
39+
// 8.0 directives are NOT visible in introspection query
40+
// 9.0 directives are visible in introspection query
41+
3842
compileKotlin {
3943
kotlinOptions.jvmTarget = "1.8"
4044
}

core/src/main/kotlin/io/github/mstachniuk/graphqlschemafromintrospectiongenerator/internal/GeneratorImpl.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,33 @@ class GeneratorImpl {
77

88
val margin = " "
99
val primitiveScalars = listOf("Boolean", "Float", "ID", "Int", "String")
10+
val primitiveDirectives = listOf("include", "skip", "defer", "deprecated")
1011

1112
fun generate(
1213
input: String,
1314
settings: GeneratorSettings
1415
): String {
1516
val response = Klaxon().parse<IntrospectionResponse>(input) ?: return ""
1617

17-
val output = printSchema(response) + printTypes(response, settings)
18+
val output = printSchema(response) + printTypes(response, settings) + printDirectives(response, settings)
1819

1920
return output.trimIndent().trimIndent()
2021
}
2122

23+
private fun printDirectives(response: IntrospectionResponse, settings: GeneratorSettings): String {
24+
return response.data.schema.directives!!
25+
.filter { !primitiveDirectives.contains(it.name) }
26+
.sortedBy { it.name }
27+
.map { "${printDescription(it, settings, false)}directive @${it.name}${printArguments(it.args, settings)} on ${printDirectiveLocation(it)}" }
28+
.joinToString("\n\n")
29+
}
30+
31+
private fun printDirectiveLocation(directive: GraphQLDirective): String {
32+
return directive.locations
33+
.sortedBy { it }
34+
.joinToString(" | ")
35+
}
36+
2237
private fun printSchema(response: IntrospectionResponse): String {
2338
if (isCustomQueryType(response) || isCustomMutationType(response) || isCustomSubscriptionType(response)
2439
) {

core/src/main/kotlin/io/github/mstachniuk/graphqlschemafromintrospectiongenerator/internal/Schema.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ data class Schema(
99
val queryType: QueryType?,
1010
val mutationType: MutationType? = null,
1111
val subscriptionType: SubscriptionType? = null,
12-
val types: List<GraphQLType>? = null
12+
val types: List<GraphQLType>? = null,
13+
val directives: List<GraphQLDirective>? = null
1314
)
1415

1516
data class QueryType(val name: String)
@@ -48,3 +49,10 @@ data class GraphQLEnumType(
4849
interface Descriptable {
4950
val description: String
5051
}
52+
53+
data class GraphQLDirective(
54+
val name: String,
55+
override val description: String = "",
56+
val locations: List<String> = listOf(),
57+
val args: List<GraphQLField> = listOf()
58+
) : Descriptable

0 commit comments

Comments
 (0)