Current Behavior
Using the latest version of serverless-offline (6.7) with apollo-server-lambda is resulting in an error.
offline: ANY /dev (位: hello)
offline: Failure: Query was defined in resolvers, but it's not an object
Error: Query was defined in resolvers, but it's not an object
Ref: https://github.com/apollographql/apollo-server/issues/4519
Sample Code
service: test
plugins:
- serverless-offline
provider:
name: aws
runtime: nodejs12.x
stage: dev
region: ap-southeast-2
functions:
graphql:
handler: src/graphql.handler
events:
- http:
path: /
method: any
Basic handler example from apollo server docs
const { ApolloServer, gql } = require('apollo-server-lambda');
// Hardcoded data store
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: 'J.K. Rowling',
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];
// Schema definition
const typeDefs = gql`
type Book {
title: String
author: String
}
type Query {
books: [Book]
}
`;
// Resolver map
const resolvers = {
Query: {
books() {
return books;
},
},
};
const server = new ApolloServer({ typeDefs, resolvers });
exports.handler = server.createHandler();
Expected behavior/code
It used to run perfectly with sls offline start
Environment
serverless version: 1.80serverless-offline version: 6.7.0node.js version: 12.16OS: macOS_optional, if you are using any of the following frameworks to invoke handlers_
python version: [e.g. v3.8.0]ruby version: [e.g. v2.6.5]Possible Solution
Additional context/Screenshots
Thanks for reporting this. I'm new to Serverless and can confirm this occurs using your same Environment config.
Downgrading to 6.6.0 works until there is a proper fix
Facing the same issue. But noticed that it actually works on first request. Second request fails with the same error.
offline: [HTTP] server ready: http://localhost:5005 馃殌
offline:
offline: Enter "rp" to replay the last request
offline: POST /dev/graphql (位: graphql)
coming HEREEE
offline: (位: graphql) RequestId: ckew97vmt0002ptfe3xg55akb Duration: 292.22 ms Billed Duration: 300 ms
offline: POST /dev/graphql (位: graphql)
offline: Failure: Query was defined in resolvers, but it's not an object
Error: Query was defined in resolvers, but it's not an object
I'm having the same issue 馃槶
When this is resolved would be really curious to see what the issue was - stumped me for a bit! Thought I had a sneaky syntax error lurking or some weird setup 馃槗 馃槃
I was able to track down a few more details about this error.
The error is raised by the graphql-tools package. Specifically this bit of code runs differently between the first request and subsequent ones:
function getFieldsForType(type) {
if (type instanceof graphql_1.GraphQLObjectType ||
type instanceof graphql_1.GraphQLInterfaceType) {
return type.getFields();
}
else {
return undefined;
}
}
Adding some logging to the top of this function:
console.log(
'getFieldsForType',
type,
type.constructor,
type instanceof graphql_1.GraphQLObjectType,
type instanceof graphql_1.GraphQLInterfaceType
)
I was able to see this for the first run:
getFieldsForType Query [Function: GraphQLObjectType] true false
And this for subsequent:
getFieldsForType Query [Function: GraphQLObjectType] false false
It reminds be a bit of issues I've had in the past when there's multiple versions of the graphql package in the same project and so instanceof checks would fail when comparing a schema from one of the graphql packages to the types in the other. But I've verified there's definitely only one instance of it in my project.
I'm using serverless-plugin-typescript as well in case it's relevant.
Reverting to 6.6.0 works for me.
I got a similar issue, allowCache: true solved my problem.
in my severless.yml file:
custom:
serverless-offline:
allowCache: true
I think severless-offline reloaded some packages, (but not all)
so for the first time, the instanceof is fine, but the second time, the constructor is reloaded and become a new one...
That's why the instanceof failed.
But haven't trace into severless-offline to find out why.
Most helpful comment
I got a similar issue,
allowCache: truesolved my problem.in my severless.yml file:
I think severless-offline reloaded some packages, (but not all)
so for the first time, the instanceof is fine, but the second time, the constructor is reloaded and become a new one...
That's why the
instanceoffailed.But haven't trace into severless-offline to find out why.