Field \ "me \" of type \ "User \" must have a choice of subfields

Hi, I'm trying to learn a language GraphQL. I have a piece 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,
//   };
// };

Inquiry:

{
  me
}

Answer:

{
  "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'm doing wrong? How to fix it?

+4
source share
1 answer

From the docs :

A GraphQL object type has a name and fields, but at some point these fields should solve some specific data. What scalar types are included: they are query leaves.

GraphQL , , . ( ). , , , , .

, , , - User, GraphQL , .

, name :

{
  me {
    name
  }
}

... age. . , GraphQL - ( ) .

+6

Source: https://habr.com/ru/post/1685283/


All Articles