javascript \"User\" 类型的字段 \"me\" 必须选择子字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46111514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Field \"me\" of type \"User\" must have a selection of subfields
提问by N Sharma
Hi I am trying to learn GraphQLlanguage. I have below snippet of code.
嗨,我正在努力学习GraphQL语言。我有下面的代码片段。
// Welcome to Launchpad!
// Log in to edit and save pads, run queries in GraphiQL on the right.
// Click "Download" above to get a zip with a standalone Node.js server.
// See docs and examples at https://github.com/apollographql/awesome-launchpad
// graphql-tools combines a schema string with resolvers.
import { makeExecutableSchema } from 'graphql-tools';
// Construct a schema, using GraphQL schema language
const typeDefs = `
type User {
name: String!
age: Int!
}
type Query {
me: User
}
`;
const user = { name: 'Williams', age: 26};
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
me: (root, args, context) => {
return user;
},
},
};
// Required: Export the GraphQL.js schema object as "schema"
export const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
// Optional: Export a function to get context from the request. It accepts two
// parameters - headers (lowercased http headers) and secrets (secrets defined
// in secrets section). It must return an object (or a promise resolving to it).
export function context(headers, secrets) {
return {
headers,
secrets,
};
};
// Optional: Export a root value to be passed during execution
// export const rootValue = {};
// Optional: Export a root function, that returns root to be passed
// during execution, accepting headers and secrets. It can return a
// promise. rootFunction takes precedence over rootValue.
// export function rootFunction(headers, secrets) {
// return {
// headers,
// secrets,
// };
// };
Request:
要求:
{
me
}
Response:
回复:
{
"errors": [
{
"message": "Field \"me\" of type \"User\" must have a selection of subfields. Did you mean \"me { ... }\"?",
"locations": [
{
"line": 4,
"column": 3
}
]
}
]
}
Does anyone know what I am doing wrong ? How to fix it ?
有谁知道我做错了什么?如何解决?
回答by Daniel Rearden
从文档:
A GraphQL object type has a name and fields, but at some point those fields have to resolve to some concrete data. That's where the scalar types come in: they represent the leaves of the query.
GraphQL 对象类型具有名称和字段,但在某些时候,这些字段必须解析为某些具体数据。这就是标量类型的用武之地:它们代表查询的叶子。
GraphQL requires that you construct your queries in a way that only returns concrete data. Each field has to ultimately resolve to one or more scalars (or enums). That means you cannot just request a field that resolves to a type without also indicating which fields of that type you want to get back.
GraphQL 要求您以仅返回具体数据的方式构建查询。每个字段最终都必须解析为一个或多个标量(或枚举)。这意味着您不能只请求一个解析为某个类型的字段,而不能同时指明您想要返回该类型的哪些字段。
That's what the error message you received is telling you -- you requested a Usertype, but you didn't tell GraphQL at least one field to get back from that type.
这就是您收到的错误消息告诉您的内容——您请求了一种User类型,但您没有告诉 GraphQL 至少一个字段以从该类型中返回。
To fix it, just change your request to include namelike this:
要修复它,只需更改您的请求以包含name如下内容:
{
me {
name
}
}
... or age. Or both. You cannot, however, request a specific type and expect GraphQL to provide all the fields for it -- you will always have to provide a selection (one or more) of fields for that type.
... 或age. 或两者。但是,您不能请求特定类型并期望 GraphQL 为其提供所有字段——您将始终必须为该类型提供一个选择(一个或多个)字段。

