Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow compatibility when using introspection derived client schema's #145

Merged
merged 3 commits into from
Oct 27, 2021

Conversation

suessflorian
Copy link
Contributor

@suessflorian suessflorian commented Oct 23, 2021

During generation we parse the schema using gqlparser.LoadSchema over here. As you can see gqlparser.LoadSchema uses it's sub directory declared validator.LoadSchema - but prepends a schema with typical implicit declared types. Full server schema introspection exposes the entire schema explicitly, hence causing a clash with this prelude schema rendering introspection derived schemas to fail.

Here I just introduce a two stage schema parsing step to accomodate both implicit and explicit sdl's.

--

I have also added to the FAQ as per #4 how we can use introspection to fetch the schema when using genqlient.

When using introspection based schema generators, the server schema is strictly explicit causing a clash with `gqlparser.LoadSchema` due to it's schema prelude source accommodating implicitly defined schema's.
Copy link
Member

@csilvers csilvers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me! But I'd like to wait a few days to give @benjaminjkraft a chance to take a look.

@csilvers
Copy link
Member

csilvers commented Oct 24, 2021

Also, if you haven't already, can you sign the CLA? https://www.khanacademy.org/r/cla

@suessflorian
Copy link
Contributor Author

@csilvers, thanks! Also, CLA signed 👍

@benjaminjkraft
Copy link
Collaborator

This looks perfect, thanks!

@benjaminjkraft benjaminjkraft merged commit e0accbd into Khan:main Oct 27, 2021
benjaminjkraft added a commit that referenced this pull request Jun 6, 2022
GraphQL schemas have some builtin types, like `String`. The spec says
your SDL must not include those, but in practice some schemas do. (This
is probably because introspection must include them, and some tools that
create SDL from introspection don't know they're supposed to filter them
out.) Anyway, we've since #145 had logic to handle this; we just parse
with and without the prelude that defines them and see which works.

The problem is that this makes for very confusing error messages if you
have an invalid schema. (Or if you have a schema that you think is valid
but gqlparser doesn't, which is the more common case in the wild; see
for example #200.) Right now if both ways error we take the
without-prelude error, which if you didn't define the builtins is just
`undefined type String`; if we took the with-prelude error then if you
did define the builtins you'd just get `type String defined twice`. So
we actually have to be smart if we want good error messages for
everyone.

So in this commit we are smart: we check if your schema defines
`String`, and include the prelude only if it does not. To do this I
basically inlined `gqlparser.LoadSchema` (twice), so that in between
parsing and validation we can check if you have `String` and if not add
the prelude. This should in theory be both more efficient
(we don't have to do everything twice) and give better error messages,
although it's a bit more code.

Test plan: make check
benjaminjkraft added a commit that referenced this pull request Jun 6, 2022
GraphQL schemas have some builtin types, like `String`. The spec says
your SDL must not include those, but in practice some schemas do. (This
is probably because introspection must include them, and some tools that
create SDL from introspection don't know they're supposed to filter them
out.) Anyway, we've since #145 had logic to handle this; we just parse
with and without the prelude that defines them and see which works.

The problem is that this makes for very confusing error messages if you
have an invalid schema. (Or if you have a schema that you think is valid
but gqlparser doesn't, which is the more common case in the wild; see
for example #200.) Right now if both ways error we take the
without-prelude error, which if you didn't define the builtins is just
`undefined type String`; if we took the with-prelude error then if you
did define the builtins you'd just get `type String defined twice`. So
we actually have to be smart if we want good error messages for
everyone.

So in this commit we are smart: we check if your schema defines
`String`, and include the prelude only if it does not. To do this I
basically inlined `gqlparser.LoadSchema` (twice), so that in between
parsing and validation we can check if you have `String` and if not add
the prelude. This should in theory be both more efficient (we don't
have to do everything twice) and give better error messages,
although it's a bit more code.

Fixes #175.

Test plan: make check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants