【问题标题】:ApolloServer using mergeSchemas, Error: Apollo Server requires either an existing schema, modules or typeDefsApolloServer 使用 mergeSchemas,错误:Apollo Server 需要现有架构、模块或 typeDefs
【发布时间】:2019-08-20 15:02:34
【问题描述】:

我认为我只是错误地使用了这个模块,这就是我收到错误的原因。根据文档,我可以将一组解析器和模式从graphql-tools 传递给mergeSchemas 函数。但是我收到了这个错误:

Error: Apollo Server requires either an existing schema, modules or typeDefs

代码如下:

app.js

import { ApolloServer } from 'apollo-server'
import schema from './modules'

const server = new ApolloServer({
    schema
})

server.listen().then(({ url }) => {
    console.log(`???? Server ready at ${url}`)
})

合并架构

import { mergeSchemas } from 'graphql-tools'

import bookSchema from './book/schema/book.gql'
import bookResolver from './book/resolvers/book'

export const schema = mergeSchemas({
    schemas: [bookSchema],
    resolvers: [bookResolver] // ==> Maybe I have to merge these before hand?
})

架构

type Query {
    book(id: String!): Book
    bookList: [Book]
}

type Book {
    id: String
    name: String
    genre: String
}

解析器

export default {
    Query: {
        book: (parent, args, context, info) => {
            console.log(parent, args, context, info)
            return {
                id: `1`,
                name: `name`,
                genre: `scary`
            }
        },
        bookList: (parent, args, context, info) => {
            console.log(parent, args, context, info)
            return [
                { id: `1`, name: `name`, genre: `scary` },
                { id: `2`, name: `name`, genre: `scary` }
            ]
        }
    }
}

【问题讨论】:

    标签: graphql apollo-server graphql-tools


    【解决方案1】:

    愚蠢的错误:

    需要从解构中导入模式对象:

    import { schema } from './modules'

    【讨论】:

      猜你喜欢
      • 2020-05-14
      • 2023-02-17
      • 2019-06-22
      • 2019-07-26
      • 2021-12-16
      • 2022-11-20
      • 1970-01-01
      • 2011-10-09
      • 2021-01-01
      相关资源
      最近更新 更多