登录
首页 >  Golang >  Go问答

Graphql:没有为 Go 中的突变配置架构

来源:stackoverflow

时间:2024-04-05 10:54:35 312浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《Graphql:没有为 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":"架构未配置突变","locations": [{"行":1,"列":1}]}]}

http://localhost:8080/graphql?query=mutation+_{createuser(username:"abc",emailid:"[email protected]"){username,emailid}}

这是我试图从邮递员那里打到的网址。 我指的是 graphql 入门使用 golang 在 go 中实现突变

任何人都可以帮助这里需要进行哪些更改吗?


解决方案


您用于突变的模式需要一个查询对象。

schema, _ := graphql.newschema(graphql.schemaconfig{
    // query needed here !
    mutation: rootmutation,
})

可以这样定义 usertype 的查询 并添加到架构中。

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,
})

这将修复 “架构未配置突变” 错误并让您 突变成功执行。

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>