Allow compatibility when using introspection derived client schema's (#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 https://github.com/Khan/genqlient/issues/4 how we can use introspection to fetch the schema when using `genqlient`.

Co-authored-by: Ben Kraft <[email protected]>
This commit is contained in:
Florian Suess
2021-10-27 15:27:37 -07:00
committed by GitHub
co-authored by Ben Kraft
parent a4aa6d9bb0
commit e0accbded1
2 changed files with 50 additions and 3 deletions
+13 -2
View File
@@ -12,6 +12,7 @@ import (
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
"github.com/vektah/gqlparser/v2/parser"
"github.com/vektah/gqlparser/v2/validator"
)
@@ -31,9 +32,19 @@ func getSchema(globs StringList) (*ast.Schema, error) {
sources[i] = &ast.Source{Name: filename, Input: string(text)}
}
schema, graphqlError := gqlparser.LoadSchema(sources...)
// Multi step schema validation
// Step 1 assume schema implicitly declares types that are required by the graphql spec
// Step 2 assume schema explicitly declares types that are required by the graphql spec
var (
schema *ast.Schema
graphqlError *gqlerror.Error
)
schema, graphqlError = gqlparser.LoadSchema(sources...)
if graphqlError != nil {
return nil, errorf(nil, "invalid schema: %v", graphqlError)
schema, graphqlError = validator.LoadSchema(sources...)
if graphqlError != nil {
return nil, errorf(nil, "invalid schema: %v", graphqlError)
}
}
return schema, nil