8

I think I am just using this module wrong and that is the reason I am getting an error. According to the documentation I can pass an array of resolvers and schemas to the mergeSchemas function from graphql-tools. But I am getting this error:

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

Here is the code:

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}`)
})

Merging Schemas

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?
})

Schema

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

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

Resolver

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` }
            ]
        }
    }
}
Taylor Austin
  • 5,407
  • 15
  • 58
  • 103

2 Answers2

5

Silly mistake:

Needed to import schema object from de structuring it:

import { schema } from './modules'

Taylor Austin
  • 5,407
  • 15
  • 58
  • 103
2

For my scenario, I was getting the above error when I passed the argument like below.

const server = new ApolloServer(typeDefs, resolvers);

then it changed to

const server = new ApolloServer({typeDefs, resolvers});

the below error has gone up.

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