Khan / genqlient

a truly type-safe Go GraphQL client
MIT License
1.02k stars 99 forks source link

Support for @authenticated directive #332

Closed librairica closed 2 months ago

librairica commented 3 months ago

Is your feature request related to a problem? Please describe. Code generation fails when the graphql schema includes the @authenticated directive. error: invalid schema: Undefined directive authenticated.

Describe the solution you'd like Generation works with this directive out of the box, or can be configured in genqlient.yaml.

Describe alternatives you've considered Currently I remove instances of @authenticated from the schema using sed: sed 's/@authenticated//' schema.graphql > schemaWithoutAuthenticated.graphql

Additional context Doc on authenticated directive from Apollo: https://www.apollographql.com/docs/router/configuration/authorization/#authenticated

benjaminjkraft commented 2 months ago

Hmm, I think the server/schema was supposed to provide the directive definition if it refers to the directive. (Spec language is here.) If it doesn't, you could probably just add the definition to your schema:

directive @authenticated on OBJECT | FIELD_DEFINITION | INTERFACE | SCALAR | ENUM

I'm curious though how you're getting a schema with undefined directives. Reading the docs, maybe this is something where apollo has their own @link spec that defines a different way to define directives? But I think that should only be if you're a subgraph where your client is the federation server. genqlient is a regular client, so it's generally designed to talk to your supergraph schema, which I think should resolve (really, hide) any federation fanciness. It's not necessarily a problem to use it to talk to a subgraph directly, but you'd have to work out how to handle details like this (e.g. patch in the federation definitions).

Or, if you have a concrete suggestion for what we should support (e.g. how to implement support for @link -- it's not even clear to me from reading the spec if there's a way to resolve them to an actual definition without prior knowledge), concrete suggestions are welcome!

librairica commented 2 months ago

Thanks for the feedback! You are right that declaring the directive instead of importing it via @link solved the issue. I was able to compose a supergraph and generate off that 👍