Skip to content

Commit

Permalink
Update generator to build a new ExecutableSchema interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 17, 2018
1 parent c082c3a commit 62a18ff
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 275 deletions.
2 changes: 1 addition & 1 deletion codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (f *Field) ResolverDeclaration() string {

result := f.Signature()
if f.Object.Stream {
result = "chan<-" + result
result = "<-chan " + result
}

res += fmt.Sprintf(") (%s, error)", result)
Expand Down
72 changes: 28 additions & 44 deletions example/dataloader/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package dataloader

import (
context "context"
io "io"
strconv "strconv"
sync "sync"

Expand All @@ -13,7 +12,6 @@ import (
introspection "github.com/vektah/gqlgen/neelance/introspection"
query "github.com/vektah/gqlgen/neelance/query"
schema "github.com/vektah/gqlgen/neelance/schema"
validation "github.com/vektah/gqlgen/neelance/validation"
)

type Resolvers interface {
Expand All @@ -24,49 +22,38 @@ type Resolvers interface {
Query_customers(ctx context.Context) ([]Customer, error)
}

func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[string]interface{}, io.Writer) []*errors.QueryError {
return func(ctx context.Context, document string, operationName string, variables map[string]interface{}, w io.Writer) []*errors.QueryError {
doc, qErr := query.Parse(document)
if qErr != nil {
return []*errors.QueryError{qErr}
}

errs := validation.Validate(parsedSchema, doc)
if len(errs) != 0 {
return errs
}
func MakeExecutableSchema(resolvers Resolvers) graphql.ExecutableSchema {
return &executableSchema{resolvers}
}

op, err := doc.GetOperation(operationName)
if err != nil {
return []*errors.QueryError{errors.Errorf("%s", err)}
}
type executableSchema struct {
resolvers Resolvers
}

c := executionContext{
resolvers: resolvers,
variables: variables,
doc: doc,
ctx: ctx,
}
func (e *executableSchema) Schema() *schema.Schema {
return parsedSchema
}

var data graphql.Marshaler
if op.Type == query.Query {
data = c._query(op.Selections, nil)
} else {
return []*errors.QueryError{errors.Errorf("unsupported operation type")}
}
func (e *executableSchema) Query(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}

c.wg.Wait()
data := ec._query(op.Selections, nil)
ec.wg.Wait()

result := &graphql.OrderedMap{}
result.Add("data", data)
return &graphql.Response{
Data: data,
Errors: ec.Errors,
}
}

if len(c.Errors) > 0 {
result.Add("errors", graphql.MarshalErrors(c.Errors))
}
func (e *executableSchema) Mutation(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {
return &graphql.Response{Errors: []*errors.QueryError{{Message: "mutations are not supported"}}}
}

result.MarshalGQL(w)
return nil
}
func (e *executableSchema) Subscription(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) <-chan *graphql.Response {
events := make(chan *graphql.Response, 1)
events <- &graphql.Response{Errors: []*errors.QueryError{{Message: "subscriptions are not supported"}}}
return events
}

type executionContext struct {
Expand Down Expand Up @@ -294,8 +281,7 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) graph
if tmp, ok := field.Args["name"]; ok {
tmp2, err := graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(err)
continue
panic(err) // todo: fixme
}
arg0 = tmp2
}
Expand Down Expand Up @@ -647,8 +633,7 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ
if tmp, ok := field.Args["includeDeprecated"]; ok {
tmp2, err := graphql.UnmarshalBoolean(tmp)
if err != nil {
ec.Error(err)
continue
panic(err) // todo: fixme
}
arg0 = tmp2
}
Expand Down Expand Up @@ -701,8 +686,7 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ
if tmp, ok := field.Args["includeDeprecated"]; ok {
tmp2, err := graphql.UnmarshalBoolean(tmp)
if err != nil {
ec.Error(err)
continue
panic(err) // todo: fixme
}
arg0 = tmp2
}
Expand Down
2 changes: 1 addition & 1 deletion example/dataloader/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func main() {
http.Handle("/", handler.GraphiQL("Dataloader", "/query"))
http.Handle("/", handler.Playground("Dataloader", "/query"))

http.Handle("/query", dataloader.LoaderMiddleware(handler.GraphQL(dataloader.NewExecutor(&dataloader.Resolver{}))))

Expand Down
78 changes: 30 additions & 48 deletions example/scalars/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package scalars

import (
context "context"
io "io"
strconv "strconv"
sync "sync"

Expand All @@ -13,57 +12,45 @@ import (
introspection "github.com/vektah/gqlgen/neelance/introspection"
query "github.com/vektah/gqlgen/neelance/query"
schema "github.com/vektah/gqlgen/neelance/schema"
validation "github.com/vektah/gqlgen/neelance/validation"
)

type Resolvers interface {
Query_user(ctx context.Context, id string) (*User, error)
Query_search(ctx context.Context, input SearchArgs) ([]User, error)
}

func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[string]interface{}, io.Writer) []*errors.QueryError {
return func(ctx context.Context, document string, operationName string, variables map[string]interface{}, w io.Writer) []*errors.QueryError {
doc, qErr := query.Parse(document)
if qErr != nil {
return []*errors.QueryError{qErr}
}

errs := validation.Validate(parsedSchema, doc)
if len(errs) != 0 {
return errs
}
func MakeExecutableSchema(resolvers Resolvers) graphql.ExecutableSchema {
return &executableSchema{resolvers}
}

op, err := doc.GetOperation(operationName)
if err != nil {
return []*errors.QueryError{errors.Errorf("%s", err)}
}
type executableSchema struct {
resolvers Resolvers
}

c := executionContext{
resolvers: resolvers,
variables: variables,
doc: doc,
ctx: ctx,
}
func (e *executableSchema) Schema() *schema.Schema {
return parsedSchema
}

var data graphql.Marshaler
if op.Type == query.Query {
data = c._query(op.Selections, nil)
} else {
return []*errors.QueryError{errors.Errorf("unsupported operation type")}
}
func (e *executableSchema) Query(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}

c.wg.Wait()
data := ec._query(op.Selections, nil)
ec.wg.Wait()

result := &graphql.OrderedMap{}
result.Add("data", data)
return &graphql.Response{
Data: data,
Errors: ec.Errors,
}
}

if len(c.Errors) > 0 {
result.Add("errors", graphql.MarshalErrors(c.Errors))
}
func (e *executableSchema) Mutation(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {
return &graphql.Response{Errors: []*errors.QueryError{{Message: "mutations are not supported"}}}
}

result.MarshalGQL(w)
return nil
}
func (e *executableSchema) Subscription(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) <-chan *graphql.Response {
events := make(chan *graphql.Response, 1)
events <- &graphql.Response{Errors: []*errors.QueryError{{Message: "subscriptions are not supported"}}}
return events
}

type executionContext struct {
Expand Down Expand Up @@ -93,8 +80,7 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) graph
if tmp, ok := field.Args["id"]; ok {
tmp2, err := graphql.UnmarshalID(tmp)
if err != nil {
ec.Error(err)
continue
panic(err) // todo: fixme
}
arg0 = tmp2
}
Expand All @@ -118,8 +104,7 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) graph
if tmp, ok := field.Args["input"]; ok {
tmp2, err := UnmarshalSearchArgs(tmp)
if err != nil {
ec.Error(err)
continue
panic(err) // todo: fixme
}
arg0 = tmp2
}
Expand Down Expand Up @@ -153,8 +138,7 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) graph
if tmp, ok := field.Args["name"]; ok {
tmp2, err := graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(err)
continue
panic(err) // todo: fixme
}
arg0 = tmp2
}
Expand Down Expand Up @@ -543,8 +527,7 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ
if tmp, ok := field.Args["includeDeprecated"]; ok {
tmp2, err := graphql.UnmarshalBoolean(tmp)
if err != nil {
ec.Error(err)
continue
panic(err) // todo: fixme
}
arg0 = tmp2
}
Expand Down Expand Up @@ -597,8 +580,7 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ
if tmp, ok := field.Args["includeDeprecated"]; ok {
tmp2, err := graphql.UnmarshalBoolean(tmp)
if err != nil {
ec.Error(err)
continue
panic(err) // todo: fixme
}
arg0 = tmp2
}
Expand Down
2 changes: 1 addition & 1 deletion example/scalars/scalar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type RawUser struct {
}

func TestScalars(t *testing.T) {
srv := httptest.NewServer(handler.GraphQL(NewExecutor(&Resolver{})))
srv := httptest.NewServer(handler.GraphQL(MakeExecutableSchema(&Resolver{})))
c := client.New(srv.URL)

var resp struct {
Expand Down
4 changes: 2 additions & 2 deletions example/scalars/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func main() {
http.Handle("/", handler.GraphiQL("Starwars", "/query"))
http.Handle("/query", handler.GraphQL(scalars.NewExecutor(&scalars.Resolver{})))
http.Handle("/", handler.Playground("Starwars", "/query"))
http.Handle("/query", handler.GraphQL(scalars.MakeExecutableSchema(&scalars.Resolver{})))

log.Fatal(http.ListenAndServe(":8084", nil))
}
Loading

0 comments on commit 62a18ff

Please sign in to comment.