Javascript 字段\“;“我”;类型\";用户\“;必须选择子字段

Javascript 字段\“;“我”;类型\";用户\“;必须选择子字段,javascript,graphql,graphql-js,Javascript,Graphql,Graphql Js,嗨,我正在努力学习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/apollog

嗨,我正在努力学习
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,
//   };
// };
请求:

{
  me
}
答复:

{
  "errors": [
    {
      "message": "Field \"me\" of type \"User\" must have a selection of subfields. Did you mean \"me { ... }\"?",
      "locations": [
        {
          "line": 4,
          "column": 3
        }
      ]
    }
  ]
}
有人知道我做错了什么吗?如何修复它?

GraphQL对象类型具有名称和字段,但在某些情况下 字段必须解析为一些具体数据。这就是标量 类型进来了:它们表示查询的叶子

GraphQL要求您以只返回具体数据的方式构造查询。每个字段最终必须解析为一个或多个标量(或枚举)。这意味着您不能只请求解析为类型的字段,而不指示要返回该类型的字段

这就是您收到的错误消息告诉您的——您请求了一个
User
类型,但您没有告诉GraphQL至少一个字段从该类型返回

要解决此问题,只需将请求更改为包含
name
,如下所示:

{
  me {
    name
  }
}
。。。或
年龄
。或者两者兼而有之。但是,您不能请求一个特定类型并期望GraphQL提供该类型的所有字段——您将始终必须为该类型提供一个字段选择(一个或多个)。

:

GraphQL对象类型具有名称和字段,但在某些情况下 字段必须解析为一些具体数据。这就是标量 类型进来了:它们表示查询的叶子

GraphQL要求您以只返回具体数据的方式构造查询。每个字段最终必须解析为一个或多个标量(或枚举)。这意味着您不能只请求解析为类型的字段,而不指示要返回该类型的字段

这就是您收到的错误消息告诉您的——您请求了一个
User
类型,但您没有告诉GraphQL至少一个字段从该类型返回

要解决此问题,只需将请求更改为包含
name
,如下所示:

{
  me {
    name
  }
}
。。。或
年龄
。或者两者兼而有之。但是,您不能请求一个特定类型并期望GraphQL为其提供所有字段——您将始终必须为该类型提供一个字段选择(一个或多个)