本文整理匯總了Golang中github.com/chris-ramon/graphql-go/types.NewGraphQLSchema函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewGraphQLSchema函數的具體用法?Golang NewGraphQLSchema怎麽用?Golang NewGraphQLSchema使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewGraphQLSchema函數的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: init
func init() {
globalIDTestUserType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "User",
Fields: types.GraphQLFieldConfigMap{
"id": gqlrelay.GlobalIdField("User", nil),
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
Interfaces: []*types.GraphQLInterfaceType{globalIDTestDef.NodeInterface},
})
photoIdFetcher := func(obj interface{}, info types.GraphQLResolveInfo) string {
switch obj := obj.(type) {
case *photo2:
return fmt.Sprintf("%v", obj.PhotoId)
}
return ""
}
globalIDTestPhotoType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Photo",
Fields: types.GraphQLFieldConfigMap{
"id": gqlrelay.GlobalIdField("Photo", photoIdFetcher),
"width": &types.GraphQLFieldConfig{
Type: types.GraphQLInt,
},
},
Interfaces: []*types.GraphQLInterfaceType{globalIDTestDef.NodeInterface},
})
globalIDTestSchema, _ = types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: globalIDTestQueryType,
})
}
示例3: TestTypeSystem_SchemaMustContainUniquelyNamedTypes_RejectsASchemaWhichRedefinesABuiltInType
func TestTypeSystem_SchemaMustContainUniquelyNamedTypes_RejectsASchemaWhichRedefinesABuiltInType(t *testing.T) {
fakeString := types.NewGraphQLScalarType(types.GraphQLScalarTypeConfig{
Name: "String",
Serialize: func(value interface{}) interface{} {
return nil
},
})
queryType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Query",
Fields: types.GraphQLFieldConfigMap{
"normal": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
"fake": &types.GraphQLFieldConfig{
Type: fakeString,
},
},
})
_, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
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)
}
}
示例4: schemaWithInputFieldOfType
func schemaWithInputFieldOfType(ttype types.GraphQLType) (types.GraphQLSchema, error) {
badInputObject := types.NewGraphQLInputObjectType(types.InputObjectConfig{
Name: "BadInputObject",
Fields: types.InputObjectConfigFieldMap{
"badField": &types.InputObjectFieldConfig{
Type: ttype,
},
},
})
return types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Query",
Fields: types.GraphQLFieldConfigMap{
"f": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
Args: types.GraphQLFieldConfigArgumentMap{
"badArg": &types.GraphQLArgumentConfig{
Type: badInputObject,
},
},
},
},
}),
})
}
示例5: TestTypeSystem_SchemaMustHaveObjectRootTypes_RejectsASchemaWithoutAQueryType
func TestTypeSystem_SchemaMustHaveObjectRootTypes_RejectsASchemaWithoutAQueryType(t *testing.T) {
_, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{})
expectedError := "Schema query must be Object Type but got: nil."
if err == nil || err.Error() != expectedError {
t.Fatalf("Expected error: %v, got %v", expectedError, err)
}
}
示例6: init
func init() {
postType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Post",
Fields: types.GraphQLFieldConfigMap{
// Define `id` field as a Relay GlobalID field.
// It helps with translating your GraphQL object's id into a global id
// For eg:
// For a `Post` type, with an id of `1`, it's global id will be `UG9zdDox`
// which is a base64 encoded version of `Post:1` string
// We will explore more in the next part of this series.
"id": gqlrelay.GlobalIdField("Post", nil),
"text": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
})
queryType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Query",
Fields: types.GraphQLFieldConfigMap{
"latestPost": &types.GraphQLFieldConfig{
Type: postType,
Resolve: func(p types.GQLFRParams) interface{} {
return GetLatestPost()
},
},
},
})
Schema, _ = types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: queryType,
})
}
示例7: TestTypeSystem_DefinitionExample_DefinesAMutationScheme
func TestTypeSystem_DefinitionExample_DefinesAMutationScheme(t *testing.T) {
blogSchema, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: blogQuery,
Mutation: blogMutation,
})
if err != nil {
t.Fatalf("unexpected error, got: %v", err)
}
if blogSchema.GetMutationType() != blogMutation {
t.Fatalf("expected blogSchema.GetMutationType() == blogMutation")
}
writeMutation, _ := blogMutation.GetFields()["writeArticle"]
if writeMutation == nil {
t.Fatalf("writeMutation is nil")
}
writeMutationType := writeMutation.Type
if writeMutationType != blogArticle {
t.Fatalf("writeMutationType expected to equal blogArticle, got: %v", writeMutationType)
}
if writeMutationType.GetName() != "Article" {
t.Fatalf("writeMutationType.Name expected to equal `Article`, got: %v", writeMutationType.GetName())
}
if writeMutation.Name != "writeArticle" {
t.Fatalf("writeMutation.Name expected to equal `writeArticle`, got: %v", writeMutation.Name)
}
}
示例8: TestIntrospection_IdentifiesDeprecatedFields
func TestIntrospection_IdentifiesDeprecatedFields(t *testing.T) {
testType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "TestType",
Fields: types.GraphQLFieldConfigMap{
"nonDeprecated": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
"deprecated": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
DeprecationReason: "Removed in 1.0",
},
},
})
schema, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: testType,
})
if err != nil {
t.Fatalf("Error creating GraphQLSchema: %v", err.Error())
}
query := `
{
__type(name: "TestType") {
name
fields(includeDeprecated: true) {
name
isDeprecated,
deprecationReason
}
}
}
`
expected := &types.GraphQLResult{
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 := graphql(t, gql.GraphqlParams{
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))
}
}
示例9: TestTypeSystem_SchemaMustHaveObjectRootTypes_AcceptsASchemaWhoseQueryTypeIsAnObjectType
func TestTypeSystem_SchemaMustHaveObjectRootTypes_AcceptsASchemaWhoseQueryTypeIsAnObjectType(t *testing.T) {
_, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: someObjectType,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
示例10: TestCorrectlyThreadsArguments
func TestCorrectlyThreadsArguments(t *testing.T) {
query := `
query Example {
b(numArg: 123, stringArg: "foo")
}
`
var resolvedArgs map[string]interface{}
schema, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Type",
Fields: types.GraphQLFieldConfigMap{
"b": &types.GraphQLFieldConfig{
Args: types.GraphQLFieldConfigArgumentMap{
"numArg": &types.GraphQLArgumentConfig{
Type: types.GraphQLInt,
},
"stringArg": &types.GraphQLArgumentConfig{
Type: types.GraphQLString,
},
},
Type: types.GraphQLString,
Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} {
resolvedArgs = p.Args
return resolvedArgs
},
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}
// parse query
ast := testutil.Parse(t, query)
// execute
ep := executor.ExecuteParams{
Schema: schema,
AST: ast,
}
result := testutil.Execute(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"])
}
}
示例11: TestAvoidsRecursion
func TestAvoidsRecursion(t *testing.T) {
doc := `
query Q {
a
...Frag
...Frag
}
fragment Frag on Type {
a,
...Frag
}
`
data := map[string]interface{}{
"a": "b",
}
expected := &types.GraphQLResult{
Data: map[string]interface{}{
"a": "b",
},
}
schema, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Type",
Fields: types.GraphQLFieldConfigMap{
"a": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}
// parse query
ast := testutil.Parse(t, doc)
// execute
ep := executor.ExecuteParams{
Schema: schema,
AST: ast,
Root: data,
OperationName: "Q",
}
result := testutil.Execute(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))
}
}
示例12: schemaWithFieldType
func schemaWithFieldType(ttype types.GraphQLOutputType) (types.GraphQLSchema, error) {
return types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Query",
Fields: types.GraphQLFieldConfigMap{
"f": &types.GraphQLFieldConfig{
Type: ttype,
},
},
}),
})
}
示例13:
func TestTypeSystem_SchemaMustContainUniquelyNamedTypes_RejectsASchemaWhichHaveSameNamedObjectsImplementingAnInterface(t *testing.T) {
anotherInterface := types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
Name: "AnotherInterface",
ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
return nil
},
Fields: types.GraphQLFieldConfigMap{
"f": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
})
_ = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "BadObject",
Interfaces: []*types.GraphQLInterfaceType{
anotherInterface,
},
Fields: types.GraphQLFieldConfigMap{
"f": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
})
_ = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "BadObject",
Interfaces: []*types.GraphQLInterfaceType{
anotherInterface,
},
Fields: types.GraphQLFieldConfigMap{
"f": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
})
queryType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Query",
Fields: types.GraphQLFieldConfigMap{
"iface": &types.GraphQLFieldConfig{
Type: anotherInterface,
},
},
})
_, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
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)
}
}
示例14: TestThreadsContextCorrectly
func TestThreadsContextCorrectly(t *testing.T) {
query := `
query Example { a }
`
data := map[string]interface{}{
"contextThing": "thing",
}
var resolvedContext map[string]interface{}
schema, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Type",
Fields: types.GraphQLFieldConfigMap{
"a": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} {
resolvedContext = p.Source.(map[string]interface{})
return resolvedContext
},
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}
// parse query
ast := testutil.Parse(t, query)
// execute
ep := executor.ExecuteParams{
Schema: schema,
Root: data,
AST: ast,
}
result := testutil.Execute(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"])
}
}
示例15: TestThrowsIfNoOperationIsProvidedWithMultipleOperations
func TestThrowsIfNoOperationIsProvidedWithMultipleOperations(t *testing.T) {
doc := `query Example { a } query OtherExample { a }`
data := map[string]interface{}{
"a": "b",
}
expectedErrors := []graphqlerrors.GraphQLFormattedError{
graphqlerrors.GraphQLFormattedError{
Message: "Must provide operation name if query contains multiple operations.",
Locations: []location.SourceLocation{},
},
}
schema, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
Query: types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
Name: "Type",
Fields: types.GraphQLFieldConfigMap{
"a": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}
// parse query
ast := testutil.Parse(t, doc)
// execute
ep := executor.ExecuteParams{
Schema: schema,
AST: ast,
Root: data,
}
result := testutil.Execute(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))
}
}