Graphql:未为 Go 中的突变配置架构
Graphql : Schema is not configured for mutations in Go
我正在尝试在 Go
中写入 Graphql Mutation
。
我正在使用 github.com/graphql-go/graphql
库。
这是我为此编写的代码。
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/graphql-go/graphql"
)
type userMaster struct {
Name string `json:"userName"`
ID string `json:"emailID"`
}
var user userMaster
func main() {
fmt.Println("Hello world !!!")
userType := graphql.NewObject(
graphql.ObjectConfig{
Name: "userMaster",
Fields: graphql.Fields{
"userName": &graphql.Field{
Type: graphql.String,
},
"emailID": &graphql.Field{
Type: graphql.String,
},
},
},
)
rootMutation := graphql.NewObject(graphql.ObjectConfig{
Name: "Mutations",
Fields: graphql.Fields{
"createUser": &graphql.Field{
Type: userType,
Args: graphql.FieldConfigArgument{
"userName": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
"emailID": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
user.Name = params.Args["userName"].(string)
user.ID = params.Args["emailID"].(string)
return user, nil
},
},
},
})
schema, _ := graphql.NewSchema(graphql.SchemaConfig{
Mutation: rootMutation,
})
http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: r.URL.Query().Get("query"),
})
json.NewEncoder(w).Encode(result)
})
http.ListenAndServe(":8080", nil)
}
当我尝试 运行 来自邮递员的命令时,我收到错误消息 {"data":null,"errors":[{"message":"Schema is not configured对于突变","locations":[{"line":1,"column":1}]}]}
http://localhost:8080/graphql?query=mutation+_{createUser(userName:"ABC",emailID:"abc@abc.com"){userName,emailID}}
这是 URL 我想从邮递员那里找到的。
我指的是 Getting Started With GraphQL Using Golang 在 Go
中实现变异
任何人都可以帮助这里需要做哪些更改吗?
您用于突变的 shema 需要一个查询对象。
schema, _ := graphql.NewSchema(graphql.SchemaConfig{
// QUERY NEEDED HERE !
Mutation: rootMutation,
})
用户类型的查询可以这样定义
并添加到架构中。
rootQuery := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"lastUser" : &graphql.Field{ // "lastUser" name can be anything
Type: userType,
},
},
})
schema, _ := graphql.NewSchema(graphql.SchemaConfig{
Query: rootQuery,
Mutation: rootMutation,
})
这将修复
"Schema is not configured for mutations"
错误并有你的
突变成功执行。
我正在尝试在 Go
中写入 Graphql Mutation
。
我正在使用 github.com/graphql-go/graphql
库。
这是我为此编写的代码。
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/graphql-go/graphql"
)
type userMaster struct {
Name string `json:"userName"`
ID string `json:"emailID"`
}
var user userMaster
func main() {
fmt.Println("Hello world !!!")
userType := graphql.NewObject(
graphql.ObjectConfig{
Name: "userMaster",
Fields: graphql.Fields{
"userName": &graphql.Field{
Type: graphql.String,
},
"emailID": &graphql.Field{
Type: graphql.String,
},
},
},
)
rootMutation := graphql.NewObject(graphql.ObjectConfig{
Name: "Mutations",
Fields: graphql.Fields{
"createUser": &graphql.Field{
Type: userType,
Args: graphql.FieldConfigArgument{
"userName": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
"emailID": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
user.Name = params.Args["userName"].(string)
user.ID = params.Args["emailID"].(string)
return user, nil
},
},
},
})
schema, _ := graphql.NewSchema(graphql.SchemaConfig{
Mutation: rootMutation,
})
http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: r.URL.Query().Get("query"),
})
json.NewEncoder(w).Encode(result)
})
http.ListenAndServe(":8080", nil)
}
当我尝试 运行 来自邮递员的命令时,我收到错误消息 {"data":null,"errors":[{"message":"Schema is not configured对于突变","locations":[{"line":1,"column":1}]}]}
http://localhost:8080/graphql?query=mutation+_{createUser(userName:"ABC",emailID:"abc@abc.com"){userName,emailID}}
这是 URL 我想从邮递员那里找到的。 我指的是 Getting Started With GraphQL Using Golang 在 Go
中实现变异任何人都可以帮助这里需要做哪些更改吗?
您用于突变的 shema 需要一个查询对象。
schema, _ := graphql.NewSchema(graphql.SchemaConfig{
// QUERY NEEDED HERE !
Mutation: rootMutation,
})
用户类型的查询可以这样定义 并添加到架构中。
rootQuery := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"lastUser" : &graphql.Field{ // "lastUser" name can be anything
Type: userType,
},
},
})
schema, _ := graphql.NewSchema(graphql.SchemaConfig{
Query: rootQuery,
Mutation: rootMutation,
})
这将修复
"Schema is not configured for mutations"
错误并有你的
突变成功执行。