本文整理汇总了Golang中github.com/graphql-go/graphql.NewEnum函数的典型用法代码示例。如果您正苦于以下问题:Golang NewEnum函数的具体用法?Golang NewEnum怎么用?Golang NewEnum使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewEnum函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestTypeSystem_EnumTypesMustBeWellDefined_RejectsAnEnumTypeWithoutValues
func TestTypeSystem_EnumTypesMustBeWellDefined_RejectsAnEnumTypeWithoutValues(t *testing.T) {
_, err := schemaWithFieldType(graphql.NewEnum(graphql.EnumConfig{
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)
}
}
示例2: TestTypeSystem_EnumTypesMustBeWellDefined_AcceptsAWellDefinedEnumTypeWithEmptyValueDefinition
func TestTypeSystem_EnumTypesMustBeWellDefined_AcceptsAWellDefinedEnumTypeWithEmptyValueDefinition(t *testing.T) {
_, err := schemaWithFieldType(graphql.NewEnum(graphql.EnumConfig{
Name: "SomeEnum",
Values: graphql.EnumValueConfigMap{
"FOO": &graphql.EnumValueConfig{},
"BAR": &graphql.EnumValueConfig{},
},
}))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
示例3: TestIntrospection_RespectsTheIncludeDeprecatedParameterForEnumValues
func TestIntrospection_RespectsTheIncludeDeprecatedParameterForEnumValues(t *testing.T) {
testEnum := graphql.NewEnum(graphql.EnumConfig{
Name: "TestEnum",
Values: graphql.EnumValueConfigMap{
"NONDEPRECATED": &graphql.EnumValueConfig{
Value: 0,
},
"DEPRECATED": &graphql.EnumValueConfig{
Value: 1,
DeprecationReason: "Removed in 1.0",
},
"ALSONONDEPRECATED": &graphql.EnumValueConfig{
Value: 2,
},
},
})
testType := graphql.NewObject(graphql.ObjectConfig{
Name: "TestType",
Fields: graphql.Fields{
"testEnum": &graphql.Field{
Type: testEnum,
},
},
})
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: testType,
})
if err != nil {
t.Fatalf("Error creating Schema: %v", err.Error())
}
query := `
{
__type(name: "TestEnum") {
name
trueValues: enumValues(includeDeprecated: true) {
name
}
falseValues: enumValues(includeDeprecated: false) {
name
}
omittedValues: enumValues {
name
}
}
}
`
expected := &graphql.Result{
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 := g(t, graphql.Params{
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))
}
}
示例4: withModifiers
},
})
var someInterfaceType = graphql.NewInterface(graphql.InterfaceConfig{
Name: "SomeInterface",
ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object {
return nil
},
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: graphql.String,
},
},
})
var someEnumType = graphql.NewEnum(graphql.EnumConfig{
Name: "SomeEnum",
Values: graphql.EnumValueConfigMap{
"ONLY": &graphql.EnumValueConfig{},
},
})
var someInputObject = graphql.NewInputObject(graphql.InputObjectConfig{
Name: "SomeInputObject",
Fields: graphql.InputObjectConfigFieldMap{
"f": &graphql.InputObjectFieldConfig{
Type: graphql.String,
DefaultValue: "Hello",
},
},
})
func withModifiers(ttypes []graphql.Type) []graphql.Type {
res := ttypes
for _, ttype := range ttypes {
示例5: 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 := graphql.NewEnum(graphql.EnumConfig{
Name: "Episode",
Description: "One of the films in the Star Wars Trilogy",
Values: graphql.EnumValueConfigMap{
"NEWHOPE": &graphql.EnumValueConfig{
Value: 4,
Description: "Released in 1977.",
},
"EMPIRE": &graphql.EnumValueConfig{
Value: 5,
Description: "Released in 1980.",
},
"JEDI": &graphql.EnumValueConfig{
Value: 6,
Description: "Released in 1983.",
},
},
})
characterInterface := graphql.NewInterface(graphql.InterfaceConfig{
Name: "Character",
Description: "A character in the Star Wars Trilogy",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Description: "The id of the character.",
},
"name": &graphql.Field{
Type: graphql.String,
Description: "The name of the character.",
},
"appearsIn": &graphql.Field{
Type: graphql.NewList(episodeEnum),
Description: "Which movies they appear in.",
},
},
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
if character, ok := p.Value.(StarWarsChar); ok {
id, _ := strconv.Atoi(character.ID)
human := GetHuman(id)
//.........这里部分代码省略.........
示例6:
import (
"reflect"
"testing"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/graphql/testutil"
)
var enumTypeTestColorType = graphql.NewEnum(graphql.EnumConfig{
Name: "Color",
Values: graphql.EnumValueConfigMap{
"RED": &graphql.EnumValueConfig{
Value: 0,
},
"GREEN": &graphql.EnumValueConfig{
Value: 1,
},
"BLUE": &graphql.EnumValueConfig{
Value: 2,
},
},
})
var enumTypeTestQueryType = graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"colorEnum": &graphql.Field{
Type: enumTypeTestColorType,
Args: graphql.FieldConfigArgument{
"fromEnum": &graphql.ArgumentConfig{
Type: enumTypeTestColorType,
},
示例7: init
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
return true
},
})
var interfaceType = graphql.NewInterface(graphql.InterfaceConfig{
Name: "Interface",
})
var unionType = graphql.NewUnion(graphql.UnionConfig{
Name: "Union",
Types: []*graphql.Object{
objectType,
},
})
var enumType = graphql.NewEnum(graphql.EnumConfig{
Name: "Enum",
Values: graphql.EnumValueConfigMap{
"foo": &graphql.EnumValueConfig{},
},
})
var inputObjectType = graphql.NewInputObject(graphql.InputObjectConfig{
Name: "InputObject",
})
func init() {
blogAuthor.AddFieldConfig("recentArticle", &graphql.Field{
Type: blogArticle,
})
}
func TestTypeSystem_DefinitionExample_DefinesAQueryOnlySchema(t *testing.T) {
blogSchema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: blogQuery,
示例8: init
func init() {
var beingInterface = graphql.NewInterface(graphql.InterfaceConfig{
Name: "Being",
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
"surname": &graphql.ArgumentConfig{
Type: graphql.Boolean,
},
},
},
},
})
var petInterface = graphql.NewInterface(graphql.InterfaceConfig{
Name: "Pet",
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
"surname": &graphql.ArgumentConfig{
Type: graphql.Boolean,
},
},
},
},
})
var dogCommandEnum = graphql.NewEnum(graphql.EnumConfig{
Name: "DogCommand",
Values: graphql.EnumValueConfigMap{
"SIT": &graphql.EnumValueConfig{
Value: 0,
},
"HEEL": &graphql.EnumValueConfig{
Value: 1,
},
"DOWN": &graphql.EnumValueConfig{
Value: 2,
},
},
})
var dogType = graphql.NewObject(graphql.ObjectConfig{
Name: "Dog",
IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool {
return true
},
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
"surname": &graphql.ArgumentConfig{
Type: graphql.Boolean,
},
},
},
"nickname": &graphql.Field{
Type: graphql.String,
},
"barkVolume": &graphql.Field{
Type: graphql.Int,
},
"barks": &graphql.Field{
Type: graphql.Boolean,
},
"doesKnowCommand": &graphql.Field{
Type: graphql.Boolean,
Args: graphql.FieldConfigArgument{
"dogCommand": &graphql.ArgumentConfig{
Type: dogCommandEnum,
},
},
},
"isHousetrained": &graphql.Field{
Type: graphql.Boolean,
Args: graphql.FieldConfigArgument{
"atOtherHomes": &graphql.ArgumentConfig{
Type: graphql.Boolean,
DefaultValue: true,
},
},
},
"isAtLocation": &graphql.Field{
Type: graphql.Boolean,
Args: graphql.FieldConfigArgument{
"x": &graphql.ArgumentConfig{
Type: graphql.Int,
},
"y": &graphql.ArgumentConfig{
Type: graphql.Int,
},
},
},
},
Interfaces: []*graphql.Interface{
beingInterface,
petInterface,
},
})
var furColorEnum = graphql.NewEnum(graphql.EnumConfig{
//.........这里部分代码省略.........
示例9:
package main
import (
"fmt"
"net/http"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql-go-handler"
)
var vehicleStateEnum = graphql.NewEnum(graphql.EnumConfig{
Name: "VehicleStatus",
Values: graphql.EnumValueConfigMap{
"FREE": &graphql.EnumValueConfig{Value: "FREE", Description: "The vehicle is free for usage"},
"RESERVED": &graphql.EnumValueConfig{Value: "RESERVED", Description: "The vehicle is reserved"},
},
})
var vehicleType = graphql.NewObject(graphql.ObjectConfig{
Name: "Vehicle",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.String, Description: "Vehicle id"},
"name": &graphql.Field{Type: graphql.String, Description: "Vehicle name"},
"state": &graphql.Field{Type: vehicleStateEnum, Description: "true=FREE, false=RESERVED"},
},
})
var queryType = graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"Vehicle": &graphql.Field{