本文整理匯總了Golang中github.com/chris-ramon/graphql-go/types.NewGraphQLNonNull函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewGraphQLNonNull函數的具體用法?Golang NewGraphQLNonNull怎麽用?Golang NewGraphQLNonNull使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewGraphQLNonNull函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: init
func init() {
nodeTestUserType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "User",
Fields: types.GraphQLFieldConfigMap{
"id": &types.GraphQLFieldConfig{
Type: types.NewGraphQLNonNull(types.GraphQLID),
},
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
Interfaces: []*types.GraphQLInterfaceType{nodeTestDef.NodeInterface},
})
nodeTestPhotoType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Photo",
Fields: types.GraphQLFieldConfigMap{
"id": &types.GraphQLFieldConfig{
Type: types.NewGraphQLNonNull(types.GraphQLID),
},
"width": &types.GraphQLFieldConfig{
Type: types.GraphQLInt,
},
},
Interfaces: []*types.GraphQLInterfaceType{nodeTestDef.NodeInterface},
})
nodeTestSchema, _ = types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: nodeTestQueryType,
})
}
示例2: PluralIdentifyingRootField
func PluralIdentifyingRootField(config PluralIdentifyingRootFieldConfig) *types.GraphQLFieldConfig {
inputArgs := types.GraphQLFieldConfigArgumentMap{}
if config.ArgName != "" {
inputArgs[config.ArgName] = &types.GraphQLArgumentConfig{
Type: types.NewGraphQLNonNull(types.NewGraphQLList(types.NewGraphQLNonNull(config.InputType))),
}
}
return &types.GraphQLFieldConfig{
Description: config.Description,
Type: types.NewGraphQLList(config.OutputType),
Args: inputArgs,
Resolve: func(p types.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: TestLists_NonNullListOfNonNullFunc_ReturnsNull
func TestLists_NonNullListOfNonNullFunc_ReturnsNull(t *testing.T) {
ttype := types.NewGraphQLNonNull(types.NewGraphQLList(types.NewGraphQLNonNull(types.GraphQLInt)))
// `data` is a function that return values
// Note that its uses the expected signature `func() interface{} {...}`
data := func() interface{} {
return nil
}
expected := &types.GraphQLResult{
Data: map[string]interface{}{
"nest": nil,
},
Errors: []graphqlerrors.GraphQLFormattedError{
graphqlerrors.GraphQLFormattedError{
Message: "Cannot return null for non-nullable field DataType.test.",
Locations: []location.SourceLocation{
location.SourceLocation{
Line: 1,
Column: 10,
},
},
},
},
}
checkList(t, ttype, data, expected)
}
示例4: TestLists_NonNullListOfNonNullArrayOfFunc_ContainsNulls
func TestLists_NonNullListOfNonNullArrayOfFunc_ContainsNulls(t *testing.T) {
ttype := types.NewGraphQLNonNull(types.NewGraphQLList(types.NewGraphQLNonNull(types.GraphQLInt)))
// `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 := &types.GraphQLResult{
Data: map[string]interface{}{
"nest": map[string]interface{}{
"test": []interface{}{
1, nil, 2,
},
},
},
}
checkList(t, ttype, data, expected)
}
示例5: TestTypeSystem_DefinitionExample_ProhibitsNestingNonNullInsideNonNull
func TestTypeSystem_DefinitionExample_ProhibitsNestingNonNullInsideNonNull(t *testing.T) {
ttype := types.NewGraphQLNonNull(types.NewGraphQLNonNull(types.GraphQLInt))
expected := `Can only create NonNull of a Nullable GraphQLType but got: Int!.`
if ttype.GetError().Error() != expected {
t.Fatalf(`expected %v , got: %v`, expected, ttype.GetError())
}
}
示例6: TestTypeSystem_DefinitionExample_StringifiesSimpleTypes
func TestTypeSystem_DefinitionExample_StringifiesSimpleTypes(t *testing.T) {
type Test struct {
ttype types.GraphQLType
expected string
}
tests := []Test{
Test{types.GraphQLInt, "Int"},
Test{blogArticle, "Article"},
Test{interfaceType, "Interface"},
Test{unionType, "Union"},
Test{enumType, "Enum"},
Test{inputObjectType, "InputObject"},
Test{types.NewGraphQLNonNull(types.GraphQLInt), "Int!"},
Test{types.NewGraphQLList(types.GraphQLInt), "[Int]"},
Test{types.NewGraphQLNonNull(types.NewGraphQLList(types.GraphQLInt)), "[Int]!"},
Test{types.NewGraphQLList(types.NewGraphQLNonNull(types.GraphQLInt)), "[Int!]"},
Test{types.NewGraphQLList(types.NewGraphQLList(types.GraphQLInt)), "[[Int]]"},
}
for _, test := range tests {
ttypeStr := fmt.Sprintf("%v", test.ttype)
if ttypeStr != test.expected {
t.Fatalf(`expected %v , got: %v`, test.expected, ttypeStr)
}
}
}
示例7:
func TestTypeSystem_ObjectsMustAdhereToInterfaceTheyImplement_AcceptsAnObjectWithAnEquivalentlyModifiedInterfaceField(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.NewGraphQLNonNull(types.NewGraphQLList(types.GraphQLString)),
},
},
})
anotherObject := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "AnotherObject",
Interfaces: []*types.GraphQLInterfaceType{anotherInterface},
Fields: types.GraphQLFieldConfigMap{
"field": &types.GraphQLFieldConfig{
Type: types.NewGraphQLNonNull(types.NewGraphQLList(types.GraphQLString)),
},
},
})
_, err := schemaWithObjectFieldOfType(anotherObject)
if err != nil {
t.Fatalf(`unexpected error: %v for type "%v"`, err, anotherObject)
}
}
示例8: withModifiers
func withModifiers(ttypes []types.GraphQLType) []types.GraphQLType {
res := ttypes
for _, ttype := range ttypes {
res = append(res, types.NewGraphQLList(ttype))
}
for _, ttype := range ttypes {
res = append(res, types.NewGraphQLNonNull(ttype))
}
for _, ttype := range ttypes {
res = append(res, types.NewGraphQLNonNull(types.NewGraphQLList(ttype)))
}
return res
}
示例9: MutationWithClientMutationId
func MutationWithClientMutationId(config MutationConfig) *types.GraphQLFieldConfig {
augmentedInputFields := config.InputFields
if augmentedInputFields == nil {
augmentedInputFields = types.InputObjectConfigFieldMap{}
}
augmentedInputFields["clientMutationId"] = &types.InputObjectFieldConfig{
Type: types.NewGraphQLNonNull(types.GraphQLString),
}
augmentedOutputFields := config.OutputFields
if augmentedOutputFields == nil {
augmentedOutputFields = types.GraphQLFieldConfigMap{}
}
augmentedOutputFields["clientMutationId"] = &types.GraphQLFieldConfig{
Type: types.NewGraphQLNonNull(types.GraphQLString),
}
inputType := types.NewGraphQLInputObjectType(types.InputObjectConfig{
Name: config.Name + "Input",
Fields: augmentedInputFields,
})
outputType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: config.Name + "Payload",
Fields: augmentedOutputFields,
})
return &types.GraphQLFieldConfig{
Type: outputType,
Args: types.GraphQLFieldConfigArgumentMap{
"input": &types.GraphQLArgumentConfig{
Type: types.NewGraphQLNonNull(inputType),
},
},
Resolve: func(p types.GQLFRParams) interface{} {
if config.MutateAndGetPayload == nil {
return nil
}
input := map[string]interface{}{}
if inputVal, ok := p.Args["input"]; ok {
if inputVal, ok := inputVal.(map[string]interface{}); ok {
input = inputVal
}
}
payload := config.MutateAndGetPayload(input, p.Info)
if clientMutationId, ok := input["clientMutationId"]; ok {
payload["clientMutationId"] = clientMutationId
}
return payload
},
}
}
示例10: GlobalIdField
/*
Creates the configuration for an id field on a node, using `toGlobalId` to
construct the ID from the provided typename. The type-specific ID is fetcher
by calling idFetcher on the object, or if not provided, by accessing the `id`
property on the object.
*/
func GlobalIdField(typeName string, idFetcher GlobalIdFetcherFn) *types.GraphQLFieldConfig {
return &types.GraphQLFieldConfig{
Name: "id",
Description: "The ID of an object",
Type: types.NewGraphQLNonNull(types.GraphQLID),
Resolve: func(p types.GQLFRParams) interface{} {
id := ""
if idFetcher != nil {
fetched := idFetcher(p.Source, p.Info)
id = fmt.Sprintf("%v", fetched)
} else {
// try to get from p.Source (data)
var objMap interface{}
b, _ := json.Marshal(p.Source)
_ = json.Unmarshal(b, &objMap)
switch obj := objMap.(type) {
case map[string]interface{}:
if iid, ok := obj["id"]; ok {
id = fmt.Sprintf("%v", iid)
}
}
}
globalId := ToGlobalId(typeName, id)
return globalId
},
}
}
示例11: TestLists_NullableListOfNonNullObjects_ContainsNull
func TestLists_NullableListOfNonNullObjects_ContainsNull(t *testing.T) {
ttype := types.NewGraphQLList(types.NewGraphQLNonNull(types.GraphQLInt))
data := []interface{}{
1, nil, 2,
}
expected := &types.GraphQLResult{
Data: map[string]interface{}{
"nest": map[string]interface{}{
"test": nil,
},
},
Errors: []graphqlerrors.GraphQLFormattedError{
graphqlerrors.GraphQLFormattedError{
Message: "Cannot return null for non-nullable field DataType.test.",
Locations: []location.SourceLocation{
location.SourceLocation{
Line: 1,
Column: 10,
},
},
},
},
}
checkList(t, ttype, data, expected)
}
示例12: TestTypeSystem_NonNullMustAcceptGraphQLTypes_RejectsNilAsNonNullableType
func TestTypeSystem_NonNullMustAcceptGraphQLTypes_RejectsNilAsNonNullableType(t *testing.T) {
result := types.NewGraphQLNonNull(nil)
expectedError := `Can only create NonNull of a Nullable GraphQLType but got: <nil>.`
if result.GetError() == nil || result.GetError().Error() != expectedError {
t.Fatalf("Expected error: %v, got %v", expectedError, result.GetError())
}
}
示例13: TestTypeSystem_DefinitionExample_IdentifiesOutputTypes
func TestTypeSystem_DefinitionExample_IdentifiesOutputTypes(t *testing.T) {
type Test struct {
ttype types.GraphQLType
expected bool
}
tests := []Test{
Test{types.GraphQLInt, true},
Test{objectType, true},
Test{interfaceType, true},
Test{unionType, true},
Test{enumType, true},
Test{inputObjectType, false},
}
for _, test := range tests {
ttypeStr := fmt.Sprintf("%v", test.ttype)
if types.IsOutputType(test.ttype) != test.expected {
t.Fatalf(`expected %v , got: %v`, test.expected, ttypeStr)
}
if types.IsOutputType(types.NewGraphQLList(test.ttype)) != test.expected {
t.Fatalf(`expected %v , got: %v`, test.expected, ttypeStr)
}
if types.IsOutputType(types.NewGraphQLNonNull(test.ttype)) != test.expected {
t.Fatalf(`expected %v , got: %v`, test.expected, ttypeStr)
}
}
}
示例14: TestLists_NonNullListOfNonNullObjects_ContainsValues
// Describe [T!]! Array<T>
func TestLists_NonNullListOfNonNullObjects_ContainsValues(t *testing.T) {
ttype := types.NewGraphQLNonNull(types.NewGraphQLList(types.NewGraphQLNonNull(types.GraphQLInt)))
data := []interface{}{
1, 2,
}
expected := &types.GraphQLResult{
Data: map[string]interface{}{
"nest": map[string]interface{}{
"test": []interface{}{
1, 2,
},
},
},
}
checkList(t, ttype, data, expected)
}
示例15: ConnectionDefinitions
func ConnectionDefinitions(config ConnectionConfig) *GraphQLConnectionDefinitions {
edgeType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: config.Name + "Edge",
Description: "An edge in a connection",
Fields: types.GraphQLFieldConfigMap{
"node": &types.GraphQLFieldConfig{
Type: config.NodeType,
Description: "The item at the end of the edge",
},
"cursor": &types.GraphQLFieldConfig{
Type: types.NewGraphQLNonNull(types.GraphQLString),
Description: " cursor for use in pagination",
},
},
})
for fieldName, fieldConfig := range config.EdgeFields {
edgeType.AddFieldConfig(fieldName, fieldConfig)
}
connectionType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: config.Name + "Connection",
Description: "A connection to a list of items.",
Fields: types.GraphQLFieldConfigMap{
"pageInfo": &types.GraphQLFieldConfig{
Type: types.NewGraphQLNonNull(pageInfoType),
Description: "Information to aid in pagination.",
},
"edges": &types.GraphQLFieldConfig{
Type: types.NewGraphQLList(edgeType),
Description: "Information to aid in pagination.",
},
},
})
for fieldName, fieldConfig := range config.ConnectionFields {
connectionType.AddFieldConfig(fieldName, fieldConfig)
}
return &GraphQLConnectionDefinitions{
EdgeType: edgeType,
ConnectionType: connectionType,
}
}