本文整理汇总了Golang中github.com/wadeandwendy/graphql.NewObject函数的典型用法代码示例。如果您正苦于以下问题:Golang NewObject函数的具体用法?Golang NewObject怎么用?Golang NewObject使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewObject函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: schemaWithArgOfType
func schemaWithArgOfType(ttype graphql.Type) (graphql.Schema, error) {
badObject := graphql.NewObject(graphql.ObjectConfig{
Name: "BadObject",
Fields: graphql.Fields{
"badField": &graphql.Field{
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.Fields{
"f": &graphql.Field{
Type: badObject,
},
},
}),
})
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:26,代码来源:validation_test.go
示例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.Fields{
"a": &graphql.Field{
Type: graphql.String,
},
},
}),
Mutation: graphql.NewObject(graphql.ObjectConfig{
Name: "M",
Fields: graphql.Fields{
"c": &graphql.Field{
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))
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:54,代码来源:executor_test.go
示例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.Fields{
"f": &graphql.Field{
Type: graphql.String,
},
},
})
_ = graphql.NewObject(graphql.ObjectConfig{
Name: "BadObject",
Interfaces: []*graphql.Interface{
anotherInterface,
},
Fields: graphql.Fields{
"f": &graphql.Field{
Type: graphql.String,
},
},
})
_ = graphql.NewObject(graphql.ObjectConfig{
Name: "BadObject",
Interfaces: []*graphql.Interface{
anotherInterface,
},
Fields: graphql.Fields{
"f": &graphql.Field{
Type: graphql.String,
},
},
})
queryType := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"iface": &graphql.Field{
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)
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:51,代码来源:validation_test.go
示例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.Fields{
"a": &graphql.Field{
Type: graphql.String,
},
},
}),
Mutation: graphql.NewObject(graphql.ObjectConfig{
Name: "M",
Fields: graphql.Fields{
"c": &graphql.Field{
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))
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:48,代码来源:executor_test.go
示例5: 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.Fields{
"field": &graphql.Field{
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.Fields{
"field": &graphql.Field{
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)
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:32,代码来源:validation_test.go
示例6: main
func main() {
// Schema
fields := graphql.Fields{
"hello": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) interface{} {
return "world"
},
},
}
rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
schema, err := graphql.NewSchema(schemaConfig)
if err != nil {
log.Fatalf("failed to create new schema, error: %v", err)
}
// Query
query := `
{
hello
}
`
params := graphql.Params{Schema: schema, RequestString: query}
r := graphql.Do(params)
if len(r.Errors) > 0 {
log.Fatalf("failed to execute graphql operation, errors: %+v", r.Errors)
}
rJSON, _ := json.Marshal(r)
fmt.Printf("%s \n", rJSON) // {“data”:{“hello”:”world”}}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:31,代码来源:main.go
示例7: 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.Fields{
"normal": &graphql.Field{
Type: graphql.String,
},
"fake": &graphql.Field{
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)
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:27,代码来源:validation_test.go
示例8: TestIntrospection_IdentifiesDeprecatedFields
func TestIntrospection_IdentifiesDeprecatedFields(t *testing.T) {
testType := graphql.NewObject(graphql.ObjectConfig{
Name: "TestType",
Fields: graphql.Fields{
"nonDeprecated": &graphql.Field{
Type: graphql.String,
},
"deprecated": &graphql.Field{
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))
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:59,代码来源:introspection_test.go
示例9: 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.Fields{
"b": &graphql.Field{
Args: graphql.FieldConfigArgument{
"numArg": &graphql.ArgumentConfig{
Type: graphql.Int,
},
"stringArg": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) 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"])
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:58,代码来源:executor_test.go
示例10: 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)
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:10,代码来源:validation_test.go
示例11: TestTypeSystem_DefinitionExample_IncludesInterfacesThunkSubtypesInTheTypeMap
func TestTypeSystem_DefinitionExample_IncludesInterfacesThunkSubtypesInTheTypeMap(t *testing.T) {
someInterface := graphql.NewInterface(graphql.InterfaceConfig{
Name: "SomeInterface",
Fields: graphql.Fields{
"f": &graphql.Field{
Type: graphql.Int,
},
},
})
someSubType := graphql.NewObject(graphql.ObjectConfig{
Name: "SomeSubtype",
Fields: graphql.Fields{
"f": &graphql.Field{
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.Fields{
"iface": &graphql.Field{
Type: someInterface,
},
},
}),
})
if err != nil {
t.Fatalf("unexpected error, got: %v", err)
}
if schema.Type("SomeSubtype") != someSubType {
t.Fatalf(`schema.GetType("SomeSubtype") expected to equal someSubType, got: %v`, schema.Type("SomeSubtype"))
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:42,代码来源:definition_test.go
示例12: schemaWithObjectFieldOfType
func schemaWithObjectFieldOfType(fieldType graphql.Input) (graphql.Schema, error) {
badObjectType := graphql.NewObject(graphql.ObjectConfig{
Name: "BadObject",
Fields: graphql.Fields{
"badField": &graphql.Field{
Type: fieldType,
},
},
})
return graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"f": &graphql.Field{
Type: badObjectType,
},
},
}),
})
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:21,代码来源:validation_test.go
示例13: schemaWithFieldType
func schemaWithFieldType(ttype graphql.Output) (graphql.Schema, error) {
return graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"f": &graphql.Field{
Type: ttype,
},
},
}),
})
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:12,代码来源:validation_test.go
示例14: schemaWithObjectImplementingType
func schemaWithObjectImplementingType(implementedType *graphql.Interface) (graphql.Schema, error) {
badObjectType := graphql.NewObject(graphql.ObjectConfig{
Name: "BadObject",
Interfaces: []*graphql.Interface{implementedType},
Fields: graphql.Fields{
"f": &graphql.Field{
Type: graphql.String,
},
},
})
return graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"f": &graphql.Field{
Type: badObjectType,
},
},
}),
})
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:22,代码来源:validation_test.go
示例15: TestTypeSystem_ObjectsMustHaveFields_AcceptsAnObjectTypeWithFieldsObject
func TestTypeSystem_ObjectsMustHaveFields_AcceptsAnObjectTypeWithFieldsObject(t *testing.T) {
_, err := schemaWithFieldType(graphql.NewObject(graphql.ObjectConfig{
Name: "SomeObject",
Fields: graphql.Fields{
"f": &graphql.Field{
Type: graphql.String,
},
},
}))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:13,代码来源:validation_test.go
示例16: TestTypeSystem_SchemaMustContainUniquelyNamedTypes_RejectsASchemaWhichDefinesAnObjectTypeTwice
func TestTypeSystem_SchemaMustContainUniquelyNamedTypes_RejectsASchemaWhichDefinesAnObjectTypeTwice(t *testing.T) {
a := graphql.NewObject(graphql.ObjectConfig{
Name: "SameName",
Fields: graphql.Fields{
"f": &graphql.Field{
Type: graphql.String,
},
},
})
b := graphql.NewObject(graphql.ObjectConfig{
Name: "SameName",
Fields: graphql.Fields{
"f": &graphql.Field{
Type: graphql.String,
},
},
})
queryType := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"a": &graphql.Field{
Type: a,
},
"b": &graphql.Field{
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)
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:37,代码来源:validation_test.go
示例17: TestThreadsContextCorrectly
func TestThreadsContextCorrectly(t *testing.T) {
query := `
query Example { a }
`
data := map[string]interface{}{
"contextThing": "thing",
}
var resolvedContext map[string]interface{}
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Type",
Fields: graphql.Fields{
"a": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) interface{} {
resolvedContext = p.Source.(map[string]interface{})
return resolvedContext
},
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}
// parse query
ast := testutil.TestParse(t, query)
// execute
ep := graphql.ExecuteParams{
Schema: schema,
Root: data,
AST: ast,
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) > 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}
expected := "thing"
if resolvedContext["contextThing"] != expected {
t.Fatalf("Expected context.contextThing to equal %v, got %v", expected, resolvedContext["contextThing"])
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:49,代码来源:executor_test.go
示例18: TestTypeSystem_ObjectsMustHaveFields_RejectsAnObjectTypeWithIncorrectlyNamedFields
func TestTypeSystem_ObjectsMustHaveFields_RejectsAnObjectTypeWithIncorrectlyNamedFields(t *testing.T) {
badObject := graphql.NewObject(graphql.ObjectConfig{
Name: "SomeObject",
Fields: graphql.Fields{
"bad-name-with-dashes": &graphql.Field{
Type: graphql.String,
},
},
})
_, err := schemaWithFieldType(badObject)
expectedError := `Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "bad-name-with-dashes" does not.`
if err == nil || err.Error() != expectedError {
t.Fatalf("Expected error: %v, got %v", expectedError, err)
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:15,代码来源:validation_test.go
示例19: TestThrowsIfNoOperationIsProvidedWithMultipleOperations
func TestThrowsIfNoOperationIsProvidedWithMultipleOperations(t *testing.T) {
doc := `query Example { a } query OtherExample { a }`
data := map[string]interface{}{
"a": "b",
}
expectedErrors := []gqlerrors.FormattedError{
gqlerrors.FormattedError{
Message: "Must provide operation name if query contains multiple operations.",
Locations: []location.SourceLocation{},
},
}
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Type",
Fields: graphql.Fields{
"a": &graphql.Field{
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,
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) != 1 {
t.Fatalf("wrong result, expected len(1) unexpected len: %v", len(result.Errors))
}
if result.Data != nil {
t.Fatalf("wrong result, expected nil result.Data, got %v", result.Data)
}
if !reflect.DeepEqual(expectedErrors, result.Errors) {
t.Fatalf("unexpected result, Diff: %v", testutil.Diff(expectedErrors, result.Errors))
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:48,代码来源:executor_test.go
示例20: TestTypeSystem_ObjectTypesMustBeAssertable_AcceptsAnObjectTypeWithAnIsTypeOfFunction
func TestTypeSystem_ObjectTypesMustBeAssertable_AcceptsAnObjectTypeWithAnIsTypeOfFunction(t *testing.T) {
_, err := schemaWithFieldType(graphql.NewObject(graphql.ObjectConfig{
Name: "AnotherObject",
IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool {
return true
},
Fields: graphql.Fields{
"f": &graphql.Field{
Type: graphql.String,
},
},
}))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
开发者ID:wadeandwendy,项目名称:graphql,代码行数:16,代码来源:validation_test.go
注:本文中的github.com/wadeandwendy/graphql.NewObject函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论