本文整理汇总了Golang中github.com/graphql-go/graphql.NewList函数的典型用法代码示例。如果您正苦于以下问题:Golang NewList函数的具体用法?Golang NewList怎么用?Golang NewList使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewList函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestTypeSystem_DefinitionExample_StringifiesSimpleTypes
func TestTypeSystem_DefinitionExample_StringifiesSimpleTypes(t *testing.T) {
type Test struct {
ttype graphql.Type
expected string
}
tests := []Test{
{graphql.Int, "Int"},
{blogArticle, "Article"},
{interfaceType, "Interface"},
{unionType, "Union"},
{enumType, "Enum"},
{inputObjectType, "InputObject"},
{graphql.NewNonNull(graphql.Int), "Int!"},
{graphql.NewList(graphql.Int), "[Int]"},
{graphql.NewNonNull(graphql.NewList(graphql.Int)), "[Int]!"},
{graphql.NewList(graphql.NewNonNull(graphql.Int)), "[Int!]"},
{graphql.NewList(graphql.NewList(graphql.Int)), "[[Int]]"},
}
for _, test := range tests {
ttypeStr := fmt.Sprintf("%v", test.ttype)
if ttypeStr != test.expected {
t.Fatalf(`expected %v , got: %v`, test.expected, ttypeStr)
}
}
}
示例2: PluralIdentifyingRootField
func PluralIdentifyingRootField(config PluralIdentifyingRootFieldConfig) *graphql.FieldConfig {
inputArgs := graphql.FieldConfigArgument{}
if config.ArgName != "" {
inputArgs[config.ArgName] = &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(config.InputType))),
}
}
return &graphql.FieldConfig{
Description: config.Description,
Type: graphql.NewList(config.OutputType),
Args: inputArgs,
Resolve: func(p graphql.GQLFRParams) interface{} {
inputs, ok := p.Args[config.ArgName]
if !ok {
return nil
}
if config.ResolveSingleInput == nil {
return nil
}
switch inputs := inputs.(type) {
case []interface{}:
res := []interface{}{}
for _, input := range inputs {
r := config.ResolveSingleInput(input)
res = append(res, r)
}
return res
}
return nil
},
}
}
示例3:
func TestTypeSystem_ObjectsMustAdhereToInterfaceTheyImplement_AcceptsAnObjectWithAnEquivalentlyModifiedInterfaceField(t *testing.T) {
anotherInterface := graphql.NewInterface(graphql.InterfaceConfig{
Name: "AnotherInterface",
ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object {
return nil
},
Fields: graphql.FieldConfigMap{
"field": &graphql.FieldConfig{
Type: graphql.NewNonNull(graphql.NewList(graphql.String)),
},
},
})
anotherObject := graphql.NewObject(graphql.ObjectConfig{
Name: "AnotherObject",
Interfaces: []*graphql.Interface{anotherInterface},
Fields: graphql.FieldConfigMap{
"field": &graphql.FieldConfig{
Type: graphql.NewNonNull(graphql.NewList(graphql.String)),
},
},
})
_, err := schemaWithObjectFieldOfType(anotherObject)
if err != nil {
t.Fatalf(`unexpected error: %v for type "%v"`, err, anotherObject)
}
}
示例4: PluralIdentifyingRootField
func PluralIdentifyingRootField(config PluralIdentifyingRootFieldConfig) *graphql.Field {
inputArgs := graphql.FieldConfigArgument{}
if config.ArgName != "" {
inputArgs[config.ArgName] = &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(config.InputType))),
}
}
return &graphql.Field{
Description: config.Description,
Type: graphql.NewList(config.OutputType),
Args: inputArgs,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
inputs, ok := p.Args[config.ArgName]
if !ok {
return nil, fmt.Errorf("Missing arg %q", config.ArgName)
}
if config.ResolveSingleInput == nil {
return nil, nil
}
switch inputs := inputs.(type) {
case []interface{}:
res := []interface{}{}
for _, input := range inputs {
r := config.ResolveSingleInput(input)
res = append(res, r)
}
return res, nil
}
return nil, fmt.Errorf("Can't handle %T", inputs)
},
}
}
示例5: withModifiers
func withModifiers(ttypes []graphql.Type) []graphql.Type {
res := ttypes
for _, ttype := range ttypes {
res = append(res, graphql.NewList(ttype))
}
for _, ttype := range ttypes {
res = append(res, graphql.NewNonNull(ttype))
}
for _, ttype := range ttypes {
res = append(res, graphql.NewNonNull(graphql.NewList(ttype)))
}
return res
}
示例6: TestLists_NullableListOfNonNullObjects_ContainsNull
func TestLists_NullableListOfNonNullObjects_ContainsNull(t *testing.T) {
ttype := graphql.NewList(graphql.NewNonNull(graphql.Int))
data := []interface{}{
1, nil, 2,
}
expected := &graphql.Result{
Data: map[string]interface{}{
"nest": map[string]interface{}{
"test": nil,
},
},
Errors: []gqlerrors.FormattedError{
gqlerrors.FormattedError{
Message: "Cannot return null for non-nullable field DataType.test.",
Locations: []location.SourceLocation{
location.SourceLocation{
Line: 1,
Column: 10,
},
},
},
},
}
checkList(t, ttype, data, expected)
}
示例7: TestLists_NonNullListOfNonNullFunc_ReturnsNull
func TestLists_NonNullListOfNonNullFunc_ReturnsNull(t *testing.T) {
ttype := graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(graphql.Int)))
// `data` is a function that return values
// Note that its uses the expected signature `func() interface{} {...}`
data := func() interface{} {
return nil
}
expected := &graphql.Result{
Data: map[string]interface{}{
"nest": nil,
},
Errors: []gqlerrors.FormattedError{
gqlerrors.FormattedError{
Message: "Cannot return null for non-nullable field DataType.test.",
Locations: []location.SourceLocation{
location.SourceLocation{
Line: 1,
Column: 10,
},
},
},
},
}
checkList(t, ttype, data, expected)
}
示例8:
func TestTypeSystem_ObjectsMustAdhereToInterfaceTheyImplement_RejectsAnObjectWithAListInterfaceFieldNonListType(t *testing.T) {
anotherInterface := graphql.NewInterface(graphql.InterfaceConfig{
Name: "AnotherInterface",
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
return nil
},
Fields: graphql.Fields{
"field": &graphql.Field{
Type: graphql.String,
},
},
})
anotherObject := graphql.NewObject(graphql.ObjectConfig{
Name: "AnotherObject",
Interfaces: []*graphql.Interface{anotherInterface},
Fields: graphql.Fields{
"field": &graphql.Field{
Type: graphql.NewList(graphql.String),
},
},
})
_, err := schemaWithFieldType(anotherObject)
expectedError := `AnotherInterface.field expects type "String" but AnotherObject.field provides type "[String]".`
if err == nil || err.Error() != expectedError {
t.Fatalf("Expected error: %v, got %v", expectedError, err)
}
}
示例9: addConnections
// addConnections adds connections fields to the object afterward to prevent from dead loops
func (t types) addConnections(o *graphql.Object, idx resource.Index, r *resource.Resource) {
// Add sub field references
for name, def := range r.Schema().Fields {
if ref, ok := def.Validator.(*schema.Reference); ok {
sr, found := idx.GetResource(ref.Path, nil)
if !found {
log.Panicf("resource reference not found: %s", ref.Path)
}
o.AddFieldConfig(name, &graphql.Field{
Description: def.Description,
Type: t.getObjectType(idx, sr),
Args: getFArgs(def.Params),
Resolve: getSubFieldResolver(name, sr, def),
})
}
}
// Add sub resources
for _, sr := range r.GetResources() {
name := sr.Name()
o.AddFieldConfig(name, &graphql.Field{
Description: fmt.Sprintf("Connection to %s", name),
Type: graphql.NewList(t.getObjectType(idx, sr)),
Args: listArgs,
Resolve: getSubResourceResolver(sr),
})
}
}
示例10: TestTypeSystem_ListMustAcceptGraphQLTypes_RejectsANilTypeAsItemTypeOfList
func TestTypeSystem_ListMustAcceptGraphQLTypes_RejectsANilTypeAsItemTypeOfList(t *testing.T) {
result := graphql.NewList(nil)
expectedError := `Can only create List of a Type but got: <nil>.`
if result.GetError() == nil || result.GetError().Error() != expectedError {
t.Fatalf("Expected error: %v, got %v", expectedError, result.GetError())
}
}
示例11: TestLists_NonNullListOfNonNullArrayOfFunc_ContainsNulls
func TestLists_NonNullListOfNonNullArrayOfFunc_ContainsNulls(t *testing.T) {
ttype := graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(graphql.Int)))
// `data` is a slice of functions that return values
// Note that its uses the expected signature `func() interface{} {...}`
data := []interface{}{
func() interface{} {
return 1
},
func() interface{} {
return nil
},
func() interface{} {
return 2
},
}
expected := &graphql.Result{
Data: map[string]interface{}{
"nest": map[string]interface{}{
"test": []interface{}{
1, nil, 2,
},
},
},
}
checkList(t, ttype, data, expected)
}
示例12: TestTypeSystem_DefinitionExample_IdentifiesOutputTypes
func TestTypeSystem_DefinitionExample_IdentifiesOutputTypes(t *testing.T) {
type Test struct {
ttype graphql.Type
expected bool
}
tests := []Test{
{graphql.Int, true},
{objectType, true},
{interfaceType, true},
{unionType, true},
{enumType, true},
{inputObjectType, false},
}
for _, test := range tests {
ttypeStr := fmt.Sprintf("%v", test.ttype)
if graphql.IsOutputType(test.ttype) != test.expected {
t.Fatalf(`expected %v , got: %v`, test.expected, ttypeStr)
}
if graphql.IsOutputType(graphql.NewList(test.ttype)) != test.expected {
t.Fatalf(`expected %v , got: %v`, test.expected, ttypeStr)
}
if graphql.IsOutputType(graphql.NewNonNull(test.ttype)) != test.expected {
t.Fatalf(`expected %v , got: %v`, test.expected, ttypeStr)
}
}
}
示例13: TestTypeSystem_NonNullMustAcceptGraphQLTypes_AcceptsAnTypeAsNullableTypeOfNonNull
func TestTypeSystem_NonNullMustAcceptGraphQLTypes_AcceptsAnTypeAsNullableTypeOfNonNull(t *testing.T) {
nullableTypes := []graphql.Type{
graphql.String,
someScalarType,
someObjectType,
someUnionType,
someInterfaceType,
someEnumType,
someInputObject,
graphql.NewList(graphql.String),
graphql.NewList(graphql.NewNonNull(graphql.String)),
}
for _, ttype := range nullableTypes {
result := graphql.NewNonNull(ttype)
if result.GetError() != nil {
t.Fatalf(`unexpected error: %v for type "%v"`, result.GetError(), ttype)
}
}
}
示例14: TestLists_ListOfNullableObjects_ReturnsNull
func TestLists_ListOfNullableObjects_ReturnsNull(t *testing.T) {
ttype := graphql.NewList(graphql.Int)
expected := &graphql.Result{
Data: map[string]interface{}{
"nest": map[string]interface{}{
"test": nil,
},
},
}
checkList(t, ttype, nil, expected)
}
示例15: TestLists_NonNullListOfNonNullObjects_ContainsValues
// Describe [T!]! Array<T>
func TestLists_NonNullListOfNonNullObjects_ContainsValues(t *testing.T) {
ttype := graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(graphql.Int)))
data := []interface{}{
1, 2,
}
expected := &graphql.Result{
Data: map[string]interface{}{
"nest": map[string]interface{}{
"test": []interface{}{
1, 2,
},
},
},
}
checkList(t, ttype, data, expected)
}