I am getting the following error when building Typedefs in Apollo Server:
return typeDef.definitions.some(definition => definition.kind === language_1.Kind.DIRECTIVE_DEFINITION &&
^
TypeError: Cannot read property 'some' of undefined
I tried to follow some solutions from here https://github.com/apollographql/apollo-server/issues/2961 but still, I am getting the error.
This is how I am creating the schema:
fs.readdirSync(__dirname)
.filter(dir => { console.log('dir', dir); return dir.indexOf('.') < 0 })
.forEach((dir) => {
const tmp = require(path.join(__dirname, dir)).default;
resolvers = merge(resolvers, tmp.resolvers);
typeDefs.push(tmp.types);
});
const schema = new ApolloServer({
typeDefs,
resolvers,
playground: {
endpoint: '/graphql',
settings: {
'editor.theme': 'light'
}
}
});
type.js
const Book = gql`
type Book {
title: String!
author: String!
}
`;
export const types = () => [Book];
export const typeResolvers = {
};
mutation.js
const Mutation = gql`
extend type Mutation {
addBook(book: BookInput): Book
}
`;
export const mutationTypes = () => [Mutation];
export const mutationResolvers = {
Mutation: {
addBook: async (_, args, ctx) => {
return []
}
}
};
index.js
export default {
types: () => [types, queryTypes, inputTypes, mutationTypes],
resolvers: Object.assign(queryResolvers, mutationResolvers, typeResolvers),
};
Any suggestions? What could I be missing?
After spending some time making changes, I finally got a working solution.
I had to make sure that
typeDefs
was an array of GraphQL Documents and not a type of[Function: types]
. To do that, I removed unnecessary function syntax.For example:
I replaced this
export const types = () => [Book];
with thisexport const types = Book;
I replaced this
types: () => [types, queryTypes, inputTypes, mutationTypes]
withtypes: [types, queryTypes, inputTypes, mutationTypes]
... and pretty much every where I had
() =>
Finally, before instantiating
ApolloServer
, instead of pushingtmp.types
to the array of types, I usedconcat
to use all defined graphql types in the I had defined the current file 'plus' the graphql types imported in every directorytypeDefs = typeDefs.concat(tmp.types);