登录
首页 >  Golang >  Go问答

Base64 哈希值与我的节点 ID 不匹配

来源:stackoverflow

时间:2024-02-23 17:45:24 293浏览 收藏

珍惜时间,勤奋学习!今天给大家带来《Base64 哈希值与我的节点 ID 不匹配》,正文内容主要涉及到等等,如果你正在学习Golang,或者是对Golang有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!

问题内容

我正在使用 graphql-go 库以及 graphql-go/relay。我正在尝试为我的 api 编写一些基本测试,但在使用 base64 编码机制时遇到了一些问题。当我对查看者 id 执行简单查询时,查看者编码的 id 是错误的。

我正在对数据库调用进行硬编码,以检索 id 为 1 的第一个用户:

func TestGetViewer(t *testing.T) {
  // Empty and fill the db for consistency
  prepDb()

  query := `
    query {
      viewer {
        id
      }
    }
  `

  // The schema contains a viewer entry point that returns a userType:
  // userType = graphql.NewObject(graphql.ObjectConfig{
  //    Name:        "User",
  //    Description: "A user",
  //    Fields: graphql.Fields{
  //      "id": relay.GlobalIDField("User", nil),
  //    },
  // })
  schema := gql.NewSchema(db)

  // graphql.Do is called by graphql.Execute. I'm trying to get 
  // closer to what is actually called in order to debug
  result := graphql.Do(graphql.Params{
    Schema:        schema,
    RequestString: query,
  })

  // The return data from our query
  data := result.Data.(map[string]interface{})

  // The relay.GlobalIDField in our schema should be calling this method
  // under the hood, however, as you'll see it returns something different
  id := relay.ToGlobalID("User", "1")

  // prints map[viewer:map[id:VXNlcjo= username:janedoe]]
  // notice the last character of the id
  fmt.Println(data)

  // prints VXNlcjox
  fmt.Println(id)

graphql.do 返回的内容非常接近,但编码 id 的最后一个字符与 relay.toglobalid 返回的内容不同。相反,它显示用 = 填充的最后一个字符。当我尝试运行 relay.fromglobalid("vxnlcjo=") 时,它能够确定其 typeuser,但它为 id 返回一个空字符串。然而,如果我将 = 替换为 x,它会返回正确的对象。

我是 go 和这些概念的新手,因此任何想法或帮助将不胜感激!


解决方案


我是个大傻瓜,在定义 user 结构以从数据库检索数据时忽略了编写 json 字段标记。单一大写是可以的,但由于我将 i 和 d 都大写,因此它无法正确解析,并且为 id 传递了一个空字符串。以下已更正:

type User struct {
    ID int `json:"id"`
}

菜鸟错误!但也许对其他人有帮助。

以上就是《Base64 哈希值与我的节点 ID 不匹配》的详细内容,更多关于的资料请关注golang学习网公众号!

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