func TestTypeSystem_EnumTypesMustBeWellDefined_RejectsAnEnumTypeWithoutValues(t *testing.T) {

	_, err := schemaWithFieldType(types.NewGraphQLEnumType(types.GraphQLEnumTypeConfig{
		Name: "SomeEnum",
	}))
	expectedError := `SomeEnum values must be an object with value names as keys.`
	if err == nil || err.Error() != expectedError {
		t.Fatalf("Expected error: %v, got %v", expectedError, err)
	}
}
func TestTypeSystem_EnumTypesMustBeWellDefined_AcceptsAWellDefinedEnumTypeWithEmptyValueDefinition(t *testing.T) {

	_, err := schemaWithFieldType(types.NewGraphQLEnumType(types.GraphQLEnumTypeConfig{
		Name: "SomeEnum",
		Values: types.GraphQLEnumValueConfigMap{
			"FOO": &types.GraphQLEnumValueConfig{},
			"BAR": &types.GraphQLEnumValueConfig{},
		},
	}))
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
}
	IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
		return true
	},
})
var interfaceType = types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
	Name: "Interface",
})
var unionType = types.NewGraphQLUnionType(types.GraphQLUnionTypeConfig{
	Name: "Union",
	Types: []*types.GraphQLObjectType{
		objectType,
	},
})
var enumType = types.NewGraphQLEnumType(types.GraphQLEnumTypeConfig{
	Name: "Enum",
	Values: types.GraphQLEnumValueConfigMap{
		"foo": &types.GraphQLEnumValueConfig{},
	},
})
var inputObjectType = types.NewGraphQLInputObjectType(types.InputObjectConfig{
	Name: "InputObject",
})

func init() {
	blogAuthor.AddFieldConfig("recentArticle", &types.GraphQLFieldConfig{
		Type: blogArticle,
	})
}

