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

fixed loader for using a new logic #15

Merged
merged 2 commits into from
Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 22 additions & 47 deletions internal/gqlgen/gqlgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,29 @@ import (
"github.com/pkg/errors"
)

func TestGitHubSchema(t *testing.T) {
schema := filepath.Join("..", "..", "example", "github", "schema.graphql")
root, err := loader.LoadSchema(schema)
if err != nil {
t.Fatal(err)
}

root2, err := LoadSchema(schema)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(root2, root, cmpopts.IgnoreUnexported(introspection.Schema{})); diff != "" {
t.Fatalf("-want, +got\n%s", diff)
func TestExampleSchema(t *testing.T) {
cases := []string{
"github",
"shopify",
"starwars",
}
}

func TestStarwarsSchema(t *testing.T) {
schema := filepath.Join("..", "..", "example", "starwars", "schema.graphql")
root, err := loader.LoadSchema(schema)
if err != nil {
t.Fatal(err)
}

root2, err := LoadSchema(schema)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(root2, root, cmpopts.IgnoreUnexported(introspection.Schema{})); diff != "" {
t.Fatalf("-want, +got\n%s", diff)
}
}

func TestShopifySchema(t *testing.T) {
schema := filepath.Join("..", "..", "example", "shopify", "schema.graphql")
root, err := loader.LoadSchema(schema)
if err != nil {
t.Fatal(err)
}

root2, err := LoadSchema(schema)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(root2, root, cmpopts.IgnoreUnexported(introspection.Schema{})); diff != "" {
t.Fatalf("-want, +got\n%s", diff)
for _, example := range cases {
t.Run("example "+example, func(t *testing.T) {
schema := filepath.Join("..", "..", "example", example, "schema.graphql")
root, err := loader.LoadSchema(schema)
if err != nil {
t.Fatalf("%+v", err)
}

root2, err := LoadSchema(schema)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(root2, root, cmpopts.IgnoreUnexported(introspection.Schema{})); diff != "" {
t.Fatalf("-want, +got\n%s", diff)
}
})
}
}

Expand Down
66 changes: 55 additions & 11 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"encoding/json"

"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/executor"
"github.com/Code-Hex/gqldoc/internal/gqlgen"
"github.com/Code-Hex/gqldoc/internal/introspection"
"github.com/pkg/errors"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/parser"
"github.com/vektah/gqlparser/v2/validator"
)

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

// Set time for tracing
ctx := graphql.StartOperationTrace(context.Background())

exec := executor.New(es)
oc, err := exec.CreateOperationContext(ctx, &graphql.RawParams{
Query: introspection.Query,
ctx, err := CreateOperationContext(Params{
Schema: es.Schema(),
Query: introspection.Query,
Variables: map[string]interface{}{},
})
// Need to do introspection
oc.DisableIntrospection = false
if err != nil {
return nil, errors.WithStack(err)
}

responses, ctx := exec.DispatchOperation(ctx, oc)
resp := responses(ctx)
resp := es.Exec(ctx)(context.Background())
if len(resp.Errors) > 0 {
return nil, resp.Errors
}
Expand All @@ -39,3 +39,47 @@ func LoadSchema(filenames ...string) (*introspection.Root, error) {
}
return &res, nil
}

type Params struct {
Schema *ast.Schema
Query string
Variables map[string]interface{}
}

func CreateOperationContext(params Params) (context.Context, error) {
doc, err := parseQuery(params.Schema, params.Query)
if err != nil {
return nil, err
}
operation := doc.Operations.ForName("")
if operation == nil {
return nil, errors.New("operation not found")
}
variables, verr := validator.VariableValues(params.Schema, operation, params.Variables)
if verr != nil {
return nil, errors.WithStack(verr)
}

opCtx := &graphql.OperationContext{
RawQuery: params.Query,
Variables: variables,
Doc: doc,
Operation: operation,
}
ctx := graphql.WithOperationContext(context.Background(), opCtx)
return ctx, nil
}

func parseQuery(schema *ast.Schema, query string) (*ast.QueryDocument, error) {
doc, err := parser.ParseQuery(&ast.Source{
Input: query,
})
if err != nil {
return nil, errors.WithStack(err)
}
listErr := validator.Validate(schema, doc)
if len(listErr) != 0 {
return nil, listErr
}
return doc, nil
}