github.com/maeglindeveloper/gqlgen@v0.13.1-0.20210413081235-57808b12a0a0/example/starwars/starwars_test.go (about)

     1  package starwars
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/99designs/gqlgen/client"
     7  	"github.com/99designs/gqlgen/example/starwars/generated"
     8  	"github.com/99designs/gqlgen/graphql/handler"
     9  	"github.com/99designs/gqlgen/graphql/introspection"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestStarwars(t *testing.T) {
    14  	c := client.New(handler.NewDefaultServer(generated.NewExecutableSchema(NewResolver())))
    15  
    16  	t.Run("Lukes starships", func(t *testing.T) {
    17  		var resp struct {
    18  			Search []struct{ Starships []struct{ Name string } }
    19  		}
    20  		c.MustPost(`{ search(text:"Luke") { ... on Human { starships { name } } } }`, &resp)
    21  
    22  		require.Equal(t, "X-Wing", resp.Search[0].Starships[0].Name)
    23  		require.Equal(t, "Imperial shuttle", resp.Search[0].Starships[1].Name)
    24  	})
    25  
    26  	t.Run("get character", func(t *testing.T) {
    27  		var resp struct {
    28  			Character struct {
    29  				Name     string
    30  				Typename string `json:"__typename"`
    31  			}
    32  		}
    33  		c.MustPost(`{ character(id:"2001") { name, __typename } }`, &resp)
    34  
    35  		require.Equal(t, "R2-D2", resp.Character.Name)
    36  		require.Equal(t, "Droid", resp.Character.Typename)
    37  	})
    38  
    39  	t.Run("missing character", func(t *testing.T) {
    40  		var resp struct {
    41  			Character *struct{ Name string }
    42  		}
    43  		c.MustPost(`{ character(id:"2002") { name } }`, &resp)
    44  
    45  		require.Nil(t, resp.Character)
    46  	})
    47  
    48  	t.Run("get droid", func(t *testing.T) {
    49  		var resp struct {
    50  			Droid struct{ PrimaryFunction string }
    51  		}
    52  		c.MustPost(`{ droid(id:"2001") { primaryFunction } }`, &resp)
    53  
    54  		require.Equal(t, "Astromech", resp.Droid.PrimaryFunction)
    55  	})
    56  
    57  	t.Run("get human", func(t *testing.T) {
    58  		var resp struct {
    59  			Human struct {
    60  				Starships []struct {
    61  					Name   string
    62  					Length float64
    63  				}
    64  			}
    65  		}
    66  		c.MustPost(`{ human(id:"1000") { starships { name length(unit:FOOT) } } }`, &resp)
    67  
    68  		require.Equal(t, "X-Wing", resp.Human.Starships[0].Name)
    69  		require.Equal(t, 41.0105, resp.Human.Starships[0].Length)
    70  
    71  		require.Equal(t, "Imperial shuttle", resp.Human.Starships[1].Name)
    72  		require.Equal(t, 65.6168, resp.Human.Starships[1].Length)
    73  	})
    74  
    75  	t.Run("hero height", func(t *testing.T) {
    76  		var resp struct {
    77  			Hero struct {
    78  				Height float64
    79  			}
    80  		}
    81  		c.MustPost(`{ hero(episode:EMPIRE) { ... on Human { height(unit:METER) } } }`, &resp)
    82  
    83  		require.Equal(t, 1.72, resp.Hero.Height)
    84  	})
    85  
    86  	t.Run("default hero episode", func(t *testing.T) {
    87  		var resp struct {
    88  			Hero struct {
    89  				Name string
    90  			}
    91  		}
    92  		c.MustPost(`{ hero { ... on Droid { name } } }`, &resp)
    93  
    94  		require.Equal(t, "R2-D2", resp.Hero.Name)
    95  	})
    96  
    97  	t.Run("friends", func(t *testing.T) {
    98  		var resp struct {
    99  			Human struct {
   100  				Friends []struct {
   101  					Name string
   102  				}
   103  			}
   104  		}
   105  		c.MustPost(`{ human(id: "1001") { friends { name } } }`, &resp)
   106  
   107  		require.Equal(t, "Wilhuff Tarkin", resp.Human.Friends[0].Name)
   108  	})
   109  
   110  	t.Run("friendsConnection.friends", func(t *testing.T) {
   111  		var resp struct {
   112  			Droid struct {
   113  				FriendsConnection struct {
   114  					Friends []struct {
   115  						Name string
   116  					}
   117  				}
   118  			}
   119  		}
   120  		c.MustPost(`{ droid(id:"2001") { friendsConnection { friends { name } } } }`, &resp)
   121  
   122  		require.Equal(t, "Luke Skywalker", resp.Droid.FriendsConnection.Friends[0].Name)
   123  		require.Equal(t, "Han Solo", resp.Droid.FriendsConnection.Friends[1].Name)
   124  		require.Equal(t, "Leia Organa", resp.Droid.FriendsConnection.Friends[2].Name)
   125  	})
   126  
   127  	t.Run("friendsConnection.edges", func(t *testing.T) {
   128  		var resp struct {
   129  			Droid struct {
   130  				FriendsConnection struct {
   131  					Edges []struct {
   132  						Cursor string
   133  						Node   struct {
   134  							Name string
   135  						}
   136  					}
   137  				}
   138  			}
   139  		}
   140  		c.MustPost(`{ droid(id:"2001") { friendsConnection { edges { cursor, node { name } } } } }`, &resp)
   141  
   142  		require.Equal(t, "Y3Vyc29yMQ==", resp.Droid.FriendsConnection.Edges[0].Cursor)
   143  		require.Equal(t, "Luke Skywalker", resp.Droid.FriendsConnection.Edges[0].Node.Name)
   144  		require.Equal(t, "Y3Vyc29yMg==", resp.Droid.FriendsConnection.Edges[1].Cursor)
   145  		require.Equal(t, "Han Solo", resp.Droid.FriendsConnection.Edges[1].Node.Name)
   146  		require.Equal(t, "Y3Vyc29yMw==", resp.Droid.FriendsConnection.Edges[2].Cursor)
   147  		require.Equal(t, "Leia Organa", resp.Droid.FriendsConnection.Edges[2].Node.Name)
   148  	})
   149  
   150  	t.Run("unset optional arguments", func(t *testing.T) {
   151  		var resp struct {
   152  			Hero struct {
   153  				FriendsConnection struct {
   154  					Friends []struct {
   155  						Name string
   156  					}
   157  				}
   158  			}
   159  		}
   160  		query := `
   161  			query a($first:Int, $after:ID) {
   162  				hero {
   163  					friendsConnection(first:$first, after:$after) {
   164  						friends { name }
   165  					}
   166  				}
   167  			}`
   168  		c.MustPost(query, &resp)
   169  
   170  		require.Len(t, resp.Hero.FriendsConnection.Friends, 3)
   171  	})
   172  
   173  	t.Run("mutations must be run in sequence", func(t *testing.T) {
   174  		var resp struct {
   175  			A struct{ Time string }
   176  			B struct{ Time string }
   177  			C struct{ Time string }
   178  		}
   179  
   180  		c.MustPost(`mutation f{
   181  		  a:createReview(episode: NEWHOPE, review:{stars:1, commentary:"Blah blah"})  {
   182  			time
   183  		  }
   184  		  b:createReview(episode: NEWHOPE, review:{stars:1, commentary:"Blah blah"})  {
   185  			time
   186  		  }
   187  		  c:createReview(episode: NEWHOPE, review:{stars:1, commentary:"Blah blah"})  {
   188  			time
   189  		  }
   190  		}`, &resp)
   191  
   192  		require.NotEqual(t, resp.A.Time, resp.B.Time)
   193  		require.NotEqual(t, resp.C.Time, resp.B.Time)
   194  	})
   195  
   196  	t.Run("multidimensional arrays", func(t *testing.T) {
   197  		var resp struct {
   198  			Starship struct {
   199  				History [][]int
   200  			}
   201  		}
   202  		c.MustPost(`{ starship(id:"3001") { history } }`, &resp)
   203  
   204  		require.Len(t, resp.Starship.History, 4)
   205  		require.Len(t, resp.Starship.History[0], 2)
   206  	})
   207  
   208  	t.Run("invalid enums in variables", func(t *testing.T) {
   209  		var resp struct{}
   210  
   211  		err := c.Post(`mutation($episode: Episode!) {
   212  		  createReview(episode: $episode, review:{stars:1, commentary:"Blah blah"})  {
   213  			time
   214  		  }
   215  		}`, &resp, client.Var("episode", "INVALID"))
   216  
   217  		require.EqualError(t, err, `http 422: {"errors":[{"message":"INVALID is not a valid Episode","path":["variable","episode"],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`)
   218  	})
   219  
   220  	t.Run("introspection", func(t *testing.T) {
   221  		// Make sure we can run the graphiql introspection query without errors
   222  		var resp interface{}
   223  		c.MustPost(introspection.Query, &resp)
   224  	})
   225  
   226  	t.Run("aliased field and non-aliased field", func(t *testing.T) {
   227  		var resp struct {
   228  			Character struct {
   229  				Name string
   230  			}
   231  			AliasedCharacter struct {
   232  				Name string
   233  			}
   234  		}
   235  		c.MustPost(`{
   236  			character(id: "2001") { name }
   237  			aliasedCharacter: character(id: "2001") { name }
   238  		}`, &resp)
   239  		require.Equal(t, resp.Character, resp.AliasedCharacter)
   240  	})
   241  }