Using:
"apollo-server": "^2.0.0-beta.1",
"apollo-server-express": "^2.0.0-beta.0",
With the following code to initialize the server:
const server = new ApolloServer(req => ({
typeDefs,
resolvers,
engine: true,
context: async () => ({
mongo: await connectMongo(),
user: req.user
})
}))
registerServer({ server, app })
// normal ApolloServer listen call but url will contain /graphql
const PORT = process.env.PORT || 3000
server.listen({ port: PORT }).then(({ url }) => {
console.log(`馃殌 Server ready at ${url}`)
})
I receive the follow error in my console:
throw new SchemaError('Must provide typeDefs');
^
Error: Must provide typeDefs
If just provide and object with instead of a function into the constructor is works, but I am integrating this with an express app which provides authentication.
Any help will be much appreciated.
I think it should be
const server = new ApolloServer({
typeDefs,
resolvers,
engine: true,
context: async () => ({
mongo: await connectMongo(),
user: req.user
})
})
without req => ({ ...
That worked out thank you!
I tried this solution. It solves the 'Must provide typeDefs' error, but now I'm getting 'req is not defined'
@gtwright I think you have not defined a "req" variable. Maybe you need to remove it from the context like this :
javascript
const server = new ApolloServer({
typeDefs,
resolvers,
engine: true,
context: async () => ({
mongo: await connectMongo(),
})
})
This was remove from code :
user: req.user
Most helpful comment
I think it should be
without
req => ({ ...