func TestTypeSystem_DefinitionExample_DefinesAQueryOnlySchema(t *testing.T) {
	blogSchema, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
		Query: blogQuery,
Exemplo n.º 4
0
func init() {
	Luke = StarWarsChar{
		Id:         "1000",
		Name:       "Luke Skywalker",
		AppearsIn:  []int{4, 5, 6},
		HomePlanet: "Tatooine",
	}
	Vader = StarWarsChar{
		Id:         "1001",
		Name:       "Darth Vader",
		AppearsIn:  []int{4, 5, 6},
		HomePlanet: "Tatooine",
	}
	Han = StarWarsChar{
		Id:        "1002",
		Name:      "Han Solo",
		AppearsIn: []int{4, 5, 6},
	}
	Leia = StarWarsChar{
		Id:         "1003",
		Name:       "Leia Organa",
		AppearsIn:  []int{4, 5, 6},
		HomePlanet: "Alderaa",
	}
	Tarkin = StarWarsChar{
		Id:        "1004",
		Name:      "Wilhuff Tarkin",
		AppearsIn: []int{4},
	}
	Threepio = StarWarsChar{
		Id:              "2000",
		Name:            "C-3PO",
		AppearsIn:       []int{4, 5, 6},
		PrimaryFunction: "Protocol",
	}
	Artoo = StarWarsChar{
		Id:              "2001",
		Name:            "R2-D2",
		AppearsIn:       []int{4, 5, 6},
		PrimaryFunction: "Astromech",
	}
	Luke.Friends = append(Luke.Friends, []StarWarsChar{Han, Leia, Threepio, Artoo}...)
	Vader.Friends = append(Luke.Friends, []StarWarsChar{Tarkin}...)
	Han.Friends = append(Han.Friends, []StarWarsChar{Luke, Leia, Artoo}...)
	Leia.Friends = append(Leia.Friends, []StarWarsChar{Luke, Han, Threepio, Artoo}...)
	Tarkin.Friends = append(Tarkin.Friends, []StarWarsChar{Vader}...)
	Threepio.Friends = append(Threepio.Friends, []StarWarsChar{Luke, Han, Leia, Artoo}...)
	Artoo.Friends = append(Artoo.Friends, []StarWarsChar{Luke, Han, Leia}...)
	HumanData = map[int]StarWarsChar{
		1000: Luke,
		1001: Vader,
		1002: Han,
		1003: Leia,
		1004: Tarkin,
	}
	DroidData = map[int]StarWarsChar{
		2000: Threepio,
		2001: Artoo,
	}

	episodeEnum := types.NewGraphQLEnumType(types.GraphQLEnumTypeConfig{
		Name:        "Episode",
		Description: "One of the films in the Star Wars Trilogy",
		Values: types.GraphQLEnumValueConfigMap{
			"NEWHOPE": &types.GraphQLEnumValueConfig{
				Value:       4,
				Description: "Released in 1977.",
			},
			"EMPIRE": &types.GraphQLEnumValueConfig{
				Value:       5,
				Description: "Released in 1980.",
			},
			"JEDI": &types.GraphQLEnumValueConfig{
				Value:       6,
				Description: "Released in 1983.",
			},
		},
	})

	characterInterface := types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
		Name:        "Character",
		Description: "A character in the Star Wars Trilogy",
		Fields: types.GraphQLFieldConfigMap{
			"id": &types.GraphQLFieldConfig{
				Type:        types.NewGraphQLNonNull(types.GraphQLString),
				Description: "The id of the character.",
			},
			"name": &types.GraphQLFieldConfig{
				Type:        types.GraphQLString,
				Description: "The name of the character.",
			},
			"appearsIn": &types.GraphQLFieldConfig{
				Type:        types.NewGraphQLList(episodeEnum),
				Description: "Which movies they appear in.",
			},
		},
		ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
			if character, ok := value.(StarWarsChar); ok {
				id, _ := strconv.Atoi(character.Id)
				human := GetHuman(id)
				if human.Id != "" {
					return humanType
				}
			}
			return droidType
		},
	})
	characterInterface.AddFieldConfig("friends", &types.GraphQLFieldConfig{
		Type:        types.NewGraphQLList(characterInterface),
		Description: "The friends of the character, or an empty list if they have none.",
	})

	humanType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
		Name:        "Human",
		Description: "A humanoid creature in the Star Wars universe.",
		Fields: types.GraphQLFieldConfigMap{
			"id": &types.GraphQLFieldConfig{
				Type:        types.NewGraphQLNonNull(types.GraphQLString),
				Description: "The id of the human.",
				Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} {
					if human, ok := p.Source.(StarWarsChar); ok {
						return human.Id
					}
					return nil
				},
			},
			"name": &types.GraphQLFieldConfig{
				Type:        types.GraphQLString,
				Description: "The name of the human.",
				Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} {
					if human, ok := p.Source.(StarWarsChar); ok {
						return human.Name
					}
					return nil
				},
			},
			"friends": &types.GraphQLFieldConfig{
				Type:        types.NewGraphQLList(characterInterface),
				Description: "The friends of the human, or an empty list if they have none.",
				Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} {
					if human, ok := p.Source.(StarWarsChar); ok {
						return human.Friends
					}
					return []interface{}{}
				},
			},
			"appearsIn": &types.GraphQLFieldConfig{
				Type:        types.NewGraphQLList(episodeEnum),
				Description: "Which movies they appear in.",
				Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} {
					if human, ok := p.Source.(StarWarsChar); ok {
						return human.AppearsIn
					}
					return nil
				},
			},
			"homePlanet": &types.GraphQLFieldConfig{
				Type:        types.GraphQLString,
				Description: "The home planet of the human, or null if unknown.",
				Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} {
					if human, ok := p.Source.(StarWarsChar); ok {
						return human.HomePlanet
					}
					return nil
				},
			},
		},
		Interfaces: []*types.GraphQLInterfaceType{
			characterInterface,
		},
	})
	droidType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
		Name:        "Droid",
		Description: "A mechanical creature in the Star Wars universe.",
		Fields: types.GraphQLFieldConfigMap{
			"id": &types.GraphQLFieldConfig{
				Type:        types.NewGraphQLNonNull(types.GraphQLString),
				Description: "The id of the droid.",
				Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} {
					if droid, ok := p.Source.(StarWarsChar); ok {
						return droid.Id
					}
					return nil
				},
			},
			"name": &types.GraphQLFieldConfig{
				Type:        types.GraphQLString,
				Description: "The name of the droid.",
				Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} {
					if droid, ok := p.Source.(StarWarsChar); ok {
						return droid.Name
					}
					return nil
				},
			},
			"friends": &types.GraphQLFieldConfig{
				Type:        types.NewGraphQLList(characterInterface),
				Description: "The friends of the droid, or an empty list if they have none.",
				Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} {
					if droid, ok := p.Source.(StarWarsChar); ok {
						friends := []map[string]interface{}{}
						for _, friend := range droid.Friends {
							friends = append(friends, map[string]interface{}{
								"name": friend.Name,
								"id":   friend.Id,
							})
						}
						return droid.Friends
					}
					return []interface{}{}
				},
			},
			"appearsIn": &types.GraphQLFieldConfig{
				Type:        types.NewGraphQLList(episodeEnum),
				Description: "Which movies they appear in.",
				Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} {
					if droid, ok := p.Source.(StarWarsChar); ok {
						return droid.AppearsIn
					}
					return nil
				},
			},
			"primaryFunction": &types.GraphQLFieldConfig{
				Type:        types.GraphQLString,
				Description: "The primary function of the droid.",
				Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} {
					if droid, ok := p.Source.(StarWarsChar); ok {
						return droid.PrimaryFunction
					}
					return nil
				},
			},
		},
		Interfaces: []*types.GraphQLInterfaceType{
			characterInterface,
		},
	})

	queryType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
		Name: "Query",
		Fields: types.GraphQLFieldConfigMap{
			"hero": &types.GraphQLFieldConfig{
				Type: characterInterface,
				Args: types.GraphQLFieldConfigArgumentMap{
					"episode": &types.GraphQLArgumentConfig{
						Description: "If omitted, returns the hero of the whole saga. If " +
							"provided, returns the hero of that particular episode.",
						Type: episodeEnum,
					},
				},
				Resolve: func(ctx context.Context, p types.GQLFRParams) (r interface{}) {
					return GetHero(p.Args["episode"])
				},
			},
			"human": &types.GraphQLFieldConfig{
				Type: humanType,
				Args: types.GraphQLFieldConfigArgumentMap{
					"id": &types.GraphQLArgumentConfig{
						Description: "id of the human",
						Type:        types.NewGraphQLNonNull(types.GraphQLString),
					},
				},
				Resolve: func(ctx context.Context, p types.GQLFRParams) (r interface{}) {
					return GetHuman(p.Args["id"].(int))
				},
			},
			"droid": &types.GraphQLFieldConfig{
				Type: droidType,
				Args: types.GraphQLFieldConfigArgumentMap{
					"id": &types.GraphQLArgumentConfig{
						Description: "id of the droid",
						Type:        types.NewGraphQLNonNull(types.GraphQLString),
					},
				},
				Resolve: func(ctx context.Context, p types.GQLFRParams) (r interface{}) {
					return GetDroid(p.Args["id"].(int))
				},
			},
		},
	})
	StarWarsSchema, _ = types.NewGraphQLSchema(types.GraphQLSchemaConfig{
		Query: queryType,
	})
}
Exemplo n.º 5
0
import (
	"github.com/chris-ramon/graphql-go"
	"github.com/chris-ramon/graphql-go/errors"
	"github.com/chris-ramon/graphql-go/testutil"
	"github.com/chris-ramon/graphql-go/types"
	"reflect"
	"testing"
)

var enumTypeTestColorType = types.NewGraphQLEnumType(types.GraphQLEnumTypeConfig{
	Name: "Color",
	Values: types.GraphQLEnumValueConfigMap{
		"RED": &types.GraphQLEnumValueConfig{
			Value: 0,
		},
		"GREEN": &types.GraphQLEnumValueConfig{
			Value: 1,
		},
		"BLUE": &types.GraphQLEnumValueConfig{
			Value: 2,
		},
	},
})
var enumTypeTestQueryType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
	Name: "Query",
	Fields: types.GraphQLFieldConfigMap{
		"colorEnum": &types.GraphQLFieldConfig{
			Type: enumTypeTestColorType,
			Args: types.GraphQLFieldConfigArgumentMap{
				"fromEnum": &types.GraphQLArgumentConfig{
					Type: enumTypeTestColorType,
				},
func TestIntrospection_RespectsTheIncludeDeprecatedParameterForEnumValues(t *testing.T) {

	testEnum := types.NewGraphQLEnumType(types.GraphQLEnumTypeConfig{
		Name: "TestEnum",
		Values: types.GraphQLEnumValueConfigMap{
			"NONDEPRECATED": &types.GraphQLEnumValueConfig{
				Value: 0,
			},
			"DEPRECATED": &types.GraphQLEnumValueConfig{
				Value:             1,
				DeprecationReason: "Removed in 1.0",
			},
			"ALSONONDEPRECATED": &types.GraphQLEnumValueConfig{
				Value: 2,
			},
		},
	})
	testType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
		Name: "TestType",
		Fields: types.GraphQLFieldConfigMap{
			"testEnum": &types.GraphQLFieldConfig{
				Type: testEnum,
			},
		},
	})
	schema, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
		Query: testType,
	})
	if err != nil {
		t.Fatalf("Error creating GraphQLSchema: %v", err.Error())
	}
	query := `
      {
        __type(name: "TestEnum") {
          name
          trueValues: enumValues(includeDeprecated: true) {
            name
          }
          falseValues: enumValues(includeDeprecated: false) {
            name
          }
          omittedValues: enumValues {
            name
          }
        }
      }
    `
	expected := &types.GraphQLResult{
		Data: map[string]interface{}{
			"__type": map[string]interface{}{
				"name": "TestEnum",
				"trueValues": []interface{}{
					map[string]interface{}{
						"name": "NONDEPRECATED",
					},
					map[string]interface{}{
						"name": "DEPRECATED",
					},
					map[string]interface{}{
						"name": "ALSONONDEPRECATED",
					},
				},
				"falseValues": []interface{}{
					map[string]interface{}{
						"name": "NONDEPRECATED",
					},
					map[string]interface{}{
						"name": "ALSONONDEPRECATED",
					},
				},
				"omittedValues": []interface{}{
					map[string]interface{}{
						"name": "NONDEPRECATED",
					},
					map[string]interface{}{
						"name": "ALSONONDEPRECATED",
					},
				},
			},
		},
	}
	result := graphql(t, gql.GraphqlParams{
		Schema:        schema,
		RequestString: query,
	})
	if !testutil.ContainSubset(result.Data.(map[string]interface{}), expected.Data.(map[string]interface{})) {
		t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
	}
}
	},
})
var someInterfaceType = types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
	Name: "SomeInterface",
	ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
		return nil
	},
	Fields: types.GraphQLFieldConfigMap{
		"f": &types.GraphQLFieldConfig{
			Type: types.GraphQLString,
		},
	},
})
var someEnumType = types.NewGraphQLEnumType(types.GraphQLEnumTypeConfig{
	Name: "SomeEnum",
	Values: types.GraphQLEnumValueConfigMap{
		"ONLY": &types.GraphQLEnumValueConfig{},
	},
})
var someInputObject = types.NewGraphQLInputObjectType(types.InputObjectConfig{
	Name: "SomeInputObject",
	Fields: types.InputObjectConfigFieldMap{
		"f": &types.InputObjectFieldConfig{
			Type:         types.GraphQLString,
			DefaultValue: "Hello",
		},
	},
})

func withModifiers(ttypes []types.GraphQLType) []types.GraphQLType {
	res := ttypes
	for _, ttype := range ttypes {