Skip to content

Commit

Permalink
__typename support
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Feb 16, 2018
1 parent 51292db commit 5c04d1a
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 11 deletions.
19 changes: 16 additions & 3 deletions example/dataloader/addressloader_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,29 @@ func (l *AddressLoader) LoadThunk(key int) func() (*Address, error) {
return func() (*Address, error) {
<-batch.done

if batch.error[pos] == nil {
var data *Address
if pos < len(batch.data) {
data = batch.data[pos]
}

var err error
// its convenient to be able to return a single error for everything
if len(batch.error) == 1 {
err = batch.error[pos]
} else if batch.error != nil {
err = batch.error[pos]
}

if err == nil {
l.mu.Lock()
if l.cache == nil {
l.cache = map[int]*Address{}
}
l.cache[key] = batch.data[pos]
l.cache[key] = data
l.mu.Unlock()
}

return batch.data[pos], batch.error[pos]
return data, err
}
}

Expand Down
22 changes: 22 additions & 0 deletions example/dataloader/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func (ec *executionContext) _address(sel []query.Selection, it *Address) jsonw.W
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("Address")
case "id":
res := it.ID

Expand Down Expand Up @@ -125,6 +127,8 @@ func (ec *executionContext) _customer(sel []query.Selection, it *Customer) jsonw
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("Customer")
case "id":
res := it.ID

Expand Down Expand Up @@ -186,6 +190,8 @@ func (ec *executionContext) _item(sel []query.Selection, it *Item) jsonw.Writer
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("Item")
case "name":
res := it.Name

Expand All @@ -209,6 +215,8 @@ func (ec *executionContext) _order(sel []query.Selection, it *Order) jsonw.Write
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("Order")
case "id":
res := it.ID

Expand Down Expand Up @@ -258,6 +266,8 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) jsonw
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("Query")
case "customers":
ec.wg.Add(1)
go func(i int, field collectedField) {
Expand Down Expand Up @@ -320,6 +330,8 @@ func (ec *executionContext) ___Directive(sel []query.Selection, it *introspectio
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("__Directive")
case "name":
res := it.Name()

Expand Down Expand Up @@ -376,6 +388,8 @@ func (ec *executionContext) ___EnumValue(sel []query.Selection, it *introspectio
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("__EnumValue")
case "name":
res := it.Name()

Expand Down Expand Up @@ -419,6 +433,8 @@ func (ec *executionContext) ___Field(sel []query.Selection, it *introspection.Fi
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("__Field")
case "name":
res := it.Name()

Expand Down Expand Up @@ -485,6 +501,8 @@ func (ec *executionContext) ___InputValue(sel []query.Selection, it *introspecti
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("__InputValue")
case "name":
res := it.Name()

Expand Down Expand Up @@ -532,6 +550,8 @@ func (ec *executionContext) ___Schema(sel []query.Selection, it *introspection.S
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("__Schema")
case "types":
res := it.Types()

Expand Down Expand Up @@ -605,6 +625,8 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("__Type")
case "kind":
res := it.Kind()

Expand Down
19 changes: 16 additions & 3 deletions example/dataloader/itemsliceloader_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,29 @@ func (l *ItemSliceLoader) LoadThunk(key int) func() ([]Item, error) {
return func() ([]Item, error) {
<-batch.done

if batch.error[pos] == nil {
var data []Item
if pos < len(batch.data) {
data = batch.data[pos]
}

var err error
// its convenient to be able to return a single error for everything
if len(batch.error) == 1 {
err = batch.error[pos]
} else if batch.error != nil {
err = batch.error[pos]
}

if err == nil {
l.mu.Lock()
if l.cache == nil {
l.cache = map[int][]Item{}
}
l.cache[key] = batch.data[pos]
l.cache[key] = data
l.mu.Unlock()
}

return batch.data[pos], batch.error[pos]
return data, err
}
}

Expand Down
19 changes: 16 additions & 3 deletions example/dataloader/ordersliceloader_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,29 @@ func (l *OrderSliceLoader) LoadThunk(key int) func() ([]Order, error) {
return func() ([]Order, error) {
<-batch.done

if batch.error[pos] == nil {
var data []Order
if pos < len(batch.data) {
data = batch.data[pos]
}

var err error
// its convenient to be able to return a single error for everything
if len(batch.error) == 1 {
err = batch.error[pos]
} else if batch.error != nil {
err = batch.error[pos]
}

if err == nil {
l.mu.Lock()
if l.cache == nil {
l.cache = map[int][]Order{}
}
l.cache[key] = batch.data[pos]
l.cache[key] = data
l.mu.Unlock()
}

return batch.data[pos], batch.error[pos]
return data, err
}
}

Expand Down
30 changes: 30 additions & 0 deletions example/starwars/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func (ec *executionContext) _droid(sel []query.Selection, it *Droid) jsonw.Write
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("Droid")
case "id":
res := it.ID

Expand Down Expand Up @@ -199,6 +201,8 @@ func (ec *executionContext) _friendsConnection(sel []query.Selection, it *Friend
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("FriendsConnection")
case "totalCount":
res := it.TotalCount()

Expand Down Expand Up @@ -262,6 +266,8 @@ func (ec *executionContext) _friendsEdge(sel []query.Selection, it *FriendsEdge)
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("FriendsEdge")
case "cursor":
res := it.Cursor

Expand Down Expand Up @@ -289,6 +295,8 @@ func (ec *executionContext) _human(sel []query.Selection, it *Human) jsonw.Write
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("Human")
case "id":
res := it.ID

Expand Down Expand Up @@ -409,6 +417,8 @@ func (ec *executionContext) _mutation(sel []query.Selection, it *interface{}) js
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("Mutation")
case "createReview":
var arg0 string
if tmp, ok := field.Args["episode"]; ok {
Expand Down Expand Up @@ -455,6 +465,8 @@ func (ec *executionContext) _pageInfo(sel []query.Selection, it *PageInfo) jsonw
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("PageInfo")
case "startCursor":
res := it.StartCursor

Expand Down Expand Up @@ -486,6 +498,8 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) jsonw
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("Query")
case "hero":
var arg0 *string
if tmp, ok := field.Args["episode"]; ok {
Expand Down Expand Up @@ -715,6 +729,8 @@ func (ec *executionContext) _review(sel []query.Selection, it *Review) jsonw.Wri
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("Review")
case "stars":
res := it.Stars

Expand Down Expand Up @@ -750,6 +766,8 @@ func (ec *executionContext) _starship(sel []query.Selection, it *Starship) jsonw
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("Starship")
case "id":
res := it.ID

Expand Down Expand Up @@ -807,6 +825,8 @@ func (ec *executionContext) ___Directive(sel []query.Selection, it *introspectio
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("__Directive")
case "name":
res := it.Name()

Expand Down Expand Up @@ -863,6 +883,8 @@ func (ec *executionContext) ___EnumValue(sel []query.Selection, it *introspectio
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("__EnumValue")
case "name":
res := it.Name()

Expand Down Expand Up @@ -906,6 +928,8 @@ func (ec *executionContext) ___Field(sel []query.Selection, it *introspection.Fi
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("__Field")
case "name":
res := it.Name()

Expand Down Expand Up @@ -972,6 +996,8 @@ func (ec *executionContext) ___InputValue(sel []query.Selection, it *introspecti
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("__InputValue")
case "name":
res := it.Name()

Expand Down Expand Up @@ -1019,6 +1045,8 @@ func (ec *executionContext) ___Schema(sel []query.Selection, it *introspection.S
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("__Schema")
case "types":
res := it.Types()

Expand Down Expand Up @@ -1092,6 +1120,8 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ
out.Values[i] = jsonw.Null

switch field.Name {
case "__typename":
out.Values[i] = jsonw.String("__Type")
case "kind":
res := it.Kind()

Expand Down
8 changes: 6 additions & 2 deletions example/starwars/starwars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ func TestStarwars(t *testing.T) {

t.Run("get character", func(t *testing.T) {
var resp struct {
Character struct{ Name string }
Character struct {
Name string
Typename string `json:"__typename"`
}
}
c.MustPost(`{ character(id:2001) { name } }`, &resp)
c.MustPost(`{ character(id:2001) { name, __typename } }`, &resp)

require.Equal(t, "R2-D2", resp.Character.Name)
require.Equal(t, "Droid", resp.Character.Typename)
})

t.Run("missing character", func(t *testing.T) {
Expand Down
Loading

0 comments on commit 5c04d1a

Please sign in to comment.