Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 250 additions & 0 deletions examples/endpoints/connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
package endpoints_test

import (
"reflect"
"testing"

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
"github.com/graphql-go/relay/examples/endpoints"
)

func TestConnection_TestFetching_CorrectlyFetchesTheFirstShipOfThesites(t *testing.T) {
query := `
query {
sites {
siteId,
endpoints(first: 1) {
edges {
node {
endpointId
}
}
}
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"sites": map[string]interface{}{
"siteId": "6500023",
"endpoints": map[string]interface{}{
"edges": []interface{}{
map[string]interface{}{
"node": map[string]interface{}{
"endpointId": "37866524-cc91-4d64-b5db-b912eaf4339e",
},
},
},
},
},
},
}
result := graphql.Do(graphql.Params{
Schema: endpoints.Schema,
RequestString: query,
})
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
}
func TestConnection_TestFetching_CorrectlyFetchesTheFirstTwoendpointsOfThesitesWithACursor(t *testing.T) {
query := `
query {
sites {
siteId,
endpoints(first: 2) {
edges {
cursor,
node {
endpointId
}
}
}
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"sites": map[string]interface{}{
"siteId": "6500023",
"endpoints": map[string]interface{}{
"edges": []interface{}{
map[string]interface{}{
"cursor": "YXJyYXljb25uZWN0aW9uOjA=",
"node": map[string]interface{}{
"endpointId": "37866524-cc91-4d64-b5db-b912eaf4339e",
},
},
map[string]interface{}{
"cursor": "YXJyYXljb25uZWN0aW9uOjE=",
"node": map[string]interface{}{
"endpointId": "37866524-cc91-4d64-b5db-b912eaf4339u",
},
},
},
},
},
},
}
result := graphql.Do(graphql.Params{
Schema: endpoints.Schema,
RequestString: query,
})
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
}
func TestConnection_TestFetching_CorrectlyFetchesTheNextTwoEndpointsOfThesitesWithACursor(t *testing.T) {
query := `
query {
sites {
siteId,
endpoints(first: 2, after: "YXJyYXljb25uZWN0aW9uOjA=") {
edges {
cursor,
node {
endpointId
}
}
}
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"sites": map[string]interface{}{
"siteId": "6500023",
"endpoints": map[string]interface{}{
"edges": []interface{}{
map[string]interface{}{
"cursor": "YXJyYXljb25uZWN0aW9uOjE=",
"node": map[string]interface{}{
"endpointId": "37866524-cc91-4d64-b5db-b912eaf4339u",
},
},
map[string]interface{}{
"cursor": "YXJyYXljb25uZWN0aW9uOjI=",
"node": map[string]interface{}{
"endpointId": "37866524-cc91-4d64-b5db-b912eaf4338u",
},
},
},
},
},
},
}
result := graphql.Do(graphql.Params{
Schema: endpoints.Schema,
RequestString: query,
})
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
}
func TestConnection_TestFetching_CorrectlyFetchesNoendpointsOfThesitesAtTheEndOfTheConnection(t *testing.T) {
query := `
query {
sites {
siteId,
endpoints(first: 2, after: "YXJyYXljb25uZWN0aW9uOjQ=") {
edges {
cursor,
node {
endpointId
}
}
}
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"sites": map[string]interface{}{
"siteId": "6500023",
"endpoints": map[string]interface{}{
"edges": []interface{}{},
},
},
},
}
result := graphql.Do(graphql.Params{
Schema: endpoints.Schema,
RequestString: query,
})
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
}
func TestConnection_TestFetching_CorrectlyIdentifiesTheEndOfTheList(t *testing.T) {
query := `
query {
sites {
siteId,
originalendpoints: endpoints(first: 2) {
edges {
node {
endpointId
}
}
pageInfo {
hasNextPage
}
}
moreendpoints: endpoints(first: 1 after: "YXJyYXljb25uZWN0aW9uOjE=") {
edges {
node {
endpointId
}
}
pageInfo {
hasNextPage
}
}
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"sites": map[string]interface{}{
"siteId": "6500023",
"originalendpoints": map[string]interface{}{
"edges": []interface{}{
map[string]interface{}{
"node": map[string]interface{}{
"endpointId": "37866524-cc91-4d64-b5db-b912eaf4339e",
},
},
map[string]interface{}{
"node": map[string]interface{}{
"endpointId": "37866524-cc91-4d64-b5db-b912eaf4339u",
},
},
},
"pageInfo": map[string]interface{}{
"hasNextPage": true,
},
},
"moreendpoints": map[string]interface{}{
"edges": []interface{}{
map[string]interface{}{
"node": map[string]interface{}{
"endpointId": "37866524-cc91-4d64-b5db-b912eaf4338u",
},
},
},
"pageInfo": map[string]interface{}{
"hasNextPage": false,
},
},
},
},
}
result := graphql.Do(graphql.Params{
Schema: endpoints.Schema,
RequestString: query,
})
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
}
62 changes: 62 additions & 0 deletions examples/endpoints/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package endpoints

/*
This defines a basic set of data for our Star Wars Schema.

This data is hard coded for the sake of the demo, but you could imagine
fetching this data from a backend service rather than from hardcoded
JSON objects in a more complex demo.
*/

//Endpoint ...
type Endpoint struct {
ClientID string `json:"clientId"`
SiteID string `json:"siteId"`
EndpointID string `json:"endpointId"`
}

//Site ...
type Site struct {
ClientID string `json:"clientId"`
SiteID string `json:"siteId"`
Code string `json:"siteCode"`
Name string `json:"siteName"`
Endpoints []Endpoint `json:"endpoints"`
}

var E1 = &Endpoint{"6500023", "6500023", "37866524-cc91-4d64-b5db-b912eaf4339e"}
var E2 = &Endpoint{"6500023", "6500023", "37866524-cc91-4d64-b5db-b912eaf4339u"}
var E3 = &Endpoint{"6500023", "6500023", "37866524-cc91-4d64-b5db-b912eaf4338u"}

var S1 = &Site{
"6500023",
"6500023",
"test",
"test-name",
[]Endpoint{*E1, *E2, *E3},
}

var endpointss = map[string]*Endpoint{
"37866524-cc91-4d64-b5db-b912eaf4339e": E1,
"37866524-cc91-4d64-b5db-b912eaf4339u": E2,
"37866524-cc91-4d64-b5db-b912eaf4338u": E3,
}
var sites = map[string]*Site{
"6500023": S1,
}

func GetSite(id string) *Site {
if site, ok := sites[id]; ok {
return site
}
return nil
}
func GetEndpoint(id string) *Endpoint {
if endpoint, ok := endpointss[id]; ok {
return endpoint
}
return nil
}
func GetSites() *Site {
return S1
}
Loading