字段“我"类型为“用户"必须有一个子字段的选择 [英] Field "me" of type "User" must have a selection of subfields

查看:26
本文介绍了字段“我"类型为“用户"必须有一个子字段的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 GraphQL 语言.我有以下代码片段.

Hi I am trying to learn GraphQL language. I have below snippet of code.

// 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
        }
      ]
    }
  ]
}

有谁知道我做错了什么?如何解决?

Does anyone know what I am doing wrong ? How to fix it ?

推荐答案

来自文档:

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

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.

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

That's what the error message you received is telling you -- you requested a User type, but you didn't tell GraphQL at least one field to get back from that type.

要修复它,只需更改您的请求以包含 name,如下所示:

To fix it, just change your request to include name like this:

{
  me {
    name
  }
}

... 或 age.或两者.但是,您不能请求特定类型并期望 GraphQL 为其提供所有字段——您始终必须为该类型提供一个选择(一个或多个)字段.

... 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.

这篇关于字段“我"类型为“用户"必须有一个子字段的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