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 fetching a schema via introspection (instead of an SDL file) #4

Closed
benjaminjkraft opened this issue Apr 22, 2021 · 14 comments
Closed
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Issues that anyone could pick up and implement if useful to them

Comments

@benjaminjkraft
Copy link
Collaborator

benjaminjkraft commented Apr 22, 2021

We'll probably just want to convert it to SDL. There are tools in Node to do that -- see e.g. this gist or this package which users can invoke themselves as a workaround. I don't see any in Go, so we might have to port the relevant parts of graphql-js (see the gist).

@benjaminjkraft benjaminjkraft added the needs use cases Feature we can add if people find use cases that need it (comment if that's you) label Sep 3, 2021
@benjaminjkraft benjaminjkraft added enhancement New feature or request good first issue Good for newcomers help wanted Issues that anyone could pick up and implement if useful to them labels Sep 11, 2021
@AlwxSin
Copy link

AlwxSin commented Oct 11, 2021

I have the use case.
We use schema stitching via https://github.com/nautilus/gateway (like Apollo Federation). The only way to get SDL file is using npm library.

@benjaminjkraft
Copy link
Collaborator Author

@AlwxSin thanks! What kind of auth would you need, if any?

@benjaminjkraft benjaminjkraft removed the needs use cases Feature we can add if people find use cases that need it (comment if that's you) label Oct 11, 2021
@AlwxSin
Copy link

AlwxSin commented Oct 12, 2021

@benjaminjkraft Authorization header

@suessflorian
Copy link
Contributor

@benjaminjkraft, same goes here via https://github.com/movio/bramble, Authorization header plus an arbitrary header (for example X-Co-Tenant) to request the schema for a specific tenant.

@benjaminjkraft
Copy link
Collaborator Author

Ah, interesting. Would you want the custom headers to be in the configuration file, or dynamic (e.g. you'd want to set them via arguments/envvars, differently for different invocations)?

I'm a bit worried that there's no way to support every kind of auth folks will want, although we could also make you call generate.Generate so you can pass your own http.Client or whatnot. Or we could support some simple options like authorization headers via the config file, and do http.Client as a fallback.

@suessflorian
Copy link
Contributor

suessflorian commented Oct 13, 2021

For us a configuration file would be awesome! Ideally in there we would be able to specify an arbitrary set of different HTTP headers.

--

Hey if you're looking for a hand, I see this issue is labelled as "help wanted", I'd be more than keen to help out 👍 love this project idea!

@suessflorian
Copy link
Contributor

suessflorian commented Oct 13, 2021

plus an arbitrary header (for example X-Co-Tenant) to request the schema for a specific tenant.

Hey @benjaminjkraft, just want to provide a correction to my comment earlier - gateway introspection queries in bramble do not react to this tenant header I speak of (thanks to @lucianjon for clarifying this).

Indeed it is only the Authorization header that is required.

@benjaminjkraft
Copy link
Collaborator Author

@suessflorian that would be great!

@suessflorian
Copy link
Contributor

suessflorian commented Oct 14, 2021

Hey @benjaminjkraft, just an FYI if you're interested. After more and more research I think long term the ability to generate a schema from introspection has reason to live in https://github.com/graphql-go/graphql, mirror of https://github.com/graphql/graphql-js, as I see pieces of the puzzle living in this JS reference implementation.

ie, genqlient imports this standard functionality from github.com/graphql-go/graphql.

But this may take a while and I'm really keen to start using genqlient at work sooner rather than later, so keen to see this done in two stages if you agree?

  1. Let genqclient in house offer introspection based schema dsl generation
  2. Then I'll head over and support graphql-go to take this logic in to offload the spec compliant related maintenance of the introspection query.

So far, I've had a little bit of time to get a rough (like really really rough) sketch together here: https://github.com/suessflorian/graphql-server-introspection-sdl-go, only prints schema directives at this stage, will get onto finalising tomorrow and black box testing (re: JS implementation input/output) before opening a PR here with this component available 👍

@benjaminjkraft
Copy link
Collaborator Author

Neat!

Yeah, importing it makes sense, that's a good point. Another option as the temporary (or permanent) solution is to just have it as a separate tool that users can call out to before genqlient:

//go:generate go run command/to/build/schema ...
//go:generate go run github.com/Khan/genqlient

I actually wonder if that's the best way to do it; that way that tool can have its own options, configuration, etc. We could document it as a recommended approach even if separate. But I can also see the convenience of having it wired into genqlient.

Do note we use https://github.com/vektah/gqlparser, not graphql-go. In principle there's no need to match (certainly not if it's a standalone tool) but it's probably cleaner if we do (then we could maybe skip serializing the SDL and then deserializing it, and just generally decrease new deps).

From a quick look, the tool looks quite reasonable :-) .

@suessflorian
Copy link
Contributor

Hey @benjaminjkraft, so I've got a full build client schema functionality tool ready to go: https://github.com/suessflorian/gqlfetch, I've taken your advice and made it standalone first. I've also aligned with https://github.com/vektah/gqlparser hosted gql schema AST. Although I have found how to go directly from introspection to the internal AST, I will need to make some decent extensions to the AST type with some fairly hefty unmarshalling methods.

Benchmarks of this standalone tool at this stage if you're curious; sub 250ms for a 440 type schema (~3800line sdl), interestingly ~98% time spent unmarshalling rest string building.

Where do you think we go from here you reckon (I'm completely impartial)?

  • Have this work moved directly upstream into gqlparser and have genqlient reference this once matured
  • Add to this projects docs somewhere showing introspection based client generation via import of this package
  • Incorporate this into genclient altogether and have this as a package offering.

@benjaminjkraft
Copy link
Collaborator Author

That seems super neat, thanks! If it's too complicated to build the AST directly, it's also fine to go via source.

As for where to host this, I'd lean towards something standalone, as long as the ergonomics aren't too bad, which it seems they wouldn't be. (At least until some potential future where gqlgen has plugins.) But I'm very happy to have it documented with genqlient -- you could add a little example to the FAQ and/or mention it inline with schema in genqlient.yaml.

But let me also pull in @StevenACoffman who is involved with the gqlgen/gqlparser working group, since there's talk of pulling genqlient into the potential forthcoming gqlgen org. If that all happens, it may make sense to pull your project into that org as well. Steve, let me know if you have thoughts on what level of integration makes sense!

@suessflorian
Copy link
Contributor

Sounds good @benjaminjkraft, I'm game for anything and keen to help out. My PR highlights a FAQ portion explaining how it can currently be used, but ofc always keen to amend as needed.

benjaminjkraft added a commit that referenced this issue Oct 27, 2021
…145)

During generation we parse the schema using [`gqlparser.LoadSchema`](https://github.com/vektah/gqlparser/blob/2a3d320c0f1d31f404cc36f6cce8f7f93b016682/gqlparser.go#L11) over [here](https://github.com/Khan/genqlient/blob/a4aa6d9bb0f45cb71b3b7816742172011d96fbc1/generate/parse.go#L34). As you can see `gqlparser.LoadSchema` uses it's sub directory declared [`validator.LoadSchema`](https://github.com/vektah/gqlparser/blob/2a3d320c0f1d31f404cc36f6cce8f7f93b016682/gqlparser.go#L12) - but prepends a schema with [typical implicit declared types](https://github.com/vektah/gqlparser/blob/2a3d320c0f1d31f404cc36f6cce8f7f93b016682/validator/prelude.go#L5). 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`.

Co-authored-by: Ben Kraft <[email protected]>
@StevenACoffman
Copy link
Member

Hey, btw, I brought this up with the other working group members, and we're going to decide at the next monthly meeting. We need to freshen up https://github.com/99designs/gqlgen-contrib as well.
Would gqlgen be an acceptable umbrella org name? A lot of the other graphl/gql/go combinations are already taken.

benjaminjkraft pushed a commit that referenced this issue Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Issues that anyone could pick up and implement if useful to them
Projects
None yet
Development

No branches or pull requests

4 participants