本文整理匯總了Golang中github.com/chris-ramon/graphql.NewObject函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewObject函數的具體用法?Golang NewObject怎麽用?Golang NewObject使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewObject函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: schemaWithArgOfType
func schemaWithArgOfType(ttype graphql.Type) (graphql.Schema, error) {
badObject := graphql.NewObject(graphql.ObjectConfig{
Name: "BadObject",
Fields: graphql.FieldConfigMap{
"badField": &graphql.FieldConfig{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
"badArg": &graphql.ArgumentConfig{
Type: ttype,
},
},
},
},
})
return graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: badObject,
},
},
}),
})
}
示例2: TestUsesTheMutationSchemaForMutations
func TestUsesTheMutationSchemaForMutations(t *testing.T) {
doc := `query Q { a } mutation M { c }`
data := map[string]interface{}{
"a": "b",
"c": "d",
}
expected := &graphql.Result{
Data: map[string]interface{}{
"c": "d",
},
}
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Q",
Fields: graphql.FieldConfigMap{
"a": &graphql.FieldConfig{
Type: graphql.String,
},
},
}),
Mutation: graphql.NewObject(graphql.ObjectConfig{
Name: "M",
Fields: graphql.FieldConfigMap{
"c": &graphql.FieldConfig{
Type: graphql.String,
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}
// parse query
ast := testutil.TestParse(t, doc)
// execute
ep := graphql.ExecuteParams{
Schema: schema,
AST: ast,
Root: data,
OperationName: "M",
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) > 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
示例3:
func TestTypeSystem_SchemaMustContainUniquelyNamedTypes_RejectsASchemaWhichHaveSameNamedObjectsImplementingAnInterface(t *testing.T) {
anotherInterface := graphql.NewInterface(graphql.InterfaceConfig{
Name: "AnotherInterface",
ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object {
return nil
},
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: graphql.String,
},
},
})
_ = graphql.NewObject(graphql.ObjectConfig{
Name: "BadObject",
Interfaces: []*graphql.Interface{
anotherInterface,
},
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: graphql.String,
},
},
})
_ = graphql.NewObject(graphql.ObjectConfig{
Name: "BadObject",
Interfaces: []*graphql.Interface{
anotherInterface,
},
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: graphql.String,
},
},
})
queryType := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.FieldConfigMap{
"iface": &graphql.FieldConfig{
Type: anotherInterface,
},
},
})
_, err := graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
})
expectedError := `Schema must contain unique named types but contains multiple types named "BadObject".`
if err == nil || err.Error() != expectedError {
t.Fatalf("Expected error: %v, got %v", expectedError, err)
}
}
示例4: TestDoesNotIncludeIllegalFieldsInOutput
func TestDoesNotIncludeIllegalFieldsInOutput(t *testing.T) {
doc := `mutation M {
thisIsIllegalDontIncludeMe
}`
expected := &graphql.Result{
Data: map[string]interface{}{},
}
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Q",
Fields: graphql.FieldConfigMap{
"a": &graphql.FieldConfig{
Type: graphql.String,
},
},
}),
Mutation: graphql.NewObject(graphql.ObjectConfig{
Name: "M",
Fields: graphql.FieldConfigMap{
"c": &graphql.FieldConfig{
Type: graphql.String,
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}
// parse query
ast := testutil.TestParse(t, doc)
// execute
ep := graphql.ExecuteParams{
Schema: schema,
AST: ast,
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) != 0 {
t.Fatalf("wrong result, expected len(%v) errors, got len(%v)", len(expected.Errors), len(result.Errors))
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
示例5: TestTypeSystem_SchemaMustContainUniquelyNamedTypes_RejectsASchemaWhichRedefinesABuiltInType
func TestTypeSystem_SchemaMustContainUniquelyNamedTypes_RejectsASchemaWhichRedefinesABuiltInType(t *testing.T) {
fakeString := graphql.NewScalar(graphql.ScalarConfig{
Name: "String",
Serialize: func(value interface{}) interface{} {
return nil
},
})
queryType := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.FieldConfigMap{
"normal": &graphql.FieldConfig{
Type: graphql.String,
},
"fake": &graphql.FieldConfig{
Type: fakeString,
},
},
})
_, err := graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
})
expectedError := `Schema must contain unique named types but contains multiple types named "String".`
if err == nil || err.Error() != expectedError {
t.Fatalf("Expected error: %v, got %v", expectedError, err)
}
}
示例6: TestTypeSystem_ObjectsMustAdhereToInterfaceTheyImplement_RejectsAnObjectMissingAnInterfaceArgument
func TestTypeSystem_ObjectsMustAdhereToInterfaceTheyImplement_RejectsAnObjectMissingAnInterfaceArgument(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.String,
Args: graphql.FieldConfigArgument{
"input": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
},
},
})
anotherObject := graphql.NewObject(graphql.ObjectConfig{
Name: "AnotherObject",
Interfaces: []*graphql.Interface{anotherInterface},
Fields: graphql.FieldConfigMap{
"field": &graphql.FieldConfig{
Type: graphql.String,
},
},
})
_, 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)
}
}
示例7: TestIntrospection_IdentifiesDeprecatedFields
func TestIntrospection_IdentifiesDeprecatedFields(t *testing.T) {
testType := graphql.NewObject(graphql.ObjectConfig{
Name: "TestType",
Fields: graphql.FieldConfigMap{
"nonDeprecated": &graphql.FieldConfig{
Type: graphql.String,
},
"deprecated": &graphql.FieldConfig{
Type: graphql.String,
DeprecationReason: "Removed in 1.0",
},
},
})
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: testType,
})
if err != nil {
t.Fatalf("Error creating Schema: %v", err.Error())
}
query := `
{
__type(name: "TestType") {
name
fields(includeDeprecated: true) {
name
isDeprecated,
deprecationReason
}
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"__type": map[string]interface{}{
"name": "TestType",
"fields": []interface{}{
map[string]interface{}{
"name": "nonDeprecated",
"isDeprecated": false,
"deprecationReason": nil,
},
map[string]interface{}{
"name": "deprecated",
"isDeprecated": true,
"deprecationReason": "Removed in 1.0",
},
},
},
},
}
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))
}
}
示例8: TestCorrectlyThreadsArguments
func TestCorrectlyThreadsArguments(t *testing.T) {
query := `
query Example {
b(numArg: 123, stringArg: "foo")
}
`
var resolvedArgs map[string]interface{}
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Type",
Fields: graphql.FieldConfigMap{
"b": &graphql.FieldConfig{
Args: graphql.FieldConfigArgument{
"numArg": &graphql.ArgumentConfig{
Type: graphql.Int,
},
"stringArg": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Type: graphql.String,
Resolve: func(p graphql.GQLFRParams) interface{} {
resolvedArgs = p.Args
return resolvedArgs
},
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}
// parse query
ast := testutil.TestParse(t, query)
// execute
ep := graphql.ExecuteParams{
Schema: schema,
AST: ast,
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) > 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}
expectedNum := 123
expectedString := "foo"
if resolvedArgs["numArg"] != expectedNum {
t.Fatalf("Expected args.numArg to equal `%v`, got `%v`", expectedNum, resolvedArgs["numArg"])
}
if resolvedArgs["stringArg"] != expectedString {
t.Fatalf("Expected args.stringArg to equal `%v`, got `%v`", expectedNum, resolvedArgs["stringArg"])
}
}
示例9: TestTypeSystem_ObjectsMustHaveFields_RejectsAnObjectTypeWithMissingFields
func TestTypeSystem_ObjectsMustHaveFields_RejectsAnObjectTypeWithMissingFields(t *testing.T) {
badObject := graphql.NewObject(graphql.ObjectConfig{
Name: "SomeObject",
})
_, err := schemaWithFieldType(badObject)
expectedError := `SomeObject fields must be an object with field names as keys or a function which return such an object.`
if err == nil || err.Error() != expectedError {
t.Fatalf("Expected error: %v, got %v", expectedError, err)
}
}
示例10: TestTypeSystem_DefinitionExample_IncludesInterfacesThunkSubtypesInTheTypeMap
func TestTypeSystem_DefinitionExample_IncludesInterfacesThunkSubtypesInTheTypeMap(t *testing.T) {
someInterface := graphql.NewInterface(graphql.InterfaceConfig{
Name: "SomeInterface",
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: graphql.Int,
},
},
})
someSubType := graphql.NewObject(graphql.ObjectConfig{
Name: "SomeSubtype",
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: graphql.Int,
},
},
Interfaces: (graphql.InterfacesThunk)(func() []*graphql.Interface {
return []*graphql.Interface{someInterface}
}),
IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool {
return true
},
})
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.FieldConfigMap{
"iface": &graphql.FieldConfig{
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"))
}
}
示例11: schemaWithObjectFieldOfType
func schemaWithObjectFieldOfType(fieldType graphql.Input) (graphql.Schema, error) {
badObjectType := graphql.NewObject(graphql.ObjectConfig{
Name: "BadObject",
Fields: graphql.FieldConfigMap{
"badField": &graphql.FieldConfig{
Type: fieldType,
},
},
})
return graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: badObjectType,
},
},
}),
})
}
示例12: schemaWithFieldType
func schemaWithFieldType(ttype graphql.Output) (graphql.Schema, error) {
return graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: ttype,
},
},
}),
})
}
示例13: TestTypeSystem_ObjectsMustHaveFields_AcceptsAnObjectTypeWithFieldsObject
func TestTypeSystem_ObjectsMustHaveFields_AcceptsAnObjectTypeWithFieldsObject(t *testing.T) {
_, err := schemaWithFieldType(graphql.NewObject(graphql.ObjectConfig{
Name: "SomeObject",
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: graphql.String,
},
},
}))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
示例14: schemaWithObjectImplementingType
func schemaWithObjectImplementingType(implementedType *graphql.Interface) (graphql.Schema, error) {
badObjectType := graphql.NewObject(graphql.ObjectConfig{
Name: "BadObject",
Interfaces: []*graphql.Interface{implementedType},
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: graphql.String,
},
},
})
return graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: badObjectType,
},
},
}),
})
}
示例15: TestTypeSystem_SchemaMustContainUniquelyNamedTypes_RejectsASchemaWhichDefinesAnObjectTypeTwice
func TestTypeSystem_SchemaMustContainUniquelyNamedTypes_RejectsASchemaWhichDefinesAnObjectTypeTwice(t *testing.T) {
a := graphql.NewObject(graphql.ObjectConfig{
Name: "SameName",
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: graphql.String,
},
},
})
b := graphql.NewObject(graphql.ObjectConfig{
Name: "SameName",
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: graphql.String,
},
},
})
queryType := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.FieldConfigMap{
"a": &graphql.FieldConfig{
Type: a,
},
"b": &graphql.FieldConfig{
Type: b,
},
},
})
_, err := graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
})
expectedError := `Schema must contain unique named types but contains multiple types named "SameName".`
if err == nil || err.Error() != expectedError {
t.Fatalf("Expected error: %v, got %v", expectedError, err)
}
}