diff --git a/gettingstarted/graph/generated.go b/gettingstarted/generated.go similarity index 67% rename from gettingstarted/graph/generated.go rename to gettingstarted/generated.go index 8051d86..4b71736 100644 --- a/gettingstarted/graph/generated.go +++ b/gettingstarted/generated.go @@ -1,34 +1,29 @@ // Code generated by github.com/vektah/gqlgen, DO NOT EDIT. -package graph +package gettingstarted import ( "bytes" context "context" strconv "strconv" - model "github.com/vektah/gqlgen-tutorials/gettingstarted/model" graphql "github.com/vektah/gqlgen/graphql" - introspection "github.com/vektah/gqlgen/neelance/introspection" - query "github.com/vektah/gqlgen/neelance/query" - schema "github.com/vektah/gqlgen/neelance/schema" + introspection "github.com/vektah/gqlgen/graphql/introspection" + gqlparser "github.com/vektah/gqlparser" + ast "github.com/vektah/gqlparser/ast" ) -// MakeExecutableSchema creates an ExecutableSchema from the Resolvers interface. -func MakeExecutableSchema(resolvers Resolvers) graphql.ExecutableSchema { - return &executableSchema{resolvers: resolvers} -} - // NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface. -func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema { - return MakeExecutableSchema(shortMapper{r: resolvers}) +func NewExecutableSchema(cfg Config) graphql.ExecutableSchema { + return &executableSchema{ + resolvers: cfg.Resolvers, + directives: cfg.Directives, + } } -type Resolvers interface { - Mutation_createTodo(ctx context.Context, input model.NewTodo) (model.Todo, error) - Query_todos(ctx context.Context) ([]model.Todo, error) - - Todo_user(ctx context.Context, obj *model.Todo) (model.User, error) +type Config struct { + Resolvers ResolverRoot + Directives DirectiveRoot } type ResolverRoot interface { @@ -36,45 +31,33 @@ type ResolverRoot interface { Query() QueryResolver Todo() TodoResolver } + +type DirectiveRoot struct { +} type MutationResolver interface { - CreateTodo(ctx context.Context, input model.NewTodo) (model.Todo, error) + CreateTodo(ctx context.Context, input NewTodo) (Todo, error) } type QueryResolver interface { - Todos(ctx context.Context) ([]model.Todo, error) + Todos(ctx context.Context) ([]Todo, error) } type TodoResolver interface { - User(ctx context.Context, obj *model.Todo) (model.User, error) -} - -type shortMapper struct { - r ResolverRoot -} - -func (s shortMapper) Mutation_createTodo(ctx context.Context, input model.NewTodo) (model.Todo, error) { - return s.r.Mutation().CreateTodo(ctx, input) -} - -func (s shortMapper) Query_todos(ctx context.Context) ([]model.Todo, error) { - return s.r.Query().Todos(ctx) -} - -func (s shortMapper) Todo_user(ctx context.Context, obj *model.Todo) (model.User, error) { - return s.r.Todo().User(ctx, obj) + User(ctx context.Context, obj *Todo) (User, error) } type executableSchema struct { - resolvers Resolvers + resolvers ResolverRoot + directives DirectiveRoot } -func (e *executableSchema) Schema() *schema.Schema { +func (e *executableSchema) Schema() *ast.Schema { return parsedSchema } -func (e *executableSchema) Query(ctx context.Context, op *query.Operation) *graphql.Response { - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} +func (e *executableSchema) Query(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { - data := ec._Query(ctx, op.Selections) + data := ec._Query(ctx, op.SelectionSet) var buf bytes.Buffer data.MarshalGQL(&buf) return buf.Bytes() @@ -86,11 +69,11 @@ func (e *executableSchema) Query(ctx context.Context, op *query.Operation) *grap } } -func (e *executableSchema) Mutation(ctx context.Context, op *query.Operation) *graphql.Response { - ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers} +func (e *executableSchema) Mutation(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { + ec := executionContext{graphql.GetRequestContext(ctx), e} buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte { - data := ec._Mutation(ctx, op.Selections) + data := ec._Mutation(ctx, op.SelectionSet) var buf bytes.Buffer data.MarshalGQL(&buf) return buf.Bytes() @@ -102,21 +85,20 @@ func (e *executableSchema) Mutation(ctx context.Context, op *query.Operation) *g } } -func (e *executableSchema) Subscription(ctx context.Context, op *query.Operation) func() *graphql.Response { +func (e *executableSchema) Subscription(ctx context.Context, op *ast.OperationDefinition) func() *graphql.Response { return graphql.OneShot(graphql.ErrorResponse(ctx, "subscriptions are not supported")) } type executionContext struct { *graphql.RequestContext - - resolvers Resolvers + *executableSchema } var mutationImplementors = []string{"Mutation"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) _Mutation(ctx context.Context, sel []query.Selection) graphql.Marshaler { - fields := graphql.CollectFields(ec.Doc, sel, mutationImplementors, ec.Variables) +func (ec *executionContext) _Mutation(ctx context.Context, sel ast.SelectionSet) graphql.Marshaler { + fields := graphql.CollectFields(ctx, sel, mutationImplementors) ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ Object: "Mutation", @@ -140,9 +122,10 @@ func (ec *executionContext) _Mutation(ctx context.Context, sel []query.Selection } func (ec *executionContext) _Mutation_createTodo(ctx context.Context, field graphql.CollectedField) graphql.Marshaler { + rawArgs := field.ArgumentMap(ec.Variables) args := map[string]interface{}{} - var arg0 model.NewTodo - if tmp, ok := field.Args["input"]; ok { + var arg0 NewTodo + if tmp, ok := rawArgs["input"]; ok { var err error arg0, err = UnmarshalNewTodo(tmp) if err != nil { @@ -157,26 +140,21 @@ func (ec *executionContext) _Mutation_createTodo(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { - return ec.resolvers.Mutation_createTodo(ctx, args["input"].(model.NewTodo)) + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return ec.resolvers.Mutation().CreateTodo(ctx, args["input"].(NewTodo)) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } - res := resTmp.(model.Todo) + res := resTmp.(Todo) return ec._Todo(ctx, field.Selections, &res) } var queryImplementors = []string{"Query"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) _Query(ctx context.Context, sel []query.Selection) graphql.Marshaler { - fields := graphql.CollectFields(ec.Doc, sel, queryImplementors, ec.Variables) +func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) graphql.Marshaler { + fields := graphql.CollectFields(ctx, sel, queryImplementors) ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ Object: "Query", @@ -191,10 +169,10 @@ func (ec *executionContext) _Query(ctx context.Context, sel []query.Selection) g out.Values[i] = graphql.MarshalString("Query") case "todos": out.Values[i] = ec._Query_todos(ctx, field) - case "__schema": - out.Values[i] = ec._Query___schema(ctx, field) case "__type": out.Values[i] = ec._Query___type(ctx, field) + case "__schema": + out.Values[i] = ec._Query___schema(ctx, field) default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -218,17 +196,13 @@ func (ec *executionContext) _Query_todos(ctx context.Context, field graphql.Coll } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { - return ec.resolvers.Query_todos(ctx) + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return ec.resolvers.Query().Todos(ctx) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } - res := resTmp.([]model.Todo) + res := resTmp.([]Todo) arr1 := graphql.Array{} for idx1 := range res { arr1 = append(arr1, func() graphql.Marshaler { @@ -242,24 +216,11 @@ func (ec *executionContext) _Query_todos(ctx context.Context, field graphql.Coll }) } -func (ec *executionContext) _Query___schema(ctx context.Context, field graphql.CollectedField) graphql.Marshaler { - rctx := graphql.GetResolverContext(ctx) - rctx.Object = "Query" - rctx.Args = nil - rctx.Field = field - rctx.PushField(field.Alias) - defer rctx.Pop() - res := ec.introspectSchema() - if res == nil { - return graphql.Null - } - return ec.___Schema(ctx, field.Selections, res) -} - func (ec *executionContext) _Query___type(ctx context.Context, field graphql.CollectedField) graphql.Marshaler { + rawArgs := field.ArgumentMap(ec.Variables) args := map[string]interface{}{} var arg0 string - if tmp, ok := field.Args["name"]; ok { + if tmp, ok := rawArgs["name"]; ok { var err error arg0, err = graphql.UnmarshalString(tmp) if err != nil { @@ -274,18 +235,44 @@ func (ec *executionContext) _Query___type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := ec.introspectType(args["name"].(string)) + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return ec.introspectType(args["name"].(string)), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*introspection.Type) if res == nil { return graphql.Null } return ec.___Type(ctx, field.Selections, res) } +func (ec *executionContext) _Query___schema(ctx context.Context, field graphql.CollectedField) graphql.Marshaler { + rctx := graphql.GetResolverContext(ctx) + rctx.Object = "Query" + rctx.Args = nil + rctx.Field = field + rctx.PushField(field.Alias) + defer rctx.Pop() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return ec.introspectSchema(), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*introspection.Schema) + if res == nil { + return graphql.Null + } + return ec.___Schema(ctx, field.Selections, res) +} + var todoImplementors = []string{"Todo"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) _Todo(ctx context.Context, sel []query.Selection, obj *model.Todo) graphql.Marshaler { - fields := graphql.CollectFields(ec.Doc, sel, todoImplementors, ec.Variables) +func (ec *executionContext) _Todo(ctx context.Context, sel ast.SelectionSet, obj *Todo) graphql.Marshaler { + fields := graphql.CollectFields(ctx, sel, todoImplementors) out := graphql.NewOrderedMap(len(fields)) for i, field := range fields { @@ -310,40 +297,58 @@ func (ec *executionContext) _Todo(ctx context.Context, sel []query.Selection, ob return out } -func (ec *executionContext) _Todo_id(ctx context.Context, field graphql.CollectedField, obj *model.Todo) graphql.Marshaler { +func (ec *executionContext) _Todo_id(ctx context.Context, field graphql.CollectedField, obj *Todo) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "Todo" rctx.Args = nil rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.ID + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.ID, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(string) return graphql.MarshalID(res) } -func (ec *executionContext) _Todo_text(ctx context.Context, field graphql.CollectedField, obj *model.Todo) graphql.Marshaler { +func (ec *executionContext) _Todo_text(ctx context.Context, field graphql.CollectedField, obj *Todo) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "Todo" rctx.Args = nil rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Text + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Text, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(string) return graphql.MarshalString(res) } -func (ec *executionContext) _Todo_done(ctx context.Context, field graphql.CollectedField, obj *model.Todo) graphql.Marshaler { +func (ec *executionContext) _Todo_done(ctx context.Context, field graphql.CollectedField, obj *Todo) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "Todo" rctx.Args = nil rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Done + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Done, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(bool) return graphql.MarshalBoolean(res) } -func (ec *executionContext) _Todo_user(ctx context.Context, field graphql.CollectedField, obj *model.Todo) graphql.Marshaler { +func (ec *executionContext) _Todo_user(ctx context.Context, field graphql.CollectedField, obj *Todo) graphql.Marshaler { ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ Object: "Todo", Args: nil, @@ -358,17 +363,13 @@ func (ec *executionContext) _Todo_user(ctx context.Context, field graphql.Collec } }() - resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { - return ec.resolvers.Todo_user(ctx, obj) + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return ec.resolvers.Todo().User(ctx, obj) }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } if resTmp == nil { return graphql.Null } - res := resTmp.(model.User) + res := resTmp.(User) return ec._User(ctx, field.Selections, &res) }) } @@ -376,8 +377,8 @@ func (ec *executionContext) _Todo_user(ctx context.Context, field graphql.Collec var userImplementors = []string{"User"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) _User(ctx context.Context, sel []query.Selection, obj *model.User) graphql.Marshaler { - fields := graphql.CollectFields(ec.Doc, sel, userImplementors, ec.Variables) +func (ec *executionContext) _User(ctx context.Context, sel ast.SelectionSet, obj *User) graphql.Marshaler { + fields := graphql.CollectFields(ctx, sel, userImplementors) out := graphql.NewOrderedMap(len(fields)) for i, field := range fields { @@ -398,33 +399,45 @@ func (ec *executionContext) _User(ctx context.Context, sel []query.Selection, ob return out } -func (ec *executionContext) _User_id(ctx context.Context, field graphql.CollectedField, obj *model.User) graphql.Marshaler { +func (ec *executionContext) _User_id(ctx context.Context, field graphql.CollectedField, obj *User) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "User" rctx.Args = nil rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.ID + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.ID, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(string) return graphql.MarshalID(res) } -func (ec *executionContext) _User_name(ctx context.Context, field graphql.CollectedField, obj *model.User) graphql.Marshaler { +func (ec *executionContext) _User_name(ctx context.Context, field graphql.CollectedField, obj *User) graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.Object = "User" rctx.Args = nil rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Name + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Name, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(string) return graphql.MarshalString(res) } var __DirectiveImplementors = []string{"__Directive"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) ___Directive(ctx context.Context, sel []query.Selection, obj *introspection.Directive) graphql.Marshaler { - fields := graphql.CollectFields(ec.Doc, sel, __DirectiveImplementors, ec.Variables) +func (ec *executionContext) ___Directive(ctx context.Context, sel ast.SelectionSet, obj *introspection.Directive) graphql.Marshaler { + fields := graphql.CollectFields(ctx, sel, __DirectiveImplementors) out := graphql.NewOrderedMap(len(fields)) for i, field := range fields { @@ -456,7 +469,13 @@ func (ec *executionContext) ___Directive_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Name() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Name, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(string) return graphql.MarshalString(res) } @@ -467,11 +486,14 @@ func (ec *executionContext) ___Directive_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Description() - if res == nil { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Description, nil + }) + if resTmp == nil { return graphql.Null } - return graphql.MarshalString(*res) + res := resTmp.(string) + return graphql.MarshalString(res) } func (ec *executionContext) ___Directive_locations(ctx context.Context, field graphql.CollectedField, obj *introspection.Directive) graphql.Marshaler { @@ -481,7 +503,13 @@ func (ec *executionContext) ___Directive_locations(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Locations() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Locations, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]string) arr1 := graphql.Array{} for idx1 := range res { arr1 = append(arr1, func() graphql.Marshaler { @@ -501,17 +529,20 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Args() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Args, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]introspection.InputValue) arr1 := graphql.Array{} for idx1 := range res { arr1 = append(arr1, func() graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.PushIndex(idx1) defer rctx.Pop() - if res[idx1] == nil { - return graphql.Null - } - return ec.___InputValue(ctx, field.Selections, res[idx1]) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) }()) } return arr1 @@ -520,8 +551,8 @@ func (ec *executionContext) ___Directive_args(ctx context.Context, field graphql var __EnumValueImplementors = []string{"__EnumValue"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) ___EnumValue(ctx context.Context, sel []query.Selection, obj *introspection.EnumValue) graphql.Marshaler { - fields := graphql.CollectFields(ec.Doc, sel, __EnumValueImplementors, ec.Variables) +func (ec *executionContext) ___EnumValue(ctx context.Context, sel ast.SelectionSet, obj *introspection.EnumValue) graphql.Marshaler { + fields := graphql.CollectFields(ctx, sel, __EnumValueImplementors) out := graphql.NewOrderedMap(len(fields)) for i, field := range fields { @@ -553,7 +584,13 @@ func (ec *executionContext) ___EnumValue_name(ctx context.Context, field graphql rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Name() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Name, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(string) return graphql.MarshalString(res) } @@ -564,11 +601,14 @@ func (ec *executionContext) ___EnumValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Description() - if res == nil { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Description, nil + }) + if resTmp == nil { return graphql.Null } - return graphql.MarshalString(*res) + res := resTmp.(string) + return graphql.MarshalString(res) } func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field graphql.CollectedField, obj *introspection.EnumValue) graphql.Marshaler { @@ -578,7 +618,13 @@ func (ec *executionContext) ___EnumValue_isDeprecated(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.IsDeprecated() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.IsDeprecated, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(bool) return graphql.MarshalBoolean(res) } @@ -589,18 +635,21 @@ func (ec *executionContext) ___EnumValue_deprecationReason(ctx context.Context, rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.DeprecationReason() - if res == nil { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.DeprecationReason, nil + }) + if resTmp == nil { return graphql.Null } - return graphql.MarshalString(*res) + res := resTmp.(string) + return graphql.MarshalString(res) } var __FieldImplementors = []string{"__Field"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) ___Field(ctx context.Context, sel []query.Selection, obj *introspection.Field) graphql.Marshaler { - fields := graphql.CollectFields(ec.Doc, sel, __FieldImplementors, ec.Variables) +func (ec *executionContext) ___Field(ctx context.Context, sel ast.SelectionSet, obj *introspection.Field) graphql.Marshaler { + fields := graphql.CollectFields(ctx, sel, __FieldImplementors) out := graphql.NewOrderedMap(len(fields)) for i, field := range fields { @@ -636,7 +685,13 @@ func (ec *executionContext) ___Field_name(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Name() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Name, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(string) return graphql.MarshalString(res) } @@ -647,11 +702,14 @@ func (ec *executionContext) ___Field_description(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Description() - if res == nil { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Description, nil + }) + if resTmp == nil { return graphql.Null } - return graphql.MarshalString(*res) + res := resTmp.(string) + return graphql.MarshalString(res) } func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.CollectedField, obj *introspection.Field) graphql.Marshaler { @@ -661,17 +719,20 @@ func (ec *executionContext) ___Field_args(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Args() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Args, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]introspection.InputValue) arr1 := graphql.Array{} for idx1 := range res { arr1 = append(arr1, func() graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.PushIndex(idx1) defer rctx.Pop() - if res[idx1] == nil { - return graphql.Null - } - return ec.___InputValue(ctx, field.Selections, res[idx1]) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) }()) } return arr1 @@ -684,7 +745,13 @@ func (ec *executionContext) ___Field_type(ctx context.Context, field graphql.Col rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Type() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Type, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*introspection.Type) if res == nil { return graphql.Null } @@ -698,7 +765,13 @@ func (ec *executionContext) ___Field_isDeprecated(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.IsDeprecated() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.IsDeprecated, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(bool) return graphql.MarshalBoolean(res) } @@ -709,18 +782,21 @@ func (ec *executionContext) ___Field_deprecationReason(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.DeprecationReason() - if res == nil { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.DeprecationReason, nil + }) + if resTmp == nil { return graphql.Null } - return graphql.MarshalString(*res) + res := resTmp.(string) + return graphql.MarshalString(res) } var __InputValueImplementors = []string{"__InputValue"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) ___InputValue(ctx context.Context, sel []query.Selection, obj *introspection.InputValue) graphql.Marshaler { - fields := graphql.CollectFields(ec.Doc, sel, __InputValueImplementors, ec.Variables) +func (ec *executionContext) ___InputValue(ctx context.Context, sel ast.SelectionSet, obj *introspection.InputValue) graphql.Marshaler { + fields := graphql.CollectFields(ctx, sel, __InputValueImplementors) out := graphql.NewOrderedMap(len(fields)) for i, field := range fields { @@ -752,7 +828,13 @@ func (ec *executionContext) ___InputValue_name(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Name() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Name, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(string) return graphql.MarshalString(res) } @@ -763,11 +845,14 @@ func (ec *executionContext) ___InputValue_description(ctx context.Context, field rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Description() - if res == nil { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Description, nil + }) + if resTmp == nil { return graphql.Null } - return graphql.MarshalString(*res) + res := resTmp.(string) + return graphql.MarshalString(res) } func (ec *executionContext) ___InputValue_type(ctx context.Context, field graphql.CollectedField, obj *introspection.InputValue) graphql.Marshaler { @@ -777,7 +862,13 @@ func (ec *executionContext) ___InputValue_type(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Type() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Type, nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*introspection.Type) if res == nil { return graphql.Null } @@ -791,18 +882,21 @@ func (ec *executionContext) ___InputValue_defaultValue(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.DefaultValue() - if res == nil { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.DefaultValue, nil + }) + if resTmp == nil { return graphql.Null } - return graphql.MarshalString(*res) + res := resTmp.(string) + return graphql.MarshalString(res) } var __SchemaImplementors = []string{"__Schema"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) ___Schema(ctx context.Context, sel []query.Selection, obj *introspection.Schema) graphql.Marshaler { - fields := graphql.CollectFields(ec.Doc, sel, __SchemaImplementors, ec.Variables) +func (ec *executionContext) ___Schema(ctx context.Context, sel ast.SelectionSet, obj *introspection.Schema) graphql.Marshaler { + fields := graphql.CollectFields(ctx, sel, __SchemaImplementors) out := graphql.NewOrderedMap(len(fields)) for i, field := range fields { @@ -836,17 +930,20 @@ func (ec *executionContext) ___Schema_types(ctx context.Context, field graphql.C rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Types() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Types(), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]introspection.Type) arr1 := graphql.Array{} for idx1 := range res { arr1 = append(arr1, func() graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.PushIndex(idx1) defer rctx.Pop() - if res[idx1] == nil { - return graphql.Null - } - return ec.___Type(ctx, field.Selections, res[idx1]) + return ec.___Type(ctx, field.Selections, &res[idx1]) }()) } return arr1 @@ -859,7 +956,13 @@ func (ec *executionContext) ___Schema_queryType(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.QueryType() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.QueryType(), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*introspection.Type) if res == nil { return graphql.Null } @@ -873,7 +976,13 @@ func (ec *executionContext) ___Schema_mutationType(ctx context.Context, field gr rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.MutationType() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.MutationType(), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*introspection.Type) if res == nil { return graphql.Null } @@ -887,7 +996,13 @@ func (ec *executionContext) ___Schema_subscriptionType(ctx context.Context, fiel rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.SubscriptionType() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.SubscriptionType(), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*introspection.Type) if res == nil { return graphql.Null } @@ -901,17 +1016,20 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Directives() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Directives(), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]introspection.Directive) arr1 := graphql.Array{} for idx1 := range res { arr1 = append(arr1, func() graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.PushIndex(idx1) defer rctx.Pop() - if res[idx1] == nil { - return graphql.Null - } - return ec.___Directive(ctx, field.Selections, res[idx1]) + return ec.___Directive(ctx, field.Selections, &res[idx1]) }()) } return arr1 @@ -920,8 +1038,8 @@ func (ec *executionContext) ___Schema_directives(ctx context.Context, field grap var __TypeImplementors = []string{"__Type"} // nolint: gocyclo, errcheck, gas, goconst -func (ec *executionContext) ___Type(ctx context.Context, sel []query.Selection, obj *introspection.Type) graphql.Marshaler { - fields := graphql.CollectFields(ec.Doc, sel, __TypeImplementors, ec.Variables) +func (ec *executionContext) ___Type(ctx context.Context, sel ast.SelectionSet, obj *introspection.Type) graphql.Marshaler { + fields := graphql.CollectFields(ctx, sel, __TypeImplementors) out := graphql.NewOrderedMap(len(fields)) for i, field := range fields { @@ -963,7 +1081,13 @@ func (ec *executionContext) ___Type_kind(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Kind() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Kind(), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(string) return graphql.MarshalString(res) } @@ -974,11 +1098,14 @@ func (ec *executionContext) ___Type_name(ctx context.Context, field graphql.Coll rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Name() - if res == nil { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Name(), nil + }) + if resTmp == nil { return graphql.Null } - return graphql.MarshalString(*res) + res := resTmp.(string) + return graphql.MarshalString(res) } func (ec *executionContext) ___Type_description(ctx context.Context, field graphql.CollectedField, obj *introspection.Type) graphql.Marshaler { @@ -988,17 +1115,21 @@ func (ec *executionContext) ___Type_description(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Description() - if res == nil { + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Description(), nil + }) + if resTmp == nil { return graphql.Null } - return graphql.MarshalString(*res) + res := resTmp.(string) + return graphql.MarshalString(res) } func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.CollectedField, obj *introspection.Type) graphql.Marshaler { + rawArgs := field.ArgumentMap(ec.Variables) args := map[string]interface{}{} var arg0 bool - if tmp, ok := field.Args["includeDeprecated"]; ok { + if tmp, ok := rawArgs["includeDeprecated"]; ok { var err error arg0, err = graphql.UnmarshalBoolean(tmp) if err != nil { @@ -1013,17 +1144,20 @@ func (ec *executionContext) ___Type_fields(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Fields(args["includeDeprecated"].(bool)) + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Fields(args["includeDeprecated"].(bool)), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]introspection.Field) arr1 := graphql.Array{} for idx1 := range res { arr1 = append(arr1, func() graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.PushIndex(idx1) defer rctx.Pop() - if res[idx1] == nil { - return graphql.Null - } - return ec.___Field(ctx, field.Selections, res[idx1]) + return ec.___Field(ctx, field.Selections, &res[idx1]) }()) } return arr1 @@ -1036,17 +1170,20 @@ func (ec *executionContext) ___Type_interfaces(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.Interfaces() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.Interfaces(), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]introspection.Type) arr1 := graphql.Array{} for idx1 := range res { arr1 = append(arr1, func() graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.PushIndex(idx1) defer rctx.Pop() - if res[idx1] == nil { - return graphql.Null - } - return ec.___Type(ctx, field.Selections, res[idx1]) + return ec.___Type(ctx, field.Selections, &res[idx1]) }()) } return arr1 @@ -1059,26 +1196,30 @@ func (ec *executionContext) ___Type_possibleTypes(ctx context.Context, field gra rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.PossibleTypes() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.PossibleTypes(), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]introspection.Type) arr1 := graphql.Array{} for idx1 := range res { arr1 = append(arr1, func() graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.PushIndex(idx1) defer rctx.Pop() - if res[idx1] == nil { - return graphql.Null - } - return ec.___Type(ctx, field.Selections, res[idx1]) + return ec.___Type(ctx, field.Selections, &res[idx1]) }()) } return arr1 } func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphql.CollectedField, obj *introspection.Type) graphql.Marshaler { + rawArgs := field.ArgumentMap(ec.Variables) args := map[string]interface{}{} var arg0 bool - if tmp, ok := field.Args["includeDeprecated"]; ok { + if tmp, ok := rawArgs["includeDeprecated"]; ok { var err error arg0, err = graphql.UnmarshalBoolean(tmp) if err != nil { @@ -1093,17 +1234,20 @@ func (ec *executionContext) ___Type_enumValues(ctx context.Context, field graphq rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.EnumValues(args["includeDeprecated"].(bool)) + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.EnumValues(args["includeDeprecated"].(bool)), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]introspection.EnumValue) arr1 := graphql.Array{} for idx1 := range res { arr1 = append(arr1, func() graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.PushIndex(idx1) defer rctx.Pop() - if res[idx1] == nil { - return graphql.Null - } - return ec.___EnumValue(ctx, field.Selections, res[idx1]) + return ec.___EnumValue(ctx, field.Selections, &res[idx1]) }()) } return arr1 @@ -1116,17 +1260,20 @@ func (ec *executionContext) ___Type_inputFields(ctx context.Context, field graph rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.InputFields() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.InputFields(), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]introspection.InputValue) arr1 := graphql.Array{} for idx1 := range res { arr1 = append(arr1, func() graphql.Marshaler { rctx := graphql.GetResolverContext(ctx) rctx.PushIndex(idx1) defer rctx.Pop() - if res[idx1] == nil { - return graphql.Null - } - return ec.___InputValue(ctx, field.Selections, res[idx1]) + return ec.___InputValue(ctx, field.Selections, &res[idx1]) }()) } return arr1 @@ -1139,15 +1286,21 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co rctx.Field = field rctx.PushField(field.Alias) defer rctx.Pop() - res := obj.OfType() + resTmp := ec.FieldMiddleware(ctx, func(ctx context.Context) (interface{}, error) { + return obj.OfType(), nil + }) + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*introspection.Type) if res == nil { return graphql.Null } return ec.___Type(ctx, field.Selections, res) } -func UnmarshalNewTodo(v interface{}) (model.NewTodo, error) { - var it model.NewTodo +func UnmarshalNewTodo(v interface{}) (NewTodo, error) { + var it NewTodo var asMap = v.(map[string]interface{}) for k, v := range asMap { @@ -1158,9 +1311,9 @@ func UnmarshalNewTodo(v interface{}) (model.NewTodo, error) { if err != nil { return it, err } - case "user": + case "userId": var err error - it.User, err = graphql.UnmarshalString(v) + it.UserID, err = graphql.UnmarshalString(v) if err != nil { return it, err } @@ -1170,19 +1323,25 @@ func UnmarshalNewTodo(v interface{}) (model.NewTodo, error) { return it, nil } +func (ec *executionContext) FieldMiddleware(ctx context.Context, next graphql.Resolver) interface{} { + res, err := ec.ResolverMiddleware(ctx, next) + if err != nil { + ec.Error(ctx, err) + return nil + } + return res +} + func (ec *executionContext) introspectSchema() *introspection.Schema { return introspection.WrapSchema(parsedSchema) } func (ec *executionContext) introspectType(name string) *introspection.Type { - t := parsedSchema.Resolve(name) - if t == nil { - return nil - } - return introspection.WrapType(t) + return introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name]) } -var parsedSchema = schema.MustParse(`type Todo { +var parsedSchema = gqlparser.MustLoadSchema( + &ast.Source{Name: "schema.graphql", Input: `type Todo { id: ID! text: String! done: Boolean! @@ -1200,9 +1359,11 @@ type Query { input NewTodo { text: String! - user: String! + userId: String! } type Mutation { createTodo(input: NewTodo!): Todo! -}`) +} +`}, +) diff --git a/gettingstarted/gqlgen.yml b/gettingstarted/gqlgen.yml index 1ed5f61..71d0c7f 100644 --- a/gettingstarted/gqlgen.yml +++ b/gettingstarted/gqlgen.yml @@ -1,11 +1,17 @@ +# .gqlgen.yml example +# +# Refer to https://gqlgen.com/config/ +# for detailed .gqlgen.yml documentation. + schema: schema.graphql exec: - filename: graph/generated.go + filename: generated.go model: - filename: model/generated.go + filename: models_gen.go +resolver: + filename: resolver.go + type: Resolver models: Todo: - model: github.com/vektah/gqlgen-tutorials/gettingstarted/model.Todo - User: - model: github.com/vektah/gqlgen-tutorials/gettingstarted/model.User \ No newline at end of file + model: github.com/vektah/gqlgen-tutorials/gettingstarted.Todo diff --git a/gettingstarted/graph/graph.go b/gettingstarted/graph/graph.go deleted file mode 100644 index 31c7c18..0000000 --- a/gettingstarted/graph/graph.go +++ /dev/null @@ -1,51 +0,0 @@ -//go:generate gqlgen -typemap types.json -schema ../schema.graphql - -package graph - -import ( - "context" - "fmt" - "math/rand" - - "github.com/vektah/gqlgen-tutorials/gettingstarted/model" -) - -type App struct { - todos []model.Todo -} - -func (a *App) Mutation() MutationResolver { - return &mutationResolver{a} -} - -func (a *App) Query() QueryResolver { - return &queryResolver{a} -} - -func (a *App) Todo() TodoResolver { - return &todoResolver{a} -} - -type queryResolver struct{ *App } - -func (a *queryResolver) Todos(ctx context.Context) ([]model.Todo, error) { - return a.todos, nil -} - -type mutationResolver struct{ *App } - -func (a *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (model.Todo, error) { - todo := model.Todo{ - Text: input.Text, - ID: fmt.Sprintf("T%d", rand.Int()), - UserID: input.User, - } - a.todos = append(a.todos, todo) - return todo, nil -} - -type todoResolver struct{ *App } - -func (a *todoResolver) User(ctx context.Context, it *model.Todo) (model.User, error) { - return model.User{ID: it.UserID, Name: "user " + it.UserID}, nil -} diff --git a/gettingstarted/graph/types.json b/gettingstarted/graph/types.json deleted file mode 100644 index 23b1028..0000000 --- a/gettingstarted/graph/types.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "User": "github.com/vektah/gqlgen-tutorials/gettingstarted/graph.User", - "Todo": "github.com/vektah/gqlgen-tutorials/gettingstarted/graph.Todo" -} \ No newline at end of file diff --git a/gettingstarted/main.go b/gettingstarted/main.go deleted file mode 100644 index 1eb9f40..0000000 --- a/gettingstarted/main.go +++ /dev/null @@ -1,18 +0,0 @@ -package main - -import ( - "fmt" - "log" - "net/http" - - "github.com/vektah/gqlgen-tutorials/gettingstarted/graph" - "github.com/vektah/gqlgen/handler" -) - -func main() { - http.Handle("/", handler.Playground("Todo", "/query")) - http.Handle("/query", handler.GraphQL(graph.NewExecutableSchema(&graph.App{}))) - - fmt.Println("Listening on :8080") - log.Fatal(http.ListenAndServe(":8080", nil)) -} diff --git a/gettingstarted/model/generated.go b/gettingstarted/model/generated.go deleted file mode 100644 index c379ba2..0000000 --- a/gettingstarted/model/generated.go +++ /dev/null @@ -1,8 +0,0 @@ -// Code generated by github.com/vektah/gqlgen, DO NOT EDIT. - -package model - -type NewTodo struct { - Text string `json:"text"` - User string `json:"user"` -} diff --git a/gettingstarted/model/user.go b/gettingstarted/model/user.go deleted file mode 100644 index aa270df..0000000 --- a/gettingstarted/model/user.go +++ /dev/null @@ -1,6 +0,0 @@ -package model - -type User struct { - ID string - Name string -} diff --git a/gettingstarted/models_gen.go b/gettingstarted/models_gen.go new file mode 100644 index 0000000..b77504d --- /dev/null +++ b/gettingstarted/models_gen.go @@ -0,0 +1,12 @@ +// Code generated by github.com/vektah/gqlgen, DO NOT EDIT. + +package gettingstarted + +type NewTodo struct { + Text string `json:"text"` + UserID string `json:"userId"` +} +type User struct { + ID string `json:"id"` + Name string `json:"name"` +} diff --git a/gettingstarted/readme.md b/gettingstarted/readme.md deleted file mode 100644 index 6fbcde6..0000000 --- a/gettingstarted/readme.md +++ /dev/null @@ -1 +0,0 @@ -Read the [getting started](https://gqlgen.com/getting-started/) tutorial. diff --git a/gettingstarted/resolver.go b/gettingstarted/resolver.go new file mode 100644 index 0000000..5e9f3ac --- /dev/null +++ b/gettingstarted/resolver.go @@ -0,0 +1,47 @@ +//go:generate gorunpkg github.com/vektah/gqlgen + +package gettingstarted + +import ( + context "context" + "fmt" + "math/rand" +) + +type Resolver struct{ + todos []Todo +} + +func (r *Resolver) Mutation() MutationResolver { + return &mutationResolver{r} +} +func (r *Resolver) Query() QueryResolver { + return &queryResolver{r} +} +func (r *Resolver) Todo() TodoResolver { + return &todoResolver{r} +} + +type mutationResolver struct{ *Resolver } + +func (r *mutationResolver) CreateTodo(ctx context.Context, input NewTodo) (Todo, error) { + todo := Todo{ + Text: input.Text, + ID: fmt.Sprintf("T%d", rand.Int()), + UserID: input.UserID, + } + r.todos = append(r.todos, todo) + return todo, nil +} + +type queryResolver struct{ *Resolver } + +func (r *queryResolver) Todos(ctx context.Context) ([]Todo, error) { + return r.todos, nil +} + +type todoResolver struct{ *Resolver } + +func (r *todoResolver) User(ctx context.Context, obj *Todo) (User, error) { + return User{ID: obj.UserID, Name: "user " + obj.UserID}, nil +} diff --git a/gettingstarted/schema.graphql b/gettingstarted/schema.graphql index 8087897..7e23019 100644 --- a/gettingstarted/schema.graphql +++ b/gettingstarted/schema.graphql @@ -16,9 +16,9 @@ type Query { input NewTodo { text: String! - user: String! + userId: String! } type Mutation { createTodo(input: NewTodo!): Todo! -} \ No newline at end of file +} diff --git a/gettingstarted/server/server.go b/gettingstarted/server/server.go new file mode 100644 index 0000000..12b9271 --- /dev/null +++ b/gettingstarted/server/server.go @@ -0,0 +1,25 @@ +package main + +import ( + "log" + "net/http" + "os" + + gettingstarted "github.com/vektah/gqlgen-tutorials/gettingstarted" + "github.com/vektah/gqlgen/handler" +) + +const defaultPort = "8080" + +func main() { + port := os.Getenv("PORT") + if port == "" { + port = defaultPort + } + + http.Handle("/", handler.Playground("GraphQL playground", "/query")) + http.Handle("/query", handler.GraphQL(gettingstarted.NewExecutableSchema(gettingstarted.Config{Resolvers: &gettingstarted.Resolver{}}))) + + log.Printf("connect to http://localhost:%s/ for GraphQL playground", port) + log.Fatal(http.ListenAndServe(":"+port, nil)) +} diff --git a/gettingstarted/model/todo.go b/gettingstarted/todo.go similarity index 77% rename from gettingstarted/model/todo.go rename to gettingstarted/todo.go index 93bc69f..5578f32 100644 --- a/gettingstarted/model/todo.go +++ b/gettingstarted/todo.go @@ -1,4 +1,4 @@ -package model +package gettingstarted type Todo struct { ID string