Skip to content

Commit d9d8991

Browse files
authored
[generator] deprecated directive cleanup (#591)
`graphql-java` now adds `@deprecated` directive definition automatically so we dont have to explicitly add it anymore. This change also fixes handling of multiline directive definition declaration in the federation library. I verified locally that our federation example still works fine with deprecated query. Unsure if this integration test (i.e. starting up 2 SpringBoot apps and node gateway and then running HTTP request) is something we can automate. Posted a question about it on Github forum: https://i.8713187.xyzmunity/t5/GitHub-Actions/integration-test-that-starts-multiple-apps/m-p/44918#M5969 Related issue: graphql-java/graphql-java#1598
1 parent 5605e24 commit d9d8991

File tree

9 files changed

+35
-55
lines changed

9 files changed

+35
-55
lines changed

examples/federation/base-app/src/main/kotlin/com/expediagroup/graphql/examples/query/SimpleQuery.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Expedia, Inc
2+
* Copyright 2020 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,4 +23,7 @@ import org.springframework.stereotype.Component
2323
@Component
2424
class SimpleQuery : Query {
2525
fun dataFromBaseApp() = "hello from base app"
26+
27+
@Deprecated(message = "old deprecated query", replaceWith = ReplaceWith("dataFromBaseApp"))
28+
fun deprecatedBaseAppQuery() = "this is deprecated"
2629
}

examples/federation/gateway/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
/node_modules
44
/build
55
/logs
6+
7+
*.log

graphql-kotlin-federation/src/main/kotlin/com/expediagroup/graphql/federation/FederatedSchemaGeneratorHooks.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Expedia, Inc
2+
* Copyright 2020 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@ import kotlin.reflect.full.findAnnotation
5151
* Hooks for generating federated GraphQL schema.
5252
*/
5353
open class FederatedSchemaGeneratorHooks(private val federatedTypeRegistry: FederatedTypeRegistry) : SchemaGeneratorHooks {
54-
private val directiveDefinitionRegex = "(^\".+\"$[\\r\\n])?^directive @\\w+.+\$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
54+
private val directiveDefinitionRegex = "(^\".+\"$[\\r\\n])?^directive @\\w+\\(.+\\) on .+?\$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE, RegexOption.DOT_MATCHES_ALL))
5555
private val scalarDefinitionRegex = "(^\".+\"$[\\r\\n])?^scalar (_FieldSet|_Any)$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
5656
private val emptyQueryRegex = "^type Query \\{$\\s+^\\}$\\s+".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
5757
private val validator = FederatedSchemaValidator()

graphql-kotlin-federation/src/test/kotlin/com/expediagroup/graphql/federation/FederatedSchemaGeneratorTest.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ directive @extends on OBJECT | INTERFACE
5353
"Specifies the base type field set that will be selectable by the gateway"
5454
directive @provides(fields: _FieldSet!) on FIELD_DEFINITION
5555
56-
"Marks the target field/enum value as deprecated"
57-
directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ENUM_VALUE
58-
5956
directive @custom on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION | ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
6057
6158
"Space separated list of primary keys needed to access federated object"
@@ -64,6 +61,12 @@ directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
6461
"Specifies required input field set from the base type for a resolver"
6562
directive @requires(fields: _FieldSet!) on FIELD_DEFINITION
6663
64+
"Marks the field or enum value as deprecated"
65+
directive @deprecated(
66+
"The reason for the deprecation"
67+
reason: String! = "No longer supported"
68+
) on FIELD_DEFINITION | ENUM_VALUE
69+
6770
interface Product @extends @key(fields : "id") {
6871
id: String! @external
6972
reviews: [Review!]!
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Expedia, Inc
2+
* Copyright 2020 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,28 +16,14 @@
1616

1717
package com.expediagroup.graphql.directives
1818

19-
import graphql.Scalars
20-
import graphql.introspection.Introspection
21-
import graphql.schema.GraphQLArgument
19+
import graphql.Directives.DeprecatedDirective
2220
import graphql.schema.GraphQLDirective
2321

2422
const val DEPRECATED_DIRECTIVE_NAME = "deprecated"
2523

26-
private val DefaultDeprecatedArgument: GraphQLArgument = GraphQLArgument.newArgument()
27-
.name("reason")
28-
.type(Scalars.GraphQLString)
29-
.defaultValue("No longer supported")
30-
.build()
31-
32-
internal val DeprecatedDirective: GraphQLDirective = GraphQLDirective.newDirective()
33-
.name(DEPRECATED_DIRECTIVE_NAME)
34-
.description("Marks the target field/enum value as deprecated")
35-
.argument(DefaultDeprecatedArgument)
36-
.validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION, Introspection.DirectiveLocation.ENUM_VALUE)
37-
.build()
38-
3924
internal fun deprecatedDirectiveWithReason(reason: String): GraphQLDirective = DeprecatedDirective.transform { directive ->
40-
directive.argument(DefaultDeprecatedArgument.transform { arg ->
41-
arg.value(reason)
42-
})
25+
val deprecatedArgument = DeprecatedDirective.getArgument("reason").transform { argument ->
26+
argument.value(reason)
27+
}
28+
directive.argument(deprecatedArgument)
4329
}

graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/SchemaGenerator.kt

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Expedia, Inc
2+
* Copyright 2020 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,14 +18,12 @@ package com.expediagroup.graphql.generator
1818

1919
import com.expediagroup.graphql.SchemaGeneratorConfig
2020
import com.expediagroup.graphql.TopLevelObject
21-
import com.expediagroup.graphql.directives.DeprecatedDirective
2221
import com.expediagroup.graphql.generator.state.ClassScanner
2322
import com.expediagroup.graphql.generator.state.TypesCache
2423
import com.expediagroup.graphql.generator.types.generateGraphQLType
2524
import com.expediagroup.graphql.generator.types.generateMutations
2625
import com.expediagroup.graphql.generator.types.generateQueries
2726
import com.expediagroup.graphql.generator.types.generateSubscriptions
28-
import graphql.Directives
2927
import graphql.schema.GraphQLCodeRegistry
3028
import graphql.schema.GraphQLDirective
3129
import graphql.schema.GraphQLSchema
@@ -49,18 +47,6 @@ open class SchemaGenerator(internal val config: SchemaGeneratorConfig) {
4947
internal val additionalTypes = mutableSetOf<KType>()
5048
internal val directives = ConcurrentHashMap<String, GraphQLDirective>()
5149

52-
init {
53-
// NOTE: @include and @defer query directives are added by graphql-java by default
54-
// adding them explicitly here to keep it consistent with missing deprecated directive
55-
directives[Directives.IncludeDirective.name] = Directives.IncludeDirective
56-
directives[Directives.SkipDirective.name] = Directives.SkipDirective
57-
58-
// graphql-kotlin default directives
59-
// @deprecated directive is a built-in directive that each GraphQL server should provide bu currently it is not added by graphql-java
60-
// see https://github.com/graphql-java/graphql-java/issues/1598
61-
directives[DeprecatedDirective.name] = DeprecatedDirective
62-
}
63-
6450
/**
6551
* Generate a schema given a list of objects to parse for the queries, mutations, and subscriptions.
6652
*/

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/extensions/GraphQLSchemaExtensionsTest.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Expedia, Inc
2+
* Copyright 2020 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -269,11 +269,14 @@ class GraphQLSchemaExtensionsTest {
269269
if: Boolean!
270270
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
271271
272-
"Marks the target field/enum value as deprecated"
273-
directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ENUM_VALUE
274-
275272
directive @customDirective on FIELD_DEFINITION
276273
274+
"Marks the field or enum value as deprecated"
275+
directive @deprecated(
276+
"The reason for the deprecation"
277+
reason: String! = "No longer supported"
278+
) on FIELD_DEFINITION | ENUM_VALUE
279+
277280
type ClassWithDirective {
278281
msg: String! @customDirective
279282
}

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/types/GenerateDirectiveTest.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Expedia, Inc
2+
* Copyright 2020 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,12 +18,10 @@ package com.expediagroup.graphql.generator.types
1818

1919
import com.expediagroup.graphql.annotations.GraphQLDescription
2020
import com.expediagroup.graphql.annotations.GraphQLDirective
21-
import com.expediagroup.graphql.directives.DeprecatedDirective
2221
import com.expediagroup.graphql.generator.SchemaGenerator
2322
import com.expediagroup.graphql.generator.extensions.isTrue
2423
import com.expediagroup.graphql.getTestSchemaConfigWithMockedDirectives
2524
import com.expediagroup.graphql.test.utils.SimpleDirective
26-
import graphql.Directives
2725
import org.junit.jupiter.api.BeforeEach
2826
import org.junit.jupiter.api.Test
2927
import kotlin.reflect.KClass
@@ -119,10 +117,6 @@ internal class GenerateDirectiveTest {
119117
@Test
120118
fun `directives are only added to the schema once`() {
121119
val initialCount = basicGenerator.directives.size
122-
assertTrue(basicGenerator.directives.containsKey(Directives.IncludeDirective.name))
123-
assertTrue(basicGenerator.directives.containsKey(Directives.SkipDirective.name))
124-
assertTrue(basicGenerator.directives.containsKey(DeprecatedDirective.name))
125-
126120
val firstInvocation = generateDirectives(basicGenerator, MyClass::simpleDirective)
127121
assertEquals(1, firstInvocation.size)
128122
val secondInvocation = generateDirectives(basicGenerator, MyClass::simpleDirective)

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/routes/RouteConfigurationIT.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Expedia, Inc
2+
* Copyright 2020 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,8 +80,11 @@ class RouteConfigurationIT(@Autowired private val testClient: WebTestClient) {
8080
if: Boolean!
8181
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
8282
83-
"Marks the target field/enum value as deprecated"
84-
directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ENUM_VALUE
83+
"Marks the field or enum value as deprecated"
84+
directive @deprecated(
85+
"The reason for the deprecation"
86+
reason: String! = "No longer supported"
87+
) on FIELD_DEFINITION | ENUM_VALUE
8588
8689
type Query {
8790
context: String!

0 commit comments

Comments
 (0)