本文整理汇总了Golang中github.com/graphql-go/graphql.NewSchema函数的典型用法代码示例。如果您正苦于以下问题:Golang NewSchema函数的具体用法?Golang NewSchema怎么用?Golang NewSchema使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewSchema函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: 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:rtbustamantec,项目名称:graphql,代码行数:31,代码来源:main.go
示例2: TestTypeSystem_DefinitionExample_DefinesASubscriptionScheme
func TestTypeSystem_DefinitionExample_DefinesASubscriptionScheme(t *testing.T) {
blogSchema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: blogQuery,
Subscription: blogSubscription,
})
if err != nil {
t.Fatalf("unexpected error, got: %v", err)
}
if blogSchema.SubscriptionType() != blogSubscription {
t.Fatalf("expected blogSchema.SubscriptionType() == blogSubscription")
}
subMutation, _ := blogSubscription.Fields()["articleSubscribe"]
if subMutation == nil {
t.Fatalf("subMutation is nil")
}
subMutationType := subMutation.Type
if subMutationType != blogArticle {
t.Fatalf("subMutationType expected to equal blogArticle, got: %v", subMutationType)
}
if subMutationType.Name() != "Article" {
t.Fatalf("subMutationType.Name expected to equal `Article`, got: %v", subMutationType.Name())
}
if subMutation.Name != "articleSubscribe" {
t.Fatalf("subMutation.Name expected to equal `articleSubscribe`, got: %v", subMutation.Name)
}
}
开发者ID:trythings,项目名称:trythings,代码行数:28,代码来源:definition_test.go
示例3: 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)
}
}
开发者ID:bbuck,项目名称:graphql,代码行数:27,代码来源:validation_test.go
示例4: TestTypeSystem_SchemaMustHaveObjectRootTypes_RejectsASchemaWithoutAQueryType
func TestTypeSystem_SchemaMustHaveObjectRootTypes_RejectsASchemaWithoutAQueryType(t *testing.T) {
_, err := graphql.NewSchema(graphql.SchemaConfig{})
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)
}
}
开发者ID:bbuck,项目名称:graphql,代码行数:7,代码来源:validation_test.go
示例5: main
func main() {
queryType := graphql.ObjectConfig{
Name: "Ping",
Description: "Ping to get pong...",
Fields: graphql.Fields{
"ping": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return "pong", nil
},
},
},
}
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(queryType),
})
if err != nil {
panic(err)
}
http.HandleFunc("/graphql", customHandler(&schema))
http.ListenAndServe(":8888", nil)
}
开发者ID:voidabhi,项目名称:golang-scripts,代码行数:26,代码来源:graphql.go
示例6: init
func init() {
globalIDTestUserType = graphql.NewObject(graphql.ObjectConfig{
Name: "User",
Fields: graphql.Fields{
"id": relay.GlobalIDField("User", nil),
"name": &graphql.Field{
Type: graphql.String,
},
},
Interfaces: []*graphql.Interface{globalIDTestDef.NodeInterface},
})
photoIDFetcher := func(obj interface{}, info graphql.ResolveInfo) string {
switch obj := obj.(type) {
case *photo2:
return fmt.Sprintf("%v", obj.PhotoId)
}
return ""
}
globalIDTestPhotoType = graphql.NewObject(graphql.ObjectConfig{
Name: "Photo",
Fields: graphql.Fields{
"id": relay.GlobalIDField("Photo", photoIDFetcher),
"width": &graphql.Field{
Type: graphql.Int,
},
},
Interfaces: []*graphql.Interface{globalIDTestDef.NodeInterface},
})
globalIDTestSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: globalIDTestQueryType,
})
}
开发者ID:N0hbdy,项目名称:relay,代码行数:33,代码来源:node_global_test.go
示例7: TestTypeSystem_DefinitionExample_DefinesAMutationScheme
func TestTypeSystem_DefinitionExample_DefinesAMutationScheme(t *testing.T) {
blogSchema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: blogQuery,
Mutation: blogMutation,
})
if err != nil {
t.Fatalf("unexpected error, got: %v", err)
}
if blogSchema.MutationType() != blogMutation {
t.Fatalf("expected blogSchema.GetMutationType() == blogMutation")
}
writeMutation, _ := blogMutation.Fields()["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.Name() != "Article" {
t.Fatalf("writeMutationType.Name expected to equal `Article`, got: %v", writeMutationType.Name())
}
if writeMutation.Name != "writeArticle" {
t.Fatalf("writeMutation.Name expected to equal `writeArticle`, got: %v", writeMutation.Name)
}
}
开发者ID:trythings,项目名称:trythings,代码行数:28,代码来源:definition_test.go
示例8: TestDirectives_DirectivesMustBeNamed
func TestDirectives_DirectivesMustBeNamed(t *testing.T) {
invalidDirective := graphql.NewDirective(graphql.DirectiveConfig{
Locations: []string{
graphql.DirectiveLocationField,
},
})
_, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "TestType",
Fields: graphql.Fields{
"a": &graphql.Field{
Type: graphql.String,
},
},
}),
Directives: []*graphql.Directive{invalidDirective},
})
expectedErr := gqlerrors.FormattedError{
Message: "Directive must be named.",
Locations: []location.SourceLocation{},
}
if !reflect.DeepEqual(expectedErr, err) {
t.Fatalf("Expected error to be equal, got: %v", testutil.Diff(expectedErr, err))
}
}
开发者ID:trythings,项目名称:trythings,代码行数:25,代码来源:directives_test.go
示例9: TestDirectives_DirectiveNameMustBeValid
func TestDirectives_DirectiveNameMustBeValid(t *testing.T) {
invalidDirective := graphql.NewDirective(graphql.DirectiveConfig{
Name: "123invalid name",
Locations: []string{
graphql.DirectiveLocationField,
},
})
_, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "TestType",
Fields: graphql.Fields{
"a": &graphql.Field{
Type: graphql.String,
},
},
}),
Directives: []*graphql.Directive{invalidDirective},
})
expectedErr := gqlerrors.FormattedError{
Message: `Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "123invalid name" does not.`,
Locations: []location.SourceLocation{},
}
if !reflect.DeepEqual(expectedErr, err) {
t.Fatalf("Expected error to be equal, got: %v", testutil.Diff(expectedErr, err))
}
}
开发者ID:trythings,项目名称:trythings,代码行数:26,代码来源:directives_test.go
示例10: init
func init() {
nodeTestUserType = graphql.NewObject(graphql.ObjectConfig{
Name: "User",
Fields: graphql.FieldConfigMap{
"id": &graphql.FieldConfig{
Type: graphql.NewNonNull(graphql.ID),
},
"name": &graphql.FieldConfig{
Type: graphql.String,
},
},
Interfaces: []*graphql.Interface{nodeTestDef.NodeInterface},
})
nodeTestPhotoType = graphql.NewObject(graphql.ObjectConfig{
Name: "Photo",
Fields: graphql.FieldConfigMap{
"id": &graphql.FieldConfig{
Type: graphql.NewNonNull(graphql.ID),
},
"width": &graphql.FieldConfig{
Type: graphql.Int,
},
},
Interfaces: []*graphql.Interface{nodeTestDef.NodeInterface},
})
nodeTestSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: nodeTestQueryType,
})
}
开发者ID:evenco,项目名称:go-graphql-relay,代码行数:30,代码来源:node_test.go
示例11: TestQuery_ExecutionDoesNotAddErrorsFromFieldResolveFn
func TestQuery_ExecutionDoesNotAddErrorsFromFieldResolveFn(t *testing.T) {
qError := errors.New("queryError")
q := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"a": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return nil, qError
},
},
"b": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return "ok", nil
},
},
},
})
blogSchema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: q,
})
if err != nil {
t.Fatalf("unexpected error, got: %v", err)
}
query := "{ b }"
result := graphql.Do(graphql.Params{
Schema: blogSchema,
RequestString: query,
})
if len(result.Errors) != 0 {
t.Fatalf("wrong result, unexpected errors: %+v", result.Errors)
}
}
开发者ID:graphql-go,项目名称:graphql,代码行数:34,代码来源:executor_test.go
示例12: schemaWithInputFieldOfType
func schemaWithInputFieldOfType(ttype graphql.Type) (graphql.Schema, error) {
badInputObject := graphql.NewInputObject(graphql.InputObjectConfig{
Name: "BadInputObject",
Fields: graphql.InputObjectConfigFieldMap{
"badField": &graphql.InputObjectFieldConfig{
Type: ttype,
},
},
})
return graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.FieldConfigMap{
"f": &graphql.FieldConfig{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
"badArg": &graphql.ArgumentConfig{
Type: badInputObject,
},
},
},
},
}),
})
}
开发者ID:bbuck,项目名称:graphql,代码行数:26,代码来源:validation_test.go
示例13: main
func main() {
log = log15.New()
log.Info("starting server...")
envconfig.Process("", &settings)
schema, err := graphql.NewSchema(makeSchema())
if err != nil {
log.Info("error creating schema", "err", err)
return
}
h := handler.New(&handler.Config{
Schema: &schema,
Pretty: false,
})
mux := http.NewServeMux()
mux.Handle("/graphql", h)
mux.Handle("/", http.FileServer(http.Dir("dist")))
log.Info("Listening at " + settings.Port + "...")
graceful.Run(":"+settings.Port, 1*time.Second, mux)
log.Info("Exiting...")
}
开发者ID:Xcopyco,项目名称:stack-forum,代码行数:25,代码来源:web.go
示例14: 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:trythings,项目名称:trythings,代码行数:59,代码来源:introspection_test.go
示例15: TestTypeSystem_SchemaMustHaveObjectRootTypes_AcceptsASchemaWhoseQueryTypeIsAnObjectType
func TestTypeSystem_SchemaMustHaveObjectRootTypes_AcceptsASchemaWhoseQueryTypeIsAnObjectType(t *testing.T) {
_, err := graphql.NewSchema(graphql.SchemaConfig{
Query: someObjectType,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
开发者ID:bbuck,项目名称:graphql,代码行数:8,代码来源:validation_test.go
示例16: 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{}, error) {
resolvedArgs = p.Args
return resolvedArgs, nil
},
},
},
}),
})
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:graphql-go,项目名称:graphql,代码行数:58,代码来源:executor_test.go
示例17: init
func init() {
s, err := graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
})
if err != nil {
log.Fatalf("failed to create schema, error: %v", err)
}
Schema = s
}
开发者ID:cerisier,项目名称:graphql,代码行数:9,代码来源:main.go
示例18: 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 := &graphql.Result{
Data: map[string]interface{}{
"a": "b",
},
}
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,
OperationName: "Q",
}
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:graphql-go,项目名称:graphql,代码行数:57,代码来源:executor_test.go
示例19: TestMutation_ExecutionAddsErrorsFromFieldResolveFn
func TestMutation_ExecutionAddsErrorsFromFieldResolveFn(t *testing.T) {
mError := errors.New("mutationError")
q := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"a": &graphql.Field{
Type: graphql.String,
},
},
})
m := graphql.NewObject(graphql.ObjectConfig{
Name: "Mutation",
Fields: graphql.Fields{
"foo": &graphql.Field{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
"f": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return nil, mError
},
},
"bar": &graphql.Field{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
"b": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return "ok", nil
},
},
},
})
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: q,
Mutation: m,
})
if err != nil {
t.Fatalf("unexpected error, got: %v", err)
}
query := "mutation _ { newFoo: foo(f:\"title\") }"
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
})
if len(result.Errors) == 0 {
t.Fatal("wrong result, expected errors, got no errors")
}
if result.Errors[0].Error() != mError.Error() {
t.Fatalf("wrong result, unexpected error, got: %v, expected: %v", result.Errors[0], mError)
}
}
开发者ID:graphql-go,项目名称:graphql,代码行数:56,代码来源:executor_test.go
示例20: TestTypeSystem_DefinitionExample_IncludesNestedInputObjectsInTheMap
func TestTypeSystem_DefinitionExample_IncludesNestedInputObjectsInTheMap(t *testing.T) {
nestedInputObject := graphql.NewInputObject(graphql.InputObjectConfig{
Name: "NestedInputObject",
Fields: graphql.InputObjectConfigFieldMap{
"value": &graphql.InputObjectFieldConfig{
Type: graphql.String,
},
},
})
someInputObject := graphql.NewInputObject(graphql.InputObjectConfig{
Name: "SomeInputObject",
Fields: graphql.InputObjectConfigFieldMap{
"nested": &graphql.InputObjectFieldConfig{
Type: nestedInputObject,
},
},
})
someMutation := graphql.NewObject(graphql.ObjectConfig{
Name: "SomeMutation",
Fields: graphql.Fields{
"mutateSomething": &graphql.Field{
Type: blogArticle,
Args: graphql.FieldConfigArgument{
"input": &graphql.ArgumentConfig{
Type: someInputObject,
},
},
},
},
})
someSubscription := graphql.NewObject(graphql.ObjectConfig{
Name: "SomeSubscription",
Fields: graphql.Fields{
"subscribeToSomething": &graphql.Field{
Type: blogArticle,
Args: graphql.FieldConfigArgument{
"input": &graphql.ArgumentConfig{
Type: someInputObject,
},
},
},
},
})
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: blogQuery,
Mutation: someMutation,
Subscription: someSubscription,
})
if err != nil {
t.Fatalf("unexpected error, got: %v", err)
}
if schema.Type("NestedInputObject") != nestedInputObject {
t.Fatalf(`schema.GetType("NestedInputObject") expected to equal nestedInputObject, got: %v`, schema.Type("NestedInputObject"))
}
}
开发者ID:trythings,项目名称:trythings,代码行数:55,代码来源:definition_test.go
注:本文中的github.com/graphql-go/graphql.NewSchema函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论