我的节点id的base64哈希值与我的期望值不同。

问题描述 投票:0回答:1

我使用的是 graphql-go 图书馆以及 graphql-go/relay. 我正试图为我的API写一些基本的测试,但在base64编码机制方面遇到了一些问题。当我执行一个简单的viewer id查询时,viewer编码的id是错误的。

我正在硬编码db调用,以检索第一个用户的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的最后一个字符与graphql.Do返回的不同。relay.ToGlobalID. 相反,它显示的是用 =. 而当我试图运行 relay.FromGlobalID("VXNlcjo=")它能够弄清其 typeUser但它返回的是一个空字符串 ID. 而如果我把 =x我是Go的新手,也是这些概念的新手,所以有什么想法或帮助可以让我非常感激。

我是Go的新手,也是这些概念的新手,所以任何想法或帮助都将是非常感激的。

go graphql base64 relay graphql-go
1个回答
0
投票

我是个大傻瓜,在定义我的API时忽略了写json字段标签。User 结构来检索db中的数据。单一的大写是可以的,但是由于我把I和D都大写了,所以不能正确的解析,传递了一个空的字符串作为 id. 以下是更正。

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

菜鸟的错误!但也许能对别人有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.