ExpediaGroup / graphql-kotlin

Libraries for running GraphQL in Kotlin
https://opensource.expediagroup.com/graphql-kotlin/
Apache License 2.0
1.74k stars 349 forks source link

FederatedSchemaValidator rejects nested union type field set #1939

Open pdambrauskas opened 8 months ago

pdambrauskas commented 8 months ago

Library Version 7.0.2

Describe the bug When I have a union type and want to use it on @requires directive, it fails. When I use:

@RequiresDirective(fields = FieldSet("animal { ... on Dog { breed { name } } }"))

I get something like

com.expediagroup.graphql.generator.federation.exception.InvalidFederatedSchema: Invalid federated schema:
  - @requires(fields = "animal { ... on Dog { breed { name } } }") directive on <...redacted...> specifies invalid field set - field set specifies field that does not exist, field=...
  - @requires(fields = "animal { ... on Dog { breed { name } } }") directive on <...redacted...> specifies invalid field set - field set specifies field that does not exist, field=on
  - @requires(fields = "animal { ... on Dog { breed { name } } }") directive on <...redacted...> specifies invalid field set - field set specifies field that does not exist, field=Dog

However, if I do not add the annotation & just edit federated schema, or remove validations by overriding didGenerateGraphQLType on FederatedSchemaGeneratorHooks. Subgraph is federated and works as expected

Expected behaviour support for this kind of directives, without validation errors.

Also, would it make sense to make validator: FederatedSchemaValidator injectable on FederatedSchemaGeneratorHooks and open for overrides so that there was an easy way to disable validation in this kind of edge-case scenarios?

Apollo mentions similar usage pattern on Provides directive documentation: https://www.apollographql.com/docs/federation/federated-types/federated-directives/#fields-1

dariuszkuc commented 8 months ago

Hello đŸ‘‹ Indeed this is a bug. Current fieldset validation logic is pretty crude and doesn't work with polymorphic types. Technically FieldSet can be any valid selection set so this validation should either be updated or removed altogether to avoid those issues.