json - Go GraphQL客户端应用于“hello world” GraphQL服务器:“hello”的结构字段在任何要解码的地方都不存在”

标签 json go graphql

我正在尝试在一个简单的GraphQL服务器上使用https://github.com/shurcooL/graphql GraphQL客户端,该服务器已按照https://github.com/apollographql/apollo-server#installation-standalone上的代码片段开始:

const { ApolloServer, gql } = require('apollo-server');

// The GraphQL schema
const typeDefs = gql`
  type Query {
    "A simple type for getting started!"
    hello: String
  }
`;

// A map of functions which return data for the schema.
const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
});

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});

我正在尝试将其与以下Go脚本一起使用:

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/shurcooL/graphql"
)

var query struct {
    hello graphql.String
}

func main() {
    client := graphql.NewClient("http://localhost:4000", nil)

    if err := client.Query(context.Background(), &query, nil); err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%+v\n", query)
}

但是,如果我尝试运行此命令,则会出现以下错误:
2019/11/21 12:07:21 struct field for "hello" doesn't exist in any of 1 places to unmarshal
exit status 1

知道是什么原因造成的吗?我看不到我的实现与https://github.com/shurcooL/graphql中的文档有何不同。

最佳答案

要将Burak Serdar的评论转换为答案,hello不是导出的名称(参见https://tour.golang.org/basics/3),我需要将其更改为Hello:

var query struct {
    Hello graphql.String
}

现在程序打印
{Hello:world}

关于json - Go GraphQL客户端应用于“hello world” GraphQL服务器:“hello”的结构字段在任何要解码的地方都不存在”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58983066/

相关文章:

arrays - 数组 Varchar (psql) 到数组字符串 (golang)

reactjs - React/Relay Modern/GraphQL 简单项目设置与 Babel 问题

json - 使用 Rest Api 的 Xamarin 中的 ListView

json - 如何在Fluter上从Firestore中的模型json数据内部序列化/建模

json - 选择键:value with jq and output as array

json - ReactJS:在渲染期间计算 JSON 对象中的字符串数组的平均值

go - 如何使 golang 的 golang 版本和我的 Dockerfile 保持同步?

go - 创建一个 rand 结构

graphql - 使用 MDX 在 Gatsby 博客中添加 featuresImage

reactjs - 在 graphql/relay 中维护有序列表