Be more precise in deciding whether to add the schema prelude (#205)

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
This commit is contained in:
Ben Kraft
2022-06-06 16:50:16 -07:00
committed by GitHub
parent 520532eb65
commit e38a212de2
15 changed files with 144 additions and 15 deletions
+32 -13
View File
@@ -10,11 +10,10 @@ import (
"strconv"
"strings"
"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"
_ "github.com/vektah/gqlparser/v2/validator/rules"
)
func getSchema(globs StringList) (*ast.Schema, error) {
@@ -32,21 +31,41 @@ func getSchema(globs StringList) (*ast.Schema, error) {
sources[i] = &ast.Source{Name: filename, Input: string(text)}
}
// 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...)
// Ideally here we'd just call gqlparser.LoadSchema. But the schema we are
// given may or may not contain the builtin types String, Int, etc. (The
// spec says it shouldn't, but introspection will return those types, and
// some introspection-to-SDL tools aren't smart enough to remove them.) So
// we inline LoadSchema and insert some checks.
document, graphqlError := parser.ParseSchemas(sources...)
if graphqlError != nil {
schema, graphqlError = validator.LoadSchema(sources...)
if graphqlError != nil {
return nil, errorf(nil, "invalid schema: %v", graphqlError)
// Schema doesn't even parse.
return nil, errorf(nil, "invalid schema: %v", graphqlError)
}
// Check if we have a builtin type. (String is an arbitrary choice.)
hasBuiltins := false
for _, def := range document.Definitions {
if def.Name == "String" {
hasBuiltins = true
break
}
}
if !hasBuiltins {
// modified from parser.ParseSchemas
var preludeAST *ast.SchemaDocument
preludeAST, graphqlError = parser.ParseSchema(validator.Prelude)
if graphqlError != nil {
return nil, errorf(nil, "invalid prelude (probably a gqlparser bug): %v", graphqlError)
}
document.Merge(preludeAST)
}
schema, graphqlError := validator.ValidateSchemaDocument(document)
if graphqlError != nil {
return nil, errorf(nil, "invalid schema: %v", graphqlError)
}
return schema, nil
}