forked from graph-gophers/graphql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Panic on Introspection of Schema using ToJSON
Introspection via ToJSON works differently when used via the `Schema.ToJSON` function than when making an introspection query from a client such as GraphiQL. In the later case, introspection uses the Schema's registered resolver, while in the former case, a `resolvable.Schema` is faked Without the `Meta` being set on this `resolvable.Schema` (following the recent changes to address race conditions around the Meta schema), this resulted in a panic when using this form of schema introspection.
- Loading branch information
Showing
9 changed files
with
3,530 additions
and
9 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,89 @@ | ||
package graphql_test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/graph-gophers/graphql-go" | ||
"github.com/graph-gophers/graphql-go/example/social" | ||
"github.com/graph-gophers/graphql-go/example/starwars" | ||
) | ||
|
||
func TestSchema_ToJSON(t *testing.T) { | ||
t.Parallel() | ||
|
||
type args struct { | ||
Schema *graphql.Schema | ||
} | ||
type want struct { | ||
JSON []byte | ||
} | ||
testTable := []struct { | ||
Name string | ||
Args args | ||
Want want | ||
}{ | ||
{ | ||
Name: "Social Schema", | ||
Args: args{Schema: graphql.MustParseSchema(social.Schema, &social.Resolver{}, graphql.UseFieldResolvers())}, | ||
Want: want{JSON: mustReadFile("example/social/introspect.json")}, | ||
}, | ||
{ | ||
Name: "Star Wars Schema", | ||
Args: args{Schema: graphql.MustParseSchema(starwars.Schema, &starwars.Resolver{})}, | ||
Want: want{JSON: mustReadFile("example/starwars/introspect.json")}, | ||
}, | ||
{ | ||
Name: "Star Wars Schema without Resolver", | ||
Args: args{Schema: graphql.MustParseSchema(starwars.Schema, nil)}, | ||
Want: want{JSON: mustReadFile("example/starwars/introspect.json")}, | ||
}, | ||
} | ||
|
||
for _, tt := range testTable { | ||
t.Run(tt.Name, func(t *testing.T) { | ||
j, err := tt.Args.Schema.ToJSON() | ||
if err != nil { | ||
t.Fatalf("invalid schema %s", err.Error()) | ||
} | ||
|
||
// Verify JSON to avoid red herring errors. | ||
got, err := formatJSON(j) | ||
if err != nil { | ||
t.Fatalf("got: invalid JSON: %s", err) | ||
} | ||
want, err := formatJSON(tt.Want.JSON) | ||
if err != nil { | ||
t.Fatalf("want: invalid JSON: %s", err) | ||
} | ||
|
||
if !bytes.Equal(got, want) { | ||
t.Logf("got: %s", got) | ||
t.Logf("want: %s", want) | ||
t.Fail() | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func formatJSON(data []byte) ([]byte, error) { | ||
var v interface{} | ||
if err := json.Unmarshal(data, &v); err != nil { | ||
return nil, err | ||
} | ||
formatted, err := json.Marshal(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return formatted, nil | ||
} | ||
|
||
func mustReadFile(filename string) []byte { | ||
b, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return b | ||
} |
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