首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >[错误:在解析器中定义的Query.Mutation,但不在架构中定义]

[错误:在解析器中定义的Query.Mutation,但不在架构中定义]
EN

Stack Overflow用户
提问于 2022-01-20 08:58:04
回答 1查看 370关注 0票数 1
代码语言:javascript
复制
const { ApolloServer, gql } = require('apollo-server-express');
const express = require('express');

const port = process.env.PORT || 4000;
const notes = [
    { id: '1', content: 'This is a note', author: 'Adam Scott' },
    { id: '2', content: 'This is another note', author: 'Harlow Everly' },
    { id: '3', content: 'Oh hey look, another note!', author: 'Riley Harrison' }
   ];

const typeDefs = gql `
    type Note {
        id: ID
        content: String
        author: String
    }

    type Query {
        hello: String
        notes: [Note]
        note(id: ID!): Note
    }

    type Mutation {
        newNote(content: String!): Note
    }
`;
const resolvers = {
    Query:{
        hello: () => 'Hello World',
        notes: () => notes,
        note: (parent, args) => {
            return notes.find(note => note.id == args.id);
        },
    Mutation: {
        newNote: (parent, args) => {
            let noteValue = {
                id : String(notes.length + 1),
                content : args.content,
                author: 'Adam Scott',
            };
            notes.push(noteValue);
            return noteValue;
        }
    }
    },
}

有些人有命名问题,但似乎我在解析器和模式中使用了相同的名称。请和我一起,这是我在GraphQL和快递的第二天。我故意删除了express对象和中间件的导入和分配,因为它不允许我发布。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-20 22:15:43

我想你只是错过了一个卷曲的括号。

代码语言:javascript
复制
const resolvers = {
    Query:{
        hello: () => 'Hello World',
        notes: () => notes,
        note: (parent, args) => {
            return notes.find(note => note.id == args.id);
        }
    }, <==== THIS IS MISSING =====>
    Mutation: {
        newNote: (parent, args) => {
            let noteValue = {
                id : String(notes.length + 1),
                content : args.content,
                author: 'Adam Scott',
            };
            notes.push(noteValue);
            return noteValue;
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70783283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档