strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
4.02k stars 533 forks source link

Schema directives not included in introspection #3649

Closed jacobmoshipco closed 1 month ago

jacobmoshipco commented 1 month ago

Describe the Bug

When using introspection, Strawberry is not returning any custom schema directives. Looking into it, I found a partial cause and a workaround.

I created a repository to show the issue. If you run main.py, it will run eight tests:

The last four tests should all pass, they show a work-around of passing the directives to Schema(..., types=[...]). Note that this doesn't work if passed to schema_directives=[...] instead.

There is also no automatic collection of directives when generating introspection. Leaving the types off Schema entirely works when running schema export, but not when running introspection.

What I personally consider to be "the bug"

  1. Types passed to Schema via schema_directives aren't being added to the graphql_directives list, but they probably should be
  2. Automatic collection of schema directives works in export-schema but not in introspection.

System Information

Additional Context

I believe it makes the most sense to pass the schema directives to Schema via schema_directives. Currently, Schema is looking for them in types and using compat.is_schema_directive to check if they're schema directives. By modifying Schema to simply iterate over schema_directives and running the same code, I was able to get that portion working exactly as I expected. This probably solves part one of the bug, but I haven't had time to run the unit tests against this yet.

No idea why part two is happening.

Upvote & Fund

Fund with Polar

patrick91 commented 1 month ago

Types passed to Schema via schema_directives aren't being added to the graphql_directives list, but they probably should be

Schema directives won't show in the introspection because that's not supported by the spec 😊 see: https://github.com/graphql/graphql-spec/issues/300

What are you trying to do, why do you need schema directives in introspection? Maybe you need operation directives?

jacobmoshipco commented 1 month ago

I didn't notice that it wasn't part of the spec. It appears I confused schema directives and operation directives when experimenting with work-arounds.

I was trying to use the schema directives for metadata for codegen (particularly oneOf). I had misremembered using the introspection feature to do just that on an earlier project, but looking at it, it appears I was using export-schema and using that for the codegen. Is that the best approach?

jacobmoshipco commented 1 month ago

Okay, using export-schema to export an SDL to be served by my API works perfectly. I'll be closing this issue; feel free to reopen if anyone needs more from it. Thanks for your help!