gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:37:23 24 4
gpt4 key购买 nike

我正在尝试在一个简单的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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com