-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
520532e
commit e38a212
Showing
15 changed files
with
143 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
query MyQuery { f g } |
101 changes: 101 additions & 0 deletions
101
generate/testdata/errors/InvalidSchemaWithBuiltins.schema.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
type Query { | ||
f: String | ||
g: Bogus | ||
} | ||
|
||
scalar Int | ||
scalar Float | ||
scalar String | ||
scalar Boolean | ||
scalar ID | ||
|
||
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT | ||
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT | ||
directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | ENUM_VALUE | ||
directive @specifiedBy(url: String!) on SCALAR | ||
|
||
type __Schema { | ||
description: String | ||
types: [__Type!]! | ||
queryType: __Type! | ||
mutationType: __Type | ||
subscriptionType: __Type | ||
directives: [__Directive!]! | ||
} | ||
|
||
type __Type { | ||
kind: __TypeKind! | ||
name: String | ||
description: String | ||
fields(includeDeprecated: Boolean = false): [__Field!] | ||
interfaces: [__Type!] | ||
possibleTypes: [__Type!] | ||
enumValues(includeDeprecated: Boolean = false): [__EnumValue!] | ||
inputFields: [__InputValue!] | ||
ofType: __Type | ||
specifiedByURL: String | ||
} | ||
|
||
type __Field { | ||
name: String! | ||
description: String | ||
args: [__InputValue!]! | ||
type: __Type! | ||
isDeprecated: Boolean! | ||
deprecationReason: String | ||
} | ||
|
||
type __InputValue { | ||
name: String! | ||
description: String | ||
type: __Type! | ||
defaultValue: String | ||
} | ||
|
||
type __EnumValue { | ||
name: String! | ||
description: String | ||
isDeprecated: Boolean! | ||
deprecationReason: String | ||
} | ||
|
||
enum __TypeKind { | ||
SCALAR | ||
OBJECT | ||
INTERFACE | ||
UNION | ||
ENUM | ||
INPUT_OBJECT | ||
LIST | ||
NON_NULL | ||
} | ||
|
||
type __Directive { | ||
name: String! | ||
description: String | ||
locations: [__DirectiveLocation!]! | ||
args: [__InputValue!]! | ||
isRepeatable: Boolean! | ||
} | ||
|
||
enum __DirectiveLocation { | ||
QUERY | ||
MUTATION | ||
SUBSCRIPTION | ||
FIELD | ||
FRAGMENT_DEFINITION | ||
FRAGMENT_SPREAD | ||
INLINE_FRAGMENT | ||
VARIABLE_DEFINITION | ||
SCHEMA | ||
SCALAR | ||
OBJECT | ||
FIELD_DEFINITION | ||
ARGUMENT_DEFINITION | ||
INTERFACE | ||
UNION | ||
ENUM | ||
ENUM_VALUE | ||
INPUT_OBJECT | ||
INPUT_FIELD_DEFINITION | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
query MyQuery { f g } |
4 changes: 4 additions & 0 deletions
4
generate/testdata/errors/InvalidSchemaWithoutBuiltins.schema.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type Query { | ||
f: String | ||
g: Bogus | ||
} |
1 change: 0 additions & 1 deletion
1
generate/testdata/snapshots/TestGenerateErrors-InvalidSchema-go
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
generate/testdata/snapshots/TestGenerateErrors-InvalidSchema-graphql
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
generate/testdata/snapshots/TestGenerateErrors-InvalidSchemaSyntax-go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
testdata/errors/InvalidSchemaSyntax.schema.graphql:4: invalid schema: Expected :, found } |
1 change: 1 addition & 0 deletions
1
generate/testdata/snapshots/TestGenerateErrors-InvalidSchemaSyntax-graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
testdata/errors/InvalidSchemaSyntax.schema.graphql:4: invalid schema: Expected :, found } |
1 change: 1 addition & 0 deletions
1
generate/testdata/snapshots/TestGenerateErrors-InvalidSchemaWithBuiltins-graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
testdata/errors/InvalidSchemaWithBuiltins.schema.graphql:3: invalid schema: Undefined type Bogus. |
1 change: 1 addition & 0 deletions
1
generate/testdata/snapshots/TestGenerateErrors-InvalidSchemaWithoutBuiltins-graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
testdata/errors/InvalidSchemaWithoutBuiltins.schema.graphql:3: invalid schema: Undefined type Bogus. |