本文整理汇总了Golang中github.com/chris-ramon/graphql-go/types.NewGraphQLInterfaceType函数的典型用法代码示例。如果您正苦于以下问题:Golang NewGraphQLInterfaceType函数的具体用法?Golang NewGraphQLInterfaceType怎么用?Golang NewGraphQLInterfaceType使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewGraphQLInterfaceType函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1:
func TestTypeSystem_InterfaceTypesMustBeResolvable_AcceptsAnInterfaceTypeDefiningResolveTypeWithImplementingTypeDefiningIsTypeOf(t *testing.T) {
anotherInterfaceType := types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
Name: "AnotherInterface",
ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
return nil
},
Fields: types.GraphQLFieldConfigMap{
"f": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
})
_, err := schemaWithFieldType(types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "SomeObject",
Interfaces: []*types.GraphQLInterfaceType{anotherInterfaceType},
IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
return true
},
Fields: types.GraphQLFieldConfigMap{
"f": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
}))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
示例2: TestTypeSystem_ObjectsMustAdhereToInterfaceTheyImplement_RejectsAnObjectMissingAnInterfaceArgument
func TestTypeSystem_ObjectsMustAdhereToInterfaceTheyImplement_RejectsAnObjectMissingAnInterfaceArgument(t *testing.T) {
anotherInterface := types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
Name: "AnotherInterface",
ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
return nil
},
Fields: types.GraphQLFieldConfigMap{
"field": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
Args: types.GraphQLFieldConfigArgumentMap{
"input": &types.GraphQLArgumentConfig{
Type: types.GraphQLString,
},
},
},
},
})
anotherObject := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "AnotherObject",
Interfaces: []*types.GraphQLInterfaceType{anotherInterface},
Fields: types.GraphQLFieldConfigMap{
"field": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
})
_, err := schemaWithObjectFieldOfType(anotherObject)
expectedError := `AnotherInterface.field expects argument "input" but AnotherObject.field does not provide it.`
if err == nil || err.Error() != expectedError {
t.Fatalf("Expected error: %v, got %v", expectedError, err)
}
}
示例3: TestTypeSystem_ObjectsCanOnlyImplementInterfaces_AcceptsAnObjectImplementingAnInterface
func TestTypeSystem_ObjectsCanOnlyImplementInterfaces_AcceptsAnObjectImplementingAnInterface(t *testing.T) {
anotherInterfaceType := types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
Name: "AnotherInterface",
ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
return nil
},
Fields: types.GraphQLFieldConfigMap{
"f": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
})
_, err := schemaWithObjectImplementingType(anotherInterfaceType)
if err != nil {
t.Fatalf(`unexpected error: %v"`, err)
}
}
示例4: TestTypeSystem_DefinitionExample_IncludesInterfacesThunkSubtypesInTheTypeMap
func TestTypeSystem_DefinitionExample_IncludesInterfacesThunkSubtypesInTheTypeMap(t *testing.T) {
someInterface := types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
Name: "SomeInterface",
Fields: types.GraphQLFieldConfigMap{
"f": &types.GraphQLFieldConfig{
Type: types.GraphQLInt,
},
},
})
someSubType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "SomeSubtype",
Fields: types.GraphQLFieldConfigMap{
"f": &types.GraphQLFieldConfig{
Type: types.GraphQLInt,
},
},
Interfaces: (types.GraphQLInterfacesThunk)(func() []*types.GraphQLInterfaceType {
return []*types.GraphQLInterfaceType{someInterface}
}),
IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
return true
},
})
schema, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Query",
Fields: types.GraphQLFieldConfigMap{
"iface": &types.GraphQLFieldConfig{
Type: someInterface,
},
},
}),
})
if err != nil {
t.Fatalf("unexpected error, got: %v", err)
}
if schema.GetType("SomeSubtype") != someSubType {
t.Fatalf(`schema.GetType("SomeSubtype") expected to equal someSubType, got: %v`, schema.GetType("SomeSubtype"))
}
}
示例5: schemaWithInterfaceFieldOfType
func schemaWithInterfaceFieldOfType(ttype types.GraphQLType) (types.GraphQLSchema, error) {
badInterfaceType := types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
Name: "BadInterface",
Fields: types.GraphQLFieldConfigMap{
"badField": &types.GraphQLFieldConfig{
Type: ttype,
},
},
})
return types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Query",
Fields: types.GraphQLFieldConfigMap{
"f": &types.GraphQLFieldConfig{
Type: badInterfaceType,
},
},
}),
})
}
示例6: NewNodeDefinitions
/*
Given a function to map from an ID to an underlying object, and a function
to map from an underlying object to the concrete GraphQLObjectType it
corresponds to, constructs a `Node` interface that objects can implement,
and a field config for a `node` root field.
If the typeResolver is omitted, object resolution on the interface will be
handled with the `isTypeOf` method on object types, as with any GraphQL
interface without a provided `resolveType` method.
*/
func NewNodeDefinitions(config NodeDefinitionsConfig) *NodeDefinitions {
nodeInterface := types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
Name: "Node",
Description: "An object with an ID",
Fields: types.GraphQLFieldConfigMap{
"id": &types.GraphQLFieldConfig{
Type: types.NewGraphQLNonNull(types.GraphQLID),
Description: "The id of the object",
},
},
ResolveType: config.TypeResolve,
})
nodeField := &types.GraphQLFieldConfig{
Name: "Node",
Description: "Fetches an object given its ID",
Type: nodeInterface,
Args: types.GraphQLFieldConfigArgumentMap{
"id": &types.GraphQLArgumentConfig{
Type: types.NewGraphQLNonNull(types.GraphQLID),
Description: "The ID of an object",
},
},
Resolve: func(p types.GQLFRParams) interface{} {
if config.IdFetcher == nil {
return nil
}
id := ""
if iid, ok := p.Args["id"]; ok {
id = fmt.Sprintf("%v", iid)
}
fetchedId := config.IdFetcher(id, p.Info)
return fetchedId
},
}
return &NodeDefinitions{
NodeInterface: nodeInterface,
NodeField: nodeField,
}
}
示例7:
Name: "Mutation",
Fields: types.GraphQLFieldConfigMap{
"writeArticle": &types.GraphQLFieldConfig{
Type: blogArticle,
},
},
})
var objectType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Object",
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",
示例8: init
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)
//.........这里部分代码省略.........
示例9: TestUnionIntersectionTypes_GetsExecutionInfoInResolver
func TestUnionIntersectionTypes_GetsExecutionInfoInResolver(t *testing.T) {
var encounteredSchema *types.GraphQLSchema
var encounteredRootValue interface{}
var personType2 *types.GraphQLObjectType
namedType2 := types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
Name: "Named",
Fields: types.GraphQLFieldConfigMap{
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
encounteredSchema = &info.Schema
encounteredRootValue = info.RootValue
return personType2
},
})
personType2 = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Person",
Interfaces: []*types.GraphQLInterfaceType{
namedType2,
},
Fields: types.GraphQLFieldConfigMap{
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
"friends": &types.GraphQLFieldConfig{
Type: types.NewGraphQLList(namedType2),
},
},
})
schema2, _ := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: personType2,
})
john2 := &testPerson{
Name: "John",
Friends: []testNamedType{
liz,
},
}
doc := `{ name, friends { name } }`
expected := &types.GraphQLResult{
Data: map[string]interface{}{
"name": "John",
"friends": []interface{}{
map[string]interface{}{
"name": "Liz",
},
},
},
}
// parse query
ast := testutil.Parse(t, doc)
// execute
ep := executor.ExecuteParams{
Schema: schema2,
AST: ast,
Root: john2,
}
result := testutil.Execute(t, ep)
if len(result.Errors) != len(expected.Errors) {
t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
示例10:
type testCat2 struct {
Name string `json:"name"`
Meows bool `json:"meows"`
}
type testPerson struct {
Name string `json:"name"`
Pets []testPet `json:"pets"`
Friends []testNamedType `json:"friends"`
}
var namedType = types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
Name: "Named",
Fields: types.GraphQLFieldConfigMap{
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
})
var dogType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Dog",
Interfaces: []*types.GraphQLInterfaceType{
namedType,
},
Fields: types.GraphQLFieldConfigMap{
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
"barks": &types.GraphQLFieldConfig{
Type: types.GraphQLBoolean,
},
示例11: TestResolveTypeOnInterfaceYieldsUsefulError
func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) {
var dogType *types.GraphQLObjectType
var catType *types.GraphQLObjectType
var humanType *types.GraphQLObjectType
petType := types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
Name: "Pet",
Fields: types.GraphQLFieldConfigMap{
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
if _, ok := value.(*testCat); ok {
return catType
}
if _, ok := value.(*testDog); ok {
return dogType
}
if _, ok := value.(*testHuman); ok {
return humanType
}
return nil
},
})
humanType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Human",
Fields: types.GraphQLFieldConfigMap{
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
Resolve: func(p types.GQLFRParams) interface{} {
if human, ok := p.Source.(*testHuman); ok {
return human.Name
}
return nil
},
},
},
})
dogType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Dog",
Interfaces: []*types.GraphQLInterfaceType{
petType,
},
IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
_, ok := value.(*testDog)
return ok
},
Fields: types.GraphQLFieldConfigMap{
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
Resolve: func(p types.GQLFRParams) interface{} {
if dog, ok := p.Source.(*testDog); ok {
return dog.Name
}
return nil
},
},
"woofs": &types.GraphQLFieldConfig{
Type: types.GraphQLBoolean,
Resolve: func(p types.GQLFRParams) interface{} {
if dog, ok := p.Source.(*testDog); ok {
return dog.Woofs
}
return nil
},
},
},
})
catType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Cat",
Interfaces: []*types.GraphQLInterfaceType{
petType,
},
IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
_, ok := value.(*testCat)
return ok
},
Fields: types.GraphQLFieldConfigMap{
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
Resolve: func(p types.GQLFRParams) interface{} {
if cat, ok := p.Source.(*testCat); ok {
return cat.Name
}
return nil
},
},
"meows": &types.GraphQLFieldConfig{
Type: types.GraphQLBoolean,
Resolve: func(p types.GQLFRParams) interface{} {
if cat, ok := p.Source.(*testCat); ok {
return cat.Meows
}
return nil
},
},
},
})
//.........这里部分代码省略.........
示例12: TestIsTypeOfUsedToResolveRuntimeTypeForInterface
func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) {
petType := types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
Name: "Pet",
Fields: types.GraphQLFieldConfigMap{
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
})
// ie declare that Dog belongs to Pet interface
_ = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Dog",
Interfaces: []*types.GraphQLInterfaceType{
petType,
},
IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
_, ok := value.(*testDog)
return ok
},
Fields: types.GraphQLFieldConfigMap{
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
Resolve: func(p types.GQLFRParams) interface{} {
if dog, ok := p.Source.(*testDog); ok {
return dog.Name
}
return nil
},
},
"woofs": &types.GraphQLFieldConfig{
Type: types.GraphQLBoolean,
Resolve: func(p types.GQLFRParams) interface{} {
if dog, ok := p.Source.(*testDog); ok {
return dog.Woofs
}
return nil
},
},
},
})
// ie declare that Cat belongs to Pet interface
_ = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Cat",
Interfaces: []*types.GraphQLInterfaceType{
petType,
},
IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
_, ok := value.(*testCat)
return ok
},
Fields: types.GraphQLFieldConfigMap{
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
Resolve: func(p types.GQLFRParams) interface{} {
if cat, ok := p.Source.(*testCat); ok {
return cat.Name
}
return nil
},
},
"meows": &types.GraphQLFieldConfig{
Type: types.GraphQLBoolean,
Resolve: func(p types.GQLFRParams) interface{} {
if cat, ok := p.Source.(*testCat); ok {
return cat.Meows
}
return nil
},
},
},
})
schema, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Query",
Fields: types.GraphQLFieldConfigMap{
"pets": &types.GraphQLFieldConfig{
Type: types.NewGraphQLList(petType),
Resolve: func(p types.GQLFRParams) interface{} {
return []interface{}{
&testDog{"Odie", true},
&testCat{"Garfield", false},
}
},
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}
query := `{
pets {
name
... on Dog {
woofs
}
... on Cat {
//.........这里部分代码省略.........