From 8fa5f628c2c03f4ca8e23554cb7bce7a0f4e6038 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Sun, 11 Feb 2018 16:38:53 +1100 Subject: [PATCH] less whitespace --- example/dataloader/generated.go | 198 ++------------------ example/starwars/generated.go | 321 ++------------------------------ example/todo/generated.go | 188 ++----------------- main.go | 2 - templates/args.gotpl | 26 +-- templates/file.gotpl | 20 +- templates/interface.gotpl | 10 +- templates/object.gotpl | 66 +++---- 8 files changed, 101 insertions(+), 730 deletions(-) diff --git a/example/dataloader/generated.go b/example/dataloader/generated.go index 683321bf2bf..7e15e478e7c 100644 --- a/example/dataloader/generated.go +++ b/example/dataloader/generated.go @@ -26,7 +26,6 @@ type Resolvers interface { Customer_orders(ctx context.Context, it *Customer) ([]Order, error) Order_items(ctx context.Context, it *Order) ([]Item, error) - Query_customers(ctx context.Context) ([]Customer, error) } @@ -57,15 +56,12 @@ func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[ var result jsonw.JsonWriter if op.Type == query.Query { result = c._query(op.Selections, nil) - } else { return []*errors.QueryError{errors.Errorf("unsupported operation type")} } c.wg.Wait() - // TODO: parallelize if query. - writer := jsonw.New(w) writer.BeginObject() @@ -93,7 +89,6 @@ type executionContext struct { type _AddressNode struct { _fields []collectedField - Id int Street string Country string @@ -111,19 +106,13 @@ func (ec *executionContext) _address(sel []query.Selection, it *Address) jsonw.J switch field.Name { case "id": res := it.ID - node.Id = res - case "street": res := it.Street - node.Street = res - case "country": res := it.Country - node.Country = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -145,7 +134,6 @@ func (t *_AddressNode) WriteJson(w *jsonw.Writer) { case "country": w.ObjectKey("country") w.String(t.Country) - } } w.EndObject() @@ -153,7 +141,6 @@ func (t *_AddressNode) WriteJson(w *jsonw.Writer) { type _CustomerNode struct { _fields []collectedField - Id int Name string Address jsonw.JsonWriter @@ -172,56 +159,38 @@ func (ec *executionContext) _customer(sel []query.Selection, it *Customer) jsonw switch field.Name { case "id": res := it.ID - node.Id = res - case "name": res := it.Name - node.Name = res - case "address": - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Customer_address(ec.ctx, it) if err != nil { ec.Error(err) return } - if res != nil { - node.Address = ec._address(field.Selections, res) - } - }(field) - case "orders": - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Customer_orders(ec.ctx, it) if err != nil { ec.Error(err) return } - if res != nil { - for i := range res { node.Orders = append(node.Orders, ec._order(field.Selections, &res[i])) } - } - }(field) - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -254,7 +223,6 @@ func (t *_CustomerNode) WriteJson(w *jsonw.Writer) { val.WriteJson(w) } w.EndArray() - } } w.EndObject() @@ -262,8 +230,7 @@ func (t *_CustomerNode) WriteJson(w *jsonw.Writer) { type _ItemNode struct { _fields []collectedField - - Name string + Name string } var itemImplementors = []string{"Item"} @@ -278,9 +245,7 @@ func (ec *executionContext) _item(sel []query.Selection, it *Item) jsonw.JsonWri switch field.Name { case "name": res := it.Name - node.Name = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -296,7 +261,6 @@ func (t *_ItemNode) WriteJson(w *jsonw.Writer) { case "name": w.ObjectKey("name") w.String(t.Name) - } } w.EndObject() @@ -304,11 +268,10 @@ func (t *_ItemNode) WriteJson(w *jsonw.Writer) { type _OrderNode struct { _fields []collectedField - - Id int - Date time.Time - Amount float64 - Items []jsonw.JsonWriter + Id int + Date time.Time + Amount float64 + Items []jsonw.JsonWriter } var orderImplementors = []string{"Order"} @@ -323,41 +286,28 @@ func (ec *executionContext) _order(sel []query.Selection, it *Order) jsonw.JsonW switch field.Name { case "id": res := it.ID - node.Id = res - case "date": res := it.Date - node.Date = res - case "amount": res := it.Amount - node.Amount = res - case "items": - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Order_items(ec.ctx, it) if err != nil { ec.Error(err) return } - if res != nil { - for i := range res { node.Items = append(node.Items, ec._item(field.Selections, &res[i])) } - } - }(field) - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -386,15 +336,13 @@ func (t *_OrderNode) WriteJson(w *jsonw.Writer) { val.WriteJson(w) } w.EndArray() - } } w.EndObject() } type _QueryNode struct { - _fields []collectedField - + _fields []collectedField Customers []jsonw.JsonWriter __schema jsonw.JsonWriter __type jsonw.JsonWriter @@ -411,36 +359,25 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) jsonw for _, field := range node._fields { switch field.Name { case "customers": - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Query_customers(ec.ctx) if err != nil { ec.Error(err) return } - if res != nil { - for i := range res { node.Customers = append(node.Customers, ec._customer(field.Selections, &res[i])) } - } - }(field) - case "__schema": res := ec.introspectSchema() - if res != nil { - node.__schema = ec.___Schema(field.Selections, res) - } - case "__type": var arg0 string if tmp, ok := field.Args["name"]; ok { @@ -452,13 +389,9 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) jsonw arg0 = tmp2 } res := ec.introspectType(arg0) - if res != nil { - node.__type = ec.___Type(field.Selections, res) - } - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -492,15 +425,13 @@ func (t *_QueryNode) WriteJson(w *jsonw.Writer) { } else { t.__type.WriteJson(w) } - } } w.EndObject() } type ___DirectiveNode struct { - _fields []collectedField - + _fields []collectedField Name string Description *string Locations []string @@ -519,30 +450,20 @@ func (ec *executionContext) ___Directive(sel []query.Selection, it *introspectio switch field.Name { case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "locations": res := it.Locations() - node.Locations = res - case "args": res := it.Args() - if res != nil { - for i := range res { node.Args = append(node.Args, ec.___InputValue(field.Selections, res[i])) } - } - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -583,15 +504,13 @@ func (t *___DirectiveNode) WriteJson(w *jsonw.Writer) { } } w.EndArray() - } } w.EndObject() } type ___EnumValueNode struct { - _fields []collectedField - + _fields []collectedField Name string Description *string IsDeprecated bool @@ -610,24 +529,16 @@ func (ec *executionContext) ___EnumValue(sel []query.Selection, it *introspectio switch field.Name { case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "isDeprecated": res := it.IsDeprecated() - node.IsDeprecated = res - case "deprecationReason": res := it.DeprecationReason() - node.DeprecationReason = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -660,15 +571,13 @@ func (t *___EnumValueNode) WriteJson(w *jsonw.Writer) { } else { w.String(*t.DeprecationReason) } - } } w.EndObject() } type ___FieldNode struct { - _fields []collectedField - + _fields []collectedField Name string Description *string Args []jsonw.JsonWriter @@ -689,44 +598,28 @@ func (ec *executionContext) ___Field(sel []query.Selection, it *introspection.Fi switch field.Name { case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "args": res := it.Args() - if res != nil { - for i := range res { node.Args = append(node.Args, ec.___InputValue(field.Selections, res[i])) } - } - case "type": res := it.Type() - if res != nil { - node.Type = ec.___Type(field.Selections, res) - } - case "isDeprecated": res := it.IsDeprecated() - node.IsDeprecated = res - case "deprecationReason": res := it.DeprecationReason() - node.DeprecationReason = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -777,15 +670,13 @@ func (t *___FieldNode) WriteJson(w *jsonw.Writer) { } else { w.String(*t.DeprecationReason) } - } } w.EndObject() } type ___InputValueNode struct { - _fields []collectedField - + _fields []collectedField Name string Description *string Type jsonw.JsonWriter @@ -804,28 +695,18 @@ func (ec *executionContext) ___InputValue(sel []query.Selection, it *introspecti switch field.Name { case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "type": res := it.Type() - if res != nil { - node.Type = ec.___Type(field.Selections, res) - } - case "defaultValue": res := it.DefaultValue() - node.DefaultValue = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -862,15 +743,13 @@ func (t *___InputValueNode) WriteJson(w *jsonw.Writer) { } else { w.String(*t.DefaultValue) } - } } w.EndObject() } type ___SchemaNode struct { - _fields []collectedField - + _fields []collectedField Types []jsonw.JsonWriter QueryType jsonw.JsonWriter MutationType jsonw.JsonWriter @@ -890,53 +769,33 @@ func (ec *executionContext) ___Schema(sel []query.Selection, it *introspection.S switch field.Name { case "types": res := it.Types() - if res != nil { - for i := range res { node.Types = append(node.Types, ec.___Type(field.Selections, res[i])) } - } - case "queryType": res := it.QueryType() - if res != nil { - node.QueryType = ec.___Type(field.Selections, res) - } - case "mutationType": res := it.MutationType() - if res != nil { - node.MutationType = ec.___Type(field.Selections, res) - } - case "subscriptionType": res := it.SubscriptionType() - if res != nil { - node.SubscriptionType = ec.___Type(field.Selections, res) - } - case "directives": res := it.Directives() - if res != nil { - for i := range res { node.Directives = append(node.Directives, ec.___Directive(field.Selections, res[i])) } - } - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -992,15 +851,13 @@ func (t *___SchemaNode) WriteJson(w *jsonw.Writer) { } } w.EndArray() - } } w.EndObject() } type ___TypeNode struct { - _fields []collectedField - + _fields []collectedField Kind string Name *string Description *string @@ -1024,19 +881,13 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ switch field.Name { case "kind": res := it.Kind() - node.Kind = res - case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "fields": var arg0 bool if tmp, ok := field.Args["includeDeprecated"]; ok { @@ -1048,37 +899,25 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ arg0 = tmp2 } res := it.Fields(arg0) - if res != nil { - for i := range res { node.Fields = append(node.Fields, ec.___Field(field.Selections, res[i])) } - } - case "interfaces": res := it.Interfaces() - if res != nil { - for i := range res { node.Interfaces = append(node.Interfaces, ec.___Type(field.Selections, res[i])) } - } - case "possibleTypes": res := it.PossibleTypes() - if res != nil { - for i := range res { node.PossibleTypes = append(node.PossibleTypes, ec.___Type(field.Selections, res[i])) } - } - case "enumValues": var arg0 bool if tmp, ok := field.Args["includeDeprecated"]; ok { @@ -1090,35 +929,23 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ arg0 = tmp2 } res := it.EnumValues(arg0) - if res != nil { - for i := range res { node.EnumValues = append(node.EnumValues, ec.___EnumValue(field.Selections, res[i])) } - } - case "inputFields": res := it.InputFields() - if res != nil { - for i := range res { node.InputFields = append(node.InputFields, ec.___InputValue(field.Selections, res[i])) } - } - case "ofType": res := it.OfType() - if res != nil { - node.OfType = ec.___Type(field.Selections, res) - } - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -1210,7 +1037,6 @@ func (t *___TypeNode) WriteJson(w *jsonw.Writer) { } else { t.OfType.WriteJson(w) } - } } w.EndObject() diff --git a/example/starwars/generated.go b/example/starwars/generated.go index 4781fb8573d..bef3123d020 100644 --- a/example/starwars/generated.go +++ b/example/starwars/generated.go @@ -32,7 +32,6 @@ type Resolvers interface { Human_friendsConnection(ctx context.Context, it *Human, first *int, after *string) (FriendsConnection, error) Human_starships(ctx context.Context, it *Human) ([]Starship, error) - Mutation_createReview(ctx context.Context, episode string, review Review) (*Review, error) Query_hero(ctx context.Context, episode *string) (Character, error) @@ -71,18 +70,14 @@ func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[ var result jsonw.JsonWriter if op.Type == query.Query { result = c._query(op.Selections, nil) - } else if op.Type == query.Mutation { result = c._mutation(op.Selections, nil) - } else { return []*errors.QueryError{errors.Errorf("unsupported operation type")} } c.wg.Wait() - // TODO: parallelize if query. - writer := jsonw.New(w) writer.BeginObject() @@ -109,8 +104,7 @@ type executionContext struct { } type _DroidNode struct { - _fields []collectedField - + _fields []collectedField Id string Name string Friends []jsonw.JsonWriter @@ -131,36 +125,25 @@ func (ec *executionContext) _droid(sel []query.Selection, it *Droid) jsonw.JsonW switch field.Name { case "id": res := it.ID - node.Id = res - case "name": res := it.Name - node.Name = res - case "friends": - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Droid_friends(ec.ctx, it) if err != nil { ec.Error(err) return } - if res != nil { - for i := range res { node.Friends = append(node.Friends, ec._character(field.Selections, res[i])) } - } - }(field) - case "friendsConnection": var arg0 *int if tmp, ok := field.Args["first"]; ok { @@ -180,31 +163,22 @@ func (ec *executionContext) _droid(sel []query.Selection, it *Droid) jsonw.JsonW } arg1 = &tmp2 } - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Droid_friendsConnection(ec.ctx, it, arg0, arg1) if err != nil { ec.Error(err) return } - node.FriendsConnection = ec._friendsConnection(field.Selections, &res) - }(field) - case "appearsIn": res := it.AppearsIn - node.AppearsIn = res - case "primaryFunction": res := it.PrimaryFunction - node.PrimaryFunction = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -243,15 +217,13 @@ func (t *_DroidNode) WriteJson(w *jsonw.Writer) { case "primaryFunction": w.ObjectKey("primaryFunction") w.String(t.PrimaryFunction) - } } w.EndObject() } type _FriendsConnectionNode struct { - _fields []collectedField - + _fields []collectedField TotalCount int Edges []jsonw.JsonWriter Friends []jsonw.JsonWriter @@ -270,58 +242,40 @@ func (ec *executionContext) _friendsConnection(sel []query.Selection, it *Friend switch field.Name { case "totalCount": res := it.TotalCount() - node.TotalCount = res - case "edges": - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.FriendsConnection_edges(ec.ctx, it) if err != nil { ec.Error(err) return } - if res != nil { - for i := range res { node.Edges = append(node.Edges, ec._friendsEdge(field.Selections, &res[i])) } - } - }(field) - case "friends": - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.FriendsConnection_friends(ec.ctx, it) if err != nil { ec.Error(err) return } - if res != nil { - for i := range res { node.Friends = append(node.Friends, ec._character(field.Selections, res[i])) } - } - }(field) - case "pageInfo": res := it.PageInfo() - node.PageInfo = ec._pageInfo(field.Selections, &res) - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -354,7 +308,6 @@ func (t *_FriendsConnectionNode) WriteJson(w *jsonw.Writer) { case "pageInfo": w.ObjectKey("pageInfo") t.PageInfo.WriteJson(w) - } } w.EndObject() @@ -362,9 +315,8 @@ func (t *_FriendsConnectionNode) WriteJson(w *jsonw.Writer) { type _FriendsEdgeNode struct { _fields []collectedField - - Cursor string - Node jsonw.JsonWriter + Cursor string + Node jsonw.JsonWriter } var friendsEdgeImplementors = []string{"FriendsEdge"} @@ -379,14 +331,10 @@ func (ec *executionContext) _friendsEdge(sel []query.Selection, it *FriendsEdge) switch field.Name { case "cursor": res := it.Cursor - node.Cursor = res - case "node": res := it.Node - node.Node = ec._character(field.Selections, res) - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -405,15 +353,13 @@ func (t *_FriendsEdgeNode) WriteJson(w *jsonw.Writer) { case "node": w.ObjectKey("node") t.Node.WriteJson(w) - } } w.EndObject() } type _HumanNode struct { - _fields []collectedField - + _fields []collectedField Id string Name string Height float64 @@ -436,14 +382,10 @@ func (ec *executionContext) _human(sel []query.Selection, it *Human) jsonw.JsonW switch field.Name { case "id": res := it.ID - node.Id = res - case "name": res := it.Name - node.Name = res - case "height": var arg0 string if tmp, ok := field.Args["unit"]; ok { @@ -455,36 +397,25 @@ func (ec *executionContext) _human(sel []query.Selection, it *Human) jsonw.JsonW arg0 = tmp2 } res := it.Height(arg0) - node.Height = res - case "mass": res := it.Mass - node.Mass = res - case "friends": - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Human_friends(ec.ctx, it) if err != nil { ec.Error(err) return } - if res != nil { - for i := range res { node.Friends = append(node.Friends, ec._character(field.Selections, res[i])) } - } - }(field) - case "friendsConnection": var arg0 *int if tmp, ok := field.Args["first"]; ok { @@ -504,48 +435,34 @@ func (ec *executionContext) _human(sel []query.Selection, it *Human) jsonw.JsonW } arg1 = &tmp2 } - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Human_friendsConnection(ec.ctx, it, arg0, arg1) if err != nil { ec.Error(err) return } - node.FriendsConnection = ec._friendsConnection(field.Selections, &res) - }(field) - case "appearsIn": res := it.AppearsIn - node.AppearsIn = res - case "starships": - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Human_starships(ec.ctx, it) if err != nil { ec.Error(err) return } - if res != nil { - for i := range res { node.Starships = append(node.Starships, ec._starship(field.Selections, &res[i])) } - } - }(field) - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -594,15 +511,13 @@ func (t *_HumanNode) WriteJson(w *jsonw.Writer) { val.WriteJson(w) } w.EndArray() - } } w.EndObject() } type _MutationNode struct { - _fields []collectedField - + _fields []collectedField CreateReview jsonw.JsonWriter } @@ -632,25 +547,18 @@ func (ec *executionContext) _mutation(sel []query.Selection, it *interface{}) js ec.Error(err) continue } - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Mutation_createReview(ec.ctx, arg0, arg1) if err != nil { ec.Error(err) return } - if res != nil { - node.CreateReview = ec._review(field.Selections, res) - } - }(field) - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -670,15 +578,13 @@ func (t *_MutationNode) WriteJson(w *jsonw.Writer) { } else { t.CreateReview.WriteJson(w) } - } } w.EndObject() } type _PageInfoNode struct { - _fields []collectedField - + _fields []collectedField StartCursor string EndCursor string HasNextPage bool @@ -696,19 +602,13 @@ func (ec *executionContext) _pageInfo(sel []query.Selection, it *PageInfo) jsonw switch field.Name { case "startCursor": res := it.StartCursor - node.StartCursor = res - case "endCursor": res := it.EndCursor - node.EndCursor = res - case "hasNextPage": res := it.HasNextPage - node.HasNextPage = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -730,15 +630,13 @@ func (t *_PageInfoNode) WriteJson(w *jsonw.Writer) { case "hasNextPage": w.ObjectKey("hasNextPage") w.Bool(t.HasNextPage) - } } w.EndObject() } type _QueryNode struct { - _fields []collectedField - + _fields []collectedField Hero jsonw.JsonWriter Reviews []jsonw.JsonWriter Search []jsonw.JsonWriter @@ -770,21 +668,16 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) jsonw } arg0 = &tmp2 } - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Query_hero(ec.ctx, arg0) if err != nil { ec.Error(err) return } - node.Hero = ec._character(field.Selections, res) - }(field) - case "reviews": var arg0 string if tmp, ok := field.Args["episode"]; ok { @@ -809,27 +702,20 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) jsonw continue } } - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Query_reviews(ec.ctx, arg0, arg1) if err != nil { ec.Error(err) return } - if res != nil { - for i := range res { node.Reviews = append(node.Reviews, ec._review(field.Selections, &res[i])) } - } - }(field) - case "search": var arg0 string if tmp, ok := field.Args["text"]; ok { @@ -840,27 +726,20 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) jsonw } arg0 = tmp2 } - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Query_search(ec.ctx, arg0) if err != nil { ec.Error(err) return } - if res != nil { - for i := range res { node.Search = append(node.Search, ec._searchResult(field.Selections, res[i])) } - } - }(field) - case "character": var arg0 string if tmp, ok := field.Args["id"]; ok { @@ -871,21 +750,16 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) jsonw } arg0 = tmp2 } - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Query_character(ec.ctx, arg0) if err != nil { ec.Error(err) return } - node.Character = ec._character(field.Selections, res) - }(field) - case "droid": var arg0 string if tmp, ok := field.Args["id"]; ok { @@ -896,25 +770,18 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) jsonw } arg0 = tmp2 } - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Query_droid(ec.ctx, arg0) if err != nil { ec.Error(err) return } - if res != nil { - node.Droid = ec._droid(field.Selections, res) - } - }(field) - case "human": var arg0 string if tmp, ok := field.Args["id"]; ok { @@ -925,25 +792,18 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) jsonw } arg0 = tmp2 } - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Query_human(ec.ctx, arg0) if err != nil { ec.Error(err) return } - if res != nil { - node.Human = ec._human(field.Selections, res) - } - }(field) - case "starship": var arg0 string if tmp, ok := field.Args["id"]; ok { @@ -954,34 +814,23 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) jsonw } arg0 = tmp2 } - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.Query_starship(ec.ctx, arg0) if err != nil { ec.Error(err) return } - if res != nil { - node.Starship = ec._starship(field.Selections, res) - } - }(field) - case "__schema": res := ec.introspectSchema() - if res != nil { - node.__schema = ec.___Schema(field.Selections, res) - } - case "__type": var arg0 string if tmp, ok := field.Args["name"]; ok { @@ -993,13 +842,9 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) jsonw arg0 = tmp2 } res := ec.introspectType(arg0) - if res != nil { - node.__type = ec.___Type(field.Selections, res) - } - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -1067,15 +912,13 @@ func (t *_QueryNode) WriteJson(w *jsonw.Writer) { } else { t.__type.WriteJson(w) } - } } w.EndObject() } type _ReviewNode struct { - _fields []collectedField - + _fields []collectedField Stars int Commentary *string Time time.Time @@ -1093,19 +936,13 @@ func (ec *executionContext) _review(sel []query.Selection, it *Review) jsonw.Jso switch field.Name { case "stars": res := it.Stars - node.Stars = res - case "commentary": res := it.Commentary - node.Commentary = res - case "time": res := it.Time - node.Time = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -1131,7 +968,6 @@ func (t *_ReviewNode) WriteJson(w *jsonw.Writer) { case "time": w.ObjectKey("time") w.Time(t.Time) - } } w.EndObject() @@ -1139,10 +975,9 @@ func (t *_ReviewNode) WriteJson(w *jsonw.Writer) { type _StarshipNode struct { _fields []collectedField - - Id string - Name string - Length float64 + Id string + Name string + Length float64 } var starshipImplementors = []string{"Starship"} @@ -1157,14 +992,10 @@ func (ec *executionContext) _starship(sel []query.Selection, it *Starship) jsonw switch field.Name { case "id": res := it.ID - node.Id = res - case "name": res := it.Name - node.Name = res - case "length": var arg0 string if tmp, ok := field.Args["unit"]; ok { @@ -1176,9 +1007,7 @@ func (ec *executionContext) _starship(sel []query.Selection, it *Starship) jsonw arg0 = tmp2 } res := it.Length(arg0) - node.Length = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -1200,15 +1029,13 @@ func (t *_StarshipNode) WriteJson(w *jsonw.Writer) { case "length": w.ObjectKey("length") w.Float64(t.Length) - } } w.EndObject() } type ___DirectiveNode struct { - _fields []collectedField - + _fields []collectedField Name string Description *string Locations []string @@ -1227,30 +1054,20 @@ func (ec *executionContext) ___Directive(sel []query.Selection, it *introspectio switch field.Name { case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "locations": res := it.Locations() - node.Locations = res - case "args": res := it.Args() - if res != nil { - for i := range res { node.Args = append(node.Args, ec.___InputValue(field.Selections, res[i])) } - } - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -1291,15 +1108,13 @@ func (t *___DirectiveNode) WriteJson(w *jsonw.Writer) { } } w.EndArray() - } } w.EndObject() } type ___EnumValueNode struct { - _fields []collectedField - + _fields []collectedField Name string Description *string IsDeprecated bool @@ -1318,24 +1133,16 @@ func (ec *executionContext) ___EnumValue(sel []query.Selection, it *introspectio switch field.Name { case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "isDeprecated": res := it.IsDeprecated() - node.IsDeprecated = res - case "deprecationReason": res := it.DeprecationReason() - node.DeprecationReason = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -1368,15 +1175,13 @@ func (t *___EnumValueNode) WriteJson(w *jsonw.Writer) { } else { w.String(*t.DeprecationReason) } - } } w.EndObject() } type ___FieldNode struct { - _fields []collectedField - + _fields []collectedField Name string Description *string Args []jsonw.JsonWriter @@ -1397,44 +1202,28 @@ func (ec *executionContext) ___Field(sel []query.Selection, it *introspection.Fi switch field.Name { case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "args": res := it.Args() - if res != nil { - for i := range res { node.Args = append(node.Args, ec.___InputValue(field.Selections, res[i])) } - } - case "type": res := it.Type() - if res != nil { - node.Type = ec.___Type(field.Selections, res) - } - case "isDeprecated": res := it.IsDeprecated() - node.IsDeprecated = res - case "deprecationReason": res := it.DeprecationReason() - node.DeprecationReason = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -1485,15 +1274,13 @@ func (t *___FieldNode) WriteJson(w *jsonw.Writer) { } else { w.String(*t.DeprecationReason) } - } } w.EndObject() } type ___InputValueNode struct { - _fields []collectedField - + _fields []collectedField Name string Description *string Type jsonw.JsonWriter @@ -1512,28 +1299,18 @@ func (ec *executionContext) ___InputValue(sel []query.Selection, it *introspecti switch field.Name { case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "type": res := it.Type() - if res != nil { - node.Type = ec.___Type(field.Selections, res) - } - case "defaultValue": res := it.DefaultValue() - node.DefaultValue = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -1570,15 +1347,13 @@ func (t *___InputValueNode) WriteJson(w *jsonw.Writer) { } else { w.String(*t.DefaultValue) } - } } w.EndObject() } type ___SchemaNode struct { - _fields []collectedField - + _fields []collectedField Types []jsonw.JsonWriter QueryType jsonw.JsonWriter MutationType jsonw.JsonWriter @@ -1598,53 +1373,33 @@ func (ec *executionContext) ___Schema(sel []query.Selection, it *introspection.S switch field.Name { case "types": res := it.Types() - if res != nil { - for i := range res { node.Types = append(node.Types, ec.___Type(field.Selections, res[i])) } - } - case "queryType": res := it.QueryType() - if res != nil { - node.QueryType = ec.___Type(field.Selections, res) - } - case "mutationType": res := it.MutationType() - if res != nil { - node.MutationType = ec.___Type(field.Selections, res) - } - case "subscriptionType": res := it.SubscriptionType() - if res != nil { - node.SubscriptionType = ec.___Type(field.Selections, res) - } - case "directives": res := it.Directives() - if res != nil { - for i := range res { node.Directives = append(node.Directives, ec.___Directive(field.Selections, res[i])) } - } - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -1700,15 +1455,13 @@ func (t *___SchemaNode) WriteJson(w *jsonw.Writer) { } } w.EndArray() - } } w.EndObject() } type ___TypeNode struct { - _fields []collectedField - + _fields []collectedField Kind string Name *string Description *string @@ -1732,19 +1485,13 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ switch field.Name { case "kind": res := it.Kind() - node.Kind = res - case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "fields": var arg0 bool if tmp, ok := field.Args["includeDeprecated"]; ok { @@ -1756,37 +1503,25 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ arg0 = tmp2 } res := it.Fields(arg0) - if res != nil { - for i := range res { node.Fields = append(node.Fields, ec.___Field(field.Selections, res[i])) } - } - case "interfaces": res := it.Interfaces() - if res != nil { - for i := range res { node.Interfaces = append(node.Interfaces, ec.___Type(field.Selections, res[i])) } - } - case "possibleTypes": res := it.PossibleTypes() - if res != nil { - for i := range res { node.PossibleTypes = append(node.PossibleTypes, ec.___Type(field.Selections, res[i])) } - } - case "enumValues": var arg0 bool if tmp, ok := field.Args["includeDeprecated"]; ok { @@ -1798,35 +1533,23 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ arg0 = tmp2 } res := it.EnumValues(arg0) - if res != nil { - for i := range res { node.EnumValues = append(node.EnumValues, ec.___EnumValue(field.Selections, res[i])) } - } - case "inputFields": res := it.InputFields() - if res != nil { - for i := range res { node.InputFields = append(node.InputFields, ec.___InputValue(field.Selections, res[i])) } - } - case "ofType": res := it.OfType() - if res != nil { - node.OfType = ec.___Type(field.Selections, res) - } - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -1918,7 +1641,6 @@ func (t *___TypeNode) WriteJson(w *jsonw.Writer) { } else { t.OfType.WriteJson(w) } - } } w.EndObject() @@ -1928,19 +1650,16 @@ func (ec *executionContext) _character(sel []query.Selection, it Character) json switch it := it.(type) { case nil: return jsonw.NullWriter - case Human: return ec._human(sel, &it) case *Human: return ec._human(sel, it) - case Droid: return ec._droid(sel, &it) case *Droid: return ec._droid(sel, it) - default: panic(fmt.Errorf("unexpected type %T", it)) } @@ -1950,25 +1669,21 @@ func (ec *executionContext) _searchResult(sel []query.Selection, it SearchResult switch it := it.(type) { case nil: return jsonw.NullWriter - case Human: return ec._human(sel, &it) case *Human: return ec._human(sel, it) - case Droid: return ec._droid(sel, &it) case *Droid: return ec._droid(sel, it) - case Starship: return ec._starship(sel, &it) case *Starship: return ec._starship(sel, it) - default: panic(fmt.Errorf("unexpected type %T", it)) } diff --git a/example/todo/generated.go b/example/todo/generated.go index 9db0c3e2eb8..c053fa2b666 100644 --- a/example/todo/generated.go +++ b/example/todo/generated.go @@ -24,7 +24,6 @@ import ( type Resolvers interface { MyMutation_createTodo(ctx context.Context, text string) (Todo, error) MyMutation_updateTodo(ctx context.Context, id int, changes map[string]interface{}) (*Todo, error) - MyQuery_todo(ctx context.Context, id int) (*Todo, error) MyQuery_lastTodo(ctx context.Context) (*Todo, error) MyQuery_todos(ctx context.Context) ([]Todo, error) @@ -57,18 +56,14 @@ func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[ var result jsonw.JsonWriter if op.Type == query.Query { result = c._myQuery(op.Selections, nil) - } else if op.Type == query.Mutation { result = c._myMutation(op.Selections, nil) - } else { return []*errors.QueryError{errors.Errorf("unsupported operation type")} } c.wg.Wait() - // TODO: parallelize if query. - writer := jsonw.New(w) writer.BeginObject() @@ -95,8 +90,7 @@ type executionContext struct { } type _MyMutationNode struct { - _fields []collectedField - + _fields []collectedField CreateTodo jsonw.JsonWriter UpdateTodo jsonw.JsonWriter } @@ -121,21 +115,16 @@ func (ec *executionContext) _myMutation(sel []query.Selection, it *interface{}) } arg0 = tmp2 } - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.MyMutation_createTodo(ec.ctx, arg0) if err != nil { ec.Error(err) return } - node.CreateTodo = ec._todo(field.Selections, &res) - }(field) - case "updateTodo": var arg0 int if tmp, ok := field.Args["id"]; ok { @@ -148,29 +137,20 @@ func (ec *executionContext) _myMutation(sel []query.Selection, it *interface{}) } var arg1 map[string]interface{} if tmp, ok := field.Args["changes"]; ok { - arg1 = tmp.(map[string]interface{}) - } - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.MyMutation_updateTodo(ec.ctx, arg0, arg1) if err != nil { ec.Error(err) return } - if res != nil { - node.UpdateTodo = ec._todo(field.Selections, res) - } - }(field) - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -193,15 +173,13 @@ func (t *_MyMutationNode) WriteJson(w *jsonw.Writer) { } else { t.UpdateTodo.WriteJson(w) } - } } w.EndObject() } type _MyQueryNode struct { - _fields []collectedField - + _fields []collectedField Todo jsonw.JsonWriter LastTodo jsonw.JsonWriter Todos []jsonw.JsonWriter @@ -229,76 +207,51 @@ func (ec *executionContext) _myQuery(sel []query.Selection, it *interface{}) jso } arg0 = tmp2 } - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.MyQuery_todo(ec.ctx, arg0) if err != nil { ec.Error(err) return } - if res != nil { - node.Todo = ec._todo(field.Selections, res) - } - }(field) - case "lastTodo": - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.MyQuery_lastTodo(ec.ctx) if err != nil { ec.Error(err) return } - if res != nil { - node.LastTodo = ec._todo(field.Selections, res) - } - }(field) - case "todos": - ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - res, err := ec.resolvers.MyQuery_todos(ec.ctx) if err != nil { ec.Error(err) return } - if res != nil { - for i := range res { node.Todos = append(node.Todos, ec._todo(field.Selections, &res[i])) } - } - }(field) - case "__schema": res := ec.introspectSchema() - if res != nil { - node.__schema = ec.___Schema(field.Selections, res) - } - case "__type": var arg0 string if tmp, ok := field.Args["name"]; ok { @@ -310,13 +263,9 @@ func (ec *executionContext) _myQuery(sel []query.Selection, it *interface{}) jso arg0 = tmp2 } res := ec.introspectType(arg0) - if res != nil { - node.__type = ec.___Type(field.Selections, res) - } - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -364,7 +313,6 @@ func (t *_MyQueryNode) WriteJson(w *jsonw.Writer) { } else { t.__type.WriteJson(w) } - } } w.EndObject() @@ -372,10 +320,9 @@ func (t *_MyQueryNode) WriteJson(w *jsonw.Writer) { type _TodoNode struct { _fields []collectedField - - Id int - Text string - Done bool + Id int + Text string + Done bool } var todoImplementors = []string{"Todo"} @@ -390,19 +337,13 @@ func (ec *executionContext) _todo(sel []query.Selection, it *Todo) jsonw.JsonWri switch field.Name { case "id": res := it.ID - node.Id = res - case "text": res := it.Text - node.Text = res - case "done": res := it.Done - node.Done = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -424,15 +365,13 @@ func (t *_TodoNode) WriteJson(w *jsonw.Writer) { case "done": w.ObjectKey("done") w.Bool(t.Done) - } } w.EndObject() } type ___DirectiveNode struct { - _fields []collectedField - + _fields []collectedField Name string Description *string Locations []string @@ -451,30 +390,20 @@ func (ec *executionContext) ___Directive(sel []query.Selection, it *introspectio switch field.Name { case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "locations": res := it.Locations() - node.Locations = res - case "args": res := it.Args() - if res != nil { - for i := range res { node.Args = append(node.Args, ec.___InputValue(field.Selections, res[i])) } - } - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -515,15 +444,13 @@ func (t *___DirectiveNode) WriteJson(w *jsonw.Writer) { } } w.EndArray() - } } w.EndObject() } type ___EnumValueNode struct { - _fields []collectedField - + _fields []collectedField Name string Description *string IsDeprecated bool @@ -542,24 +469,16 @@ func (ec *executionContext) ___EnumValue(sel []query.Selection, it *introspectio switch field.Name { case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "isDeprecated": res := it.IsDeprecated() - node.IsDeprecated = res - case "deprecationReason": res := it.DeprecationReason() - node.DeprecationReason = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -592,15 +511,13 @@ func (t *___EnumValueNode) WriteJson(w *jsonw.Writer) { } else { w.String(*t.DeprecationReason) } - } } w.EndObject() } type ___FieldNode struct { - _fields []collectedField - + _fields []collectedField Name string Description *string Args []jsonw.JsonWriter @@ -621,44 +538,28 @@ func (ec *executionContext) ___Field(sel []query.Selection, it *introspection.Fi switch field.Name { case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "args": res := it.Args() - if res != nil { - for i := range res { node.Args = append(node.Args, ec.___InputValue(field.Selections, res[i])) } - } - case "type": res := it.Type() - if res != nil { - node.Type = ec.___Type(field.Selections, res) - } - case "isDeprecated": res := it.IsDeprecated() - node.IsDeprecated = res - case "deprecationReason": res := it.DeprecationReason() - node.DeprecationReason = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -709,15 +610,13 @@ func (t *___FieldNode) WriteJson(w *jsonw.Writer) { } else { w.String(*t.DeprecationReason) } - } } w.EndObject() } type ___InputValueNode struct { - _fields []collectedField - + _fields []collectedField Name string Description *string Type jsonw.JsonWriter @@ -736,28 +635,18 @@ func (ec *executionContext) ___InputValue(sel []query.Selection, it *introspecti switch field.Name { case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "type": res := it.Type() - if res != nil { - node.Type = ec.___Type(field.Selections, res) - } - case "defaultValue": res := it.DefaultValue() - node.DefaultValue = res - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -794,15 +683,13 @@ func (t *___InputValueNode) WriteJson(w *jsonw.Writer) { } else { w.String(*t.DefaultValue) } - } } w.EndObject() } type ___SchemaNode struct { - _fields []collectedField - + _fields []collectedField Types []jsonw.JsonWriter QueryType jsonw.JsonWriter MutationType jsonw.JsonWriter @@ -822,53 +709,33 @@ func (ec *executionContext) ___Schema(sel []query.Selection, it *introspection.S switch field.Name { case "types": res := it.Types() - if res != nil { - for i := range res { node.Types = append(node.Types, ec.___Type(field.Selections, res[i])) } - } - case "queryType": res := it.QueryType() - if res != nil { - node.QueryType = ec.___Type(field.Selections, res) - } - case "mutationType": res := it.MutationType() - if res != nil { - node.MutationType = ec.___Type(field.Selections, res) - } - case "subscriptionType": res := it.SubscriptionType() - if res != nil { - node.SubscriptionType = ec.___Type(field.Selections, res) - } - case "directives": res := it.Directives() - if res != nil { - for i := range res { node.Directives = append(node.Directives, ec.___Directive(field.Selections, res[i])) } - } - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -924,15 +791,13 @@ func (t *___SchemaNode) WriteJson(w *jsonw.Writer) { } } w.EndArray() - } } w.EndObject() } type ___TypeNode struct { - _fields []collectedField - + _fields []collectedField Kind string Name *string Description *string @@ -956,19 +821,13 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ switch field.Name { case "kind": res := it.Kind() - node.Kind = res - case "name": res := it.Name() - node.Name = res - case "description": res := it.Description() - node.Description = res - case "fields": var arg0 bool if tmp, ok := field.Args["includeDeprecated"]; ok { @@ -980,37 +839,25 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ arg0 = tmp2 } res := it.Fields(arg0) - if res != nil { - for i := range res { node.Fields = append(node.Fields, ec.___Field(field.Selections, res[i])) } - } - case "interfaces": res := it.Interfaces() - if res != nil { - for i := range res { node.Interfaces = append(node.Interfaces, ec.___Type(field.Selections, res[i])) } - } - case "possibleTypes": res := it.PossibleTypes() - if res != nil { - for i := range res { node.PossibleTypes = append(node.PossibleTypes, ec.___Type(field.Selections, res[i])) } - } - case "enumValues": var arg0 bool if tmp, ok := field.Args["includeDeprecated"]; ok { @@ -1022,35 +869,23 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ arg0 = tmp2 } res := it.EnumValues(arg0) - if res != nil { - for i := range res { node.EnumValues = append(node.EnumValues, ec.___EnumValue(field.Selections, res[i])) } - } - case "inputFields": res := it.InputFields() - if res != nil { - for i := range res { node.InputFields = append(node.InputFields, ec.___InputValue(field.Selections, res[i])) } - } - case "ofType": res := it.OfType() - if res != nil { - node.OfType = ec.___Type(field.Selections, res) - } - default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -1142,7 +977,6 @@ func (t *___TypeNode) WriteJson(w *jsonw.Writer) { } else { t.OfType.WriteJson(w) } - } } w.EndObject() diff --git a/main.go b/main.go index f81a80c73ca..daa2d20d008 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,6 @@ import ( "syscall" "github.com/vektah/gqlgen/neelance/schema" - "golang.org/x/tools/imports" ) @@ -68,7 +67,6 @@ func main() { "schema": "github.com/vektah/gqlgen/neelance/schema", "validation": "github.com/vektah/gqlgen/neelance/validation", "jsonw": "github.com/vektah/gqlgen/jsonw", - "graphql": "github.com/vektah/gqlgen", }, } e.extract() diff --git a/templates/args.gotpl b/templates/args.gotpl index 5648634d96f..49130501d78 100644 --- a/templates/args.gotpl +++ b/templates/args.gotpl @@ -1,7 +1,7 @@ -{{define "args" -}} - {{- range $i, $arg := . -}} - var arg{{$i}} {{$arg.Type.Local -}} - {{ if eq $arg.Type.FullName "time.Time" }} +{{- define "args" }} + {{- range $i, $arg := . }} + var arg{{$i}} {{$arg.Type.Local }} + {{- if eq $arg.Type.FullName "time.Time" }} if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok { if tmpStr, ok := tmp.(string); ok { tmpDate, err := time.Parse(time.RFC3339, tmpStr) @@ -15,16 +15,16 @@ continue } } - {{ else if eq $arg.Type.Name "map[string]interface{}" }} + {{- else if eq $arg.Type.Name "map[string]interface{}" }} if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok { - {{ if $arg.Type.IsPtr }} + {{- if $arg.Type.IsPtr }} tmp2 := tmp.({{$arg.Type.Name}}) arg{{$i}} = &tmp2 - {{ else }} + {{- else }} arg{{$i}} = tmp.({{$arg.Type.Name}}) - {{ end }} + {{- end }} } - {{ else if $arg.Type.Scalar }} + {{- else if $arg.Type.Scalar }} if tmp, ok := field.Args[{{$arg.Name|quote}}]; ok { tmp2, err := coerce{{$arg.Type.Name|ucFirst}}(tmp) if err != nil { @@ -33,12 +33,12 @@ } arg{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmp2 } - {{ else }} + {{- else }} err := unpackComplexArg(&arg{{$i}}, field.Args[{{$arg.Name|quote}}]) if err != nil { ec.Error(err) continue } - {{end}} - {{- end -}} -{{- end -}} + {{- end}} + {{- end }} +{{- end }} diff --git a/templates/file.gotpl b/templates/file.gotpl index dec3c9fe6d1..a53749eb14b 100644 --- a/templates/file.gotpl +++ b/templates/file.gotpl @@ -4,17 +4,17 @@ package {{ .PackageName }} import ( -{{ range $as, $import := .Imports }} +{{- range $as, $import := .Imports }} {{- $as }} "{{ $import }}" {{ end }} ) type Resolvers interface { -{{ range $object := .Objects -}} +{{- range $object := .Objects -}} {{ range $field := $object.Fields -}} {{ $field.ResolverDeclaration $object }} {{ end }} -{{ end }} +{{- end }} } func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[string]interface{}, io.Writer) []*errors.QueryError { @@ -44,18 +44,16 @@ func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[ var result jsonw.JsonWriter if op.Type == query.Query { result = c._{{.QueryRoot|lcFirst}}(op.Selections, nil) - {{if .MutationRoot}} + {{- if .MutationRoot}} } else if op.Type == query.Mutation { result = c._{{.MutationRoot|lcFirst}}(op.Selections, nil) - {{end}} + {{- end}} } else { return []*errors.QueryError{errors.Errorf("unsupported operation type")} } c.wg.Wait() - // TODO: parallelize if query. - writer := jsonw.New(w) writer.BeginObject() @@ -81,13 +79,13 @@ type executionContext struct { wg sync.WaitGroup } -{{range $object := .Objects }} +{{- range $object := .Objects }} {{ template "object" $object }} -{{end}} +{{- end}} -{{ range $interface := .Interfaces }} +{{- range $interface := .Interfaces }} {{ template "interface" $interface }} -{{ end }} +{{- end }} var parsedSchema = schema.MustParse({{.SchemaRaw|quote}}) diff --git a/templates/interface.gotpl b/templates/interface.gotpl index d890aa9518a..0b745fdc3ad 100644 --- a/templates/interface.gotpl +++ b/templates/interface.gotpl @@ -1,21 +1,21 @@ -{{define "interface"}} -{{ $interface := . }} +{{- define "interface"}} +{{- $interface := . }} func (ec *executionContext) _{{$interface.Type.GraphQLName|lcFirst}}(sel []query.Selection, it {{$interface.Type.Local}}) jsonw.JsonWriter { switch it := it.(type) { case nil: return jsonw.NullWriter - {{range $implementor := $interface.Type.Implementors}} + {{- range $implementor := $interface.Type.Implementors }} case {{$implementor.Local}}: return ec._{{$implementor.GraphQLName|lcFirst}}(sel, &it) case *{{$implementor.Local}}: return ec._{{$implementor.GraphQLName|lcFirst}}(sel, it) - {{end}} + {{- end }} default: panic(fmt.Errorf("unexpected type %T", it)) } } -{{end}} +{{- end}} diff --git a/templates/object.gotpl b/templates/object.gotpl index d24969e7d97..4b3e455089f 100644 --- a/templates/object.gotpl +++ b/templates/object.gotpl @@ -1,15 +1,15 @@ -{{ define "object" }} +{{- define "object" }} {{ $object := . }} type _{{ .Type.GraphQLName}}Node struct { _fields []collectedField - {{range $field := .Fields -}} - {{ if $field.Type.Scalar -}} + {{- range $field := .Fields }} + {{- if $field.Type.Scalar }} {{$field.GraphQLName|ucFirst}} {{ $field.Type.Local }} - {{ else -}} + {{- else }} {{$field.GraphQLName|ucFirst}} {{if $field.Type.IsSlice}}[]{{end}}jsonw.JsonWriter - {{ end -}} - {{end}} + {{- end }} + {{- end}} } var {{ $object.Type.GraphQLName|lcFirst}}Implementors = {{$object.Implementors}} @@ -22,58 +22,58 @@ func (ec *executionContext) _{{$object.Type.GraphQLName|lcFirst}}(sel []query.Se for _, field := range node._fields { switch field.Name { - {{range $field := $object.Fields -}} + {{- range $field := $object.Fields }} case "{{$field.GraphQLName}}": - {{ template "args" $field.Args -}} + {{- template "args" $field.Args }} - {{ if $field.IsResolver }} + {{- if $field.IsResolver }} ec.wg.Add(1) go func(field collectedField) { defer ec.wg.Done() - {{ end }} + {{- end }} - {{- if $field.VarName -}} + {{- if $field.VarName }} res := {{$field.VarName}} - {{ else if $field.MethodName -}} - {{ if $field.NoErr -}} + {{- else if $field.MethodName }} + {{- if $field.NoErr }} res := {{$field.MethodName}}({{ $field.CallArgs $object }}) - {{ else -}} + {{- else }} res, err := {{$field.MethodName}}({{ $field.CallArgs $object }}) if err != nil { ec.Error(err) continue } - {{ end }} - {{ else }} + {{- end }} + {{- else }} res, err := ec.resolvers.{{ $object.Name }}_{{ $field.GraphQLName }}({{ $field.CallArgs $object }}) if err != nil { ec.Error(err) return } - {{ end -}} + {{- end }} - {{ if $field.Type.Scalar }} + {{- if $field.Type.Scalar }} node.{{$field.GraphQLName|ucFirst}} = res - {{ else }} - {{ if or $field.Type.IsPtr $field.Type.IsSlice }} + {{- else }} + {{- if or $field.Type.IsPtr $field.Type.IsSlice }} if res != nil { - {{ end}} - {{ if $field.Type.IsSlice }} + {{- end }} + {{- if $field.Type.IsSlice }} for i := range {{$field.Type.ByVal "res" }} { node.{{$field.GraphQLName|ucFirst}} = append(node.{{$field.GraphQLName|ucFirst}}, ec._{{$field.Type.GraphQLName|lcFirst}}(field.Selections, {{ $field.Type.Elem.ByRef "res[i]" }})) } - {{ else }} + {{- else }} node.{{$field.GraphQLName|ucFirst}} = ec._{{$field.Type.GraphQLName|lcFirst}}(field.Selections, {{$field.Type.ByRef "res" }}) - {{ end }} - {{ if or $field.Type.IsPtr $field.Type.IsSlice }} + {{- end }} + {{- if or $field.Type.IsPtr $field.Type.IsSlice }} } - {{ end}} - {{ end }} + {{- end}} + {{- end }} - {{ if $field.IsResolver }} + {{- if $field.IsResolver }} }(field) - {{ end }} - {{ end}} + {{- end }} + {{- end }} default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -86,14 +86,14 @@ func (t *_{{ $object.Type.GraphQLName }}Node) WriteJson(w *jsonw.Writer) { w.BeginObject() for _, field := range t._fields { switch field.Name { - {{range $field := $object.Fields -}} + {{- range $field := $object.Fields }} case "{{$field.GraphQLName}}": w.ObjectKey({{$field.GraphQLName|quote}}) {{ $field.WriteJson }} - {{ end }} + {{- end }} } } w.EndObject() } -{{end}} +{{- end}}