Title: Fix @skip definition is added twice to the GraphQLSchema when defined in sdl by tinnou · Pull Request #2940 · graphql-java/graphql-java · GitHub
Open Graph Title: Fix @skip definition is added twice to the GraphQLSchema when defined in sdl by tinnou · Pull Request #2940 · graphql-java/graphql-java
X Title: Fix @skip definition is added twice to the GraphQLSchema when defined in sdl by tinnou · Pull Request #2940 · graphql-java/graphql-java
Description: Description When @skip/@include is defined in a SDL, schema generation will create a GraphQLSchema with multiple skip/include definitions. This can cause problems when a schema is serialized and parsed multiple times. A little far fetched example: @Test fun parse() { val schema: GraphQLSchema = """ type Query { hello: String } """.trimIndent().mockSchema() val sdl = schema.printSdl() val schema2 = sdl.mockSchema() val sdl2 = schema2.printSdl() sdl2.mockSchema() // throws } fun GraphQLSchema.printSdl(): String { return SchemaPrinter(SchemaPrinter.Options.defaultOptions() .includeDirectiveDefinitions(true) ).print(this) } will throw: Caused by: SchemaProblem{errors=['include' type [@13:1] tried to redefine existing directive 'include' type [@7:1], 'skip' type [@25:1] tried to redefine existing directive 'skip' type [@19:1]]} at graphql.schema.idl.SchemaParser.buildRegistry(SchemaParser.java:156) at graphql.schema.idl.SchemaParser.parseImpl(SchemaParser.java:124) at graphql.schema.idl.SchemaParser.parse(SchemaParser.java:90) at graphql.schema.idl.SchemaParser.parse(SchemaParser.java:75) We could add a predicate to avoid printing directives skip and include but it would sit better if we could ensure printing the sdl generates a valid SDL (that can be parsed back into a valid GraphQLSchema). The actual issue comes from the fact @skip and @include are added by default on the GraphQLSchema builder. Expected Behavior To preserve the behavior of being able to redefine the skip and include directives without raising an error, we should probably just ignore any directive definition named skip or include and rely on the added by default definitions provided by the GraphQL library implementation. I'm on the fence as to add validation to check if a provided skip/include definition is semantically equivalent to the ones defined by the server. Since these are guaranteed to be provided by the GraphQL server implementation, I don't feel this is really needed and it will likely be an artifact of a tool having printed them in the SDL. Changes The fix consists of skipping any directive defined in the sdl with the name skip/include with the expectation graphql-java will use its own. Testing The following test fails and show that redefining the skip/include directive leads to multiple definitions added to the GraphQLSchema instance. def "skip and include should be added to the schema only if not already defined"() { def sdl = ''' "Directs the executor to skip this field or fragment when the `if`'argument is true." directive @skip( "Skipped when true." if: Boolean! ) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT "Directs the executor to include this field or fragment only when the `if` argument is true" directive @include( "Included when true." if: Boolean! ) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT type Query { hello: String } ''' when: def schema = TestUtil.schema(sdl) then: schema.getDirectives().findAll { it.name == "skip" }.size() == 1 schema.getDirectives().findAll { it.name == "include" }.size() == 1 and: def newSchema = GraphQLSchema.newSchema(schema).build() then: newSchema.getDirectives().findAll { it.name == "skip" }.size() == 1 newSchema.getDirectives().findAll { it.name == "include" }.size() == 1 }
Open Graph Description: Description When @skip/@include is defined in a SDL, schema generation will create a GraphQLSchema with multiple skip/include definitions. This can cause problems when a schema is serialized and pa...
X Description: Description When @skip/@include is defined in a SDL, schema generation will create a GraphQLSchema with multiple skip/include definitions. This can cause problems when a schema is serialized and pa...
Opengraph URL: https://github.com/graphql-java/graphql-java/pull/2940
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:2cbaa915-7ab1-1be8-6201-9bf4a64e488a |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | DB06:3EDED5:1238B6:17CB1F:6A60218C |
| html-safe-nonce | 5788416aff5e935fb87daa123f1ebedc7aed468424c836b0fa58578855134a97 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjA2OjNFREVENToxMjM4QjY6MTdDQjFGOjZBNjAyMThDIiwidmlzaXRvcl9pZCI6IjYxNTc1NTMwNTY4MjI5MjYwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | fb0db26ad54002a1283c58e947b66657b3acd9cce8b7521ac72190f2ccea6d2f |
| hovercard-subject-tag | pull_request:1044397290 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/graphql-java/graphql-java/pull/2940/files |
| twitter:image | https://avatars.githubusercontent.com/u/1523863?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/1523863?s=400&v=4 |
| og:image:alt | Description When @skip/@include is defined in a SDL, schema generation will create a GraphQLSchema with multiple skip/include definitions. This can cause problems when a schema is serialized and pa... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7e86a727ba4abf81d0638c5ead57286c17d3b2706f57813445c4316d8e848673 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/graphql-java/graphql-java git https://github.com/graphql-java/graphql-java.git |
| octolytics-dimension-user_id | 14289921 |
| octolytics-dimension-user_login | graphql-java |
| octolytics-dimension-repository_id | 38602457 |
| octolytics-dimension-repository_nwo | graphql-java/graphql-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 38602457 |
| octolytics-dimension-repository_network_root_nwo | graphql-java/graphql-java |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f13d8ac548fb76838b3d703c7fe2100887547d0e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width