Skip to content
This repository was archived by the owner on Apr 1, 2024. It is now read-only.

Commit 3aebf8d

Browse files
authored
Merge pull request #15 from Code-Hex/fix/loader
fixed loader for using a new logic
2 parents c65df78 + 9085656 commit 3aebf8d

File tree

2 files changed

+77
-58
lines changed

2 files changed

+77
-58
lines changed

internal/gqlgen/gqlgen_test.go

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,29 @@ import (
1616
"github.com/pkg/errors"
1717
)
1818

19-
func TestGitHubSchema(t *testing.T) {
20-
schema := filepath.Join("..", "..", "example", "github", "schema.graphql")
21-
root, err := loader.LoadSchema(schema)
22-
if err != nil {
23-
t.Fatal(err)
24-
}
25-
26-
root2, err := LoadSchema(schema)
27-
if err != nil {
28-
t.Fatal(err)
29-
}
30-
31-
if diff := cmp.Diff(root2, root, cmpopts.IgnoreUnexported(introspection.Schema{})); diff != "" {
32-
t.Fatalf("-want, +got\n%s", diff)
19+
func TestExampleSchema(t *testing.T) {
20+
cases := []string{
21+
"github",
22+
"shopify",
23+
"starwars",
3324
}
34-
}
35-
36-
func TestStarwarsSchema(t *testing.T) {
37-
schema := filepath.Join("..", "..", "example", "starwars", "schema.graphql")
38-
root, err := loader.LoadSchema(schema)
39-
if err != nil {
40-
t.Fatal(err)
41-
}
42-
43-
root2, err := LoadSchema(schema)
44-
if err != nil {
45-
t.Fatal(err)
46-
}
47-
48-
if diff := cmp.Diff(root2, root, cmpopts.IgnoreUnexported(introspection.Schema{})); diff != "" {
49-
t.Fatalf("-want, +got\n%s", diff)
50-
}
51-
}
52-
53-
func TestShopifySchema(t *testing.T) {
54-
schema := filepath.Join("..", "..", "example", "shopify", "schema.graphql")
55-
root, err := loader.LoadSchema(schema)
56-
if err != nil {
57-
t.Fatal(err)
58-
}
59-
60-
root2, err := LoadSchema(schema)
61-
if err != nil {
62-
t.Fatal(err)
63-
}
64-
65-
if diff := cmp.Diff(root2, root, cmpopts.IgnoreUnexported(introspection.Schema{})); diff != "" {
66-
t.Fatalf("-want, +got\n%s", diff)
25+
for _, example := range cases {
26+
t.Run("example "+example, func(t *testing.T) {
27+
schema := filepath.Join("..", "..", "example", example, "schema.graphql")
28+
root, err := loader.LoadSchema(schema)
29+
if err != nil {
30+
t.Fatalf("%+v", err)
31+
}
32+
33+
root2, err := LoadSchema(schema)
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
38+
if diff := cmp.Diff(root2, root, cmpopts.IgnoreUnexported(introspection.Schema{})); diff != "" {
39+
t.Fatalf("-want, +got\n%s", diff)
40+
}
41+
})
6742
}
6843
}
6944

loader/loader.go

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55
"encoding/json"
66

77
"github.com/99designs/gqlgen/graphql"
8-
"github.com/99designs/gqlgen/graphql/executor"
98
"github.com/Code-Hex/gqldoc/internal/gqlgen"
109
"github.com/Code-Hex/gqldoc/internal/introspection"
1110
"github.com/pkg/errors"
11+
"github.com/vektah/gqlparser/v2/ast"
12+
"github.com/vektah/gqlparser/v2/parser"
13+
"github.com/vektah/gqlparser/v2/validator"
1214
)
1315

1416
func LoadSchema(filenames ...string) (*introspection.Root, error) {
@@ -17,18 +19,16 @@ func LoadSchema(filenames ...string) (*introspection.Root, error) {
1719
return nil, errors.WithStack(err)
1820
}
1921

20-
// Set time for tracing
21-
ctx := graphql.StartOperationTrace(context.Background())
22-
23-
exec := executor.New(es)
24-
oc, err := exec.CreateOperationContext(ctx, &graphql.RawParams{
25-
Query: introspection.Query,
22+
ctx, err := CreateOperationContext(Params{
23+
Schema: es.Schema(),
24+
Query: introspection.Query,
25+
Variables: map[string]interface{}{},
2626
})
27-
// Need to do introspection
28-
oc.DisableIntrospection = false
27+
if err != nil {
28+
return nil, errors.WithStack(err)
29+
}
2930

30-
responses, ctx := exec.DispatchOperation(ctx, oc)
31-
resp := responses(ctx)
31+
resp := es.Exec(ctx)(context.Background())
3232
if len(resp.Errors) > 0 {
3333
return nil, resp.Errors
3434
}
@@ -39,3 +39,47 @@ func LoadSchema(filenames ...string) (*introspection.Root, error) {
3939
}
4040
return &res, nil
4141
}
42+
43+
type Params struct {
44+
Schema *ast.Schema
45+
Query string
46+
Variables map[string]interface{}
47+
}
48+
49+
func CreateOperationContext(params Params) (context.Context, error) {
50+
doc, err := parseQuery(params.Schema, params.Query)
51+
if err != nil {
52+
return nil, err
53+
}
54+
operation := doc.Operations.ForName("")
55+
if operation == nil {
56+
return nil, errors.New("operation not found")
57+
}
58+
variables, verr := validator.VariableValues(params.Schema, operation, params.Variables)
59+
if verr != nil {
60+
return nil, errors.WithStack(verr)
61+
}
62+
63+
opCtx := &graphql.OperationContext{
64+
RawQuery: params.Query,
65+
Variables: variables,
66+
Doc: doc,
67+
Operation: operation,
68+
}
69+
ctx := graphql.WithOperationContext(context.Background(), opCtx)
70+
return ctx, nil
71+
}
72+
73+
func parseQuery(schema *ast.Schema, query string) (*ast.QueryDocument, error) {
74+
doc, err := parser.ParseQuery(&ast.Source{
75+
Input: query,
76+
})
77+
if err != nil {
78+
return nil, errors.WithStack(err)
79+
}
80+
listErr := validator.Validate(schema, doc)
81+
if len(listErr) != 0 {
82+
return nil, listErr
83+
}
84+
return doc, nil
85+
}

0 commit comments

Comments
 (0)