-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explicit support for deprecated directive in the schema (#247)
* Explicit support for deprecated directive in the schema * custom SDL printer * customize schema print extension, fix unit tests and detekt * add printer tests * revert changes to GraphQLName
- Loading branch information
1 parent
1d00b99
commit 0d6bee7
Showing
14 changed files
with
378 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/com/expedia/graphql/directives/DeprecatedDirective.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.expedia.graphql.directives | ||
|
||
import graphql.Scalars | ||
import graphql.introspection.Introspection | ||
import graphql.schema.GraphQLArgument | ||
import graphql.schema.GraphQLDirective | ||
import graphql.schema.GraphQLNonNull | ||
|
||
const val DEPRECATED_DIRECTIVE_NAME = "deprecated" | ||
|
||
private val DefaultDeprecatedArgument: GraphQLArgument = GraphQLArgument.newArgument() | ||
.name("reason") | ||
.type(GraphQLNonNull.nonNull(Scalars.GraphQLString)) | ||
.defaultValue("No longer supported") | ||
.build() | ||
|
||
internal val DeprecatedDirective: GraphQLDirective = GraphQLDirective.newDirective() | ||
.name(DEPRECATED_DIRECTIVE_NAME) | ||
.description("Marks the target field/enum value as deprecated") | ||
.argument(DefaultDeprecatedArgument) | ||
.validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION, Introspection.DirectiveLocation.ENUM_VALUE) | ||
.build() | ||
|
||
internal fun deprecatedDirectiveWithReason(reason: String): GraphQLDirective = DeprecatedDirective.transform { directive -> | ||
directive.argument(DefaultDeprecatedArgument.transform { arg -> | ||
arg.value(reason) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/com/expedia/graphql/extensions/GraphQLSchemaExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.expedia.graphql.extensions | ||
|
||
import com.expedia.graphql.directives.DeprecatedDirective | ||
import graphql.Directives | ||
import graphql.schema.GraphQLSchema | ||
import graphql.schema.idl.SchemaPrinter | ||
|
||
/** | ||
* Prints out SDL representation of a target schema. | ||
* | ||
* @param includeIntrospectionTypes boolean flag indicating whether SDL should include introspection types | ||
* @param includeScalarTypes boolean flag indicating whether SDL should include custom schema scalars | ||
* @param includeExtendedScalarTypes boolean flag indicating whether SDL should include extended scalars (e.g. Long) | ||
* supported by graphql-java, if set will automatically also set the includeScalarTypes flag | ||
* @param includeDefaultSchemaDefinition boolean flag indicating whether SDL should include schema definition if using | ||
* default root type names | ||
* @param includeDirectives boolean flag indicating whether SDL should include directive information | ||
*/ | ||
fun GraphQLSchema.print( | ||
includeIntrospectionTypes: Boolean = false, | ||
includeScalarTypes: Boolean = true, | ||
includeExtendedScalarTypes: Boolean = true, | ||
includeDefaultSchemaDefinition: Boolean = true, | ||
includeDirectives: Boolean = true | ||
): String { | ||
val schemaPrinter = SchemaPrinter( | ||
SchemaPrinter.Options.defaultOptions() | ||
.includeIntrospectionTypes(includeIntrospectionTypes) | ||
.includeScalarTypes(includeScalarTypes || includeExtendedScalarTypes) | ||
.includeExtendedScalarTypes(includeExtendedScalarTypes) | ||
.includeSchemaDefintion(includeDefaultSchemaDefinition) | ||
.includeDirectives(includeDirectives) | ||
) | ||
|
||
var schemaString = schemaPrinter.print(this) | ||
if (includeDirectives) { | ||
// graphql-java SchemaPrinter filters out common directives, below is a workaround to print default built-in directives | ||
val defaultDirectives = arrayOf(Directives.IncludeDirective, Directives.SkipDirective, DeprecatedDirective) | ||
val directivesToString = defaultDirectives.joinToString("\n\n") { directive -> """ | ||
#${directive.description} | ||
directive @${directive.name} on ${directive.validLocations().joinToString(" | ") { loc -> loc.name }} | ||
""".trimIndent() | ||
} | ||
schemaString += "\n" + directivesToString | ||
} | ||
return schemaString | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.