简体   繁体   中英

UnhandledPromiseRejectionWarning: Error: Type Query must define one or more fields

this my code

import { gql } from "apollo-server-express";

export const typeDefs = gql`
  type Query 
  type Mutation {
    id: ID!
    text: String!
  }
`;

I only have mutations, I don't need Query.i get error

UnhandledPromiseRejectionWarning: Error: Type Query must define one or more fields.

How can I declare an empty Query?

From the spec :

A schema defines the initial root operation type for each kind of operation it supports: query, mutation, and subscription; this determines the place in the type system where those operations begin.

The query root operation type must be provided and must be an Object type.

The mutation root operation type is optional; if it is not provided, the service does not support mutations. If it is provided, it must be an Object type.

Additionally :

An Object type must define one or more fields.

So a query root operation type must be provided, it must be an Object type and, as a result, it must include at least one field.

You'll need to provide at least one field for Query, even if it's never used and always just returns null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM