字段“我" \\"User \"类型的必须具有选择的子字段 [英] Field \"me\" of type \"User\" must have a selection of subfields

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

问题描述

我正在尝试学习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.

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

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