Error when building typedefs TypeError: Cannot read property 'some' of undefined

3.6k views Asked by At

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?

2

There are 2 answers

0
chris On BEST ANSWER

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 this export const types = Book;

I replaced this types: () => [types, queryTypes, inputTypes, mutationTypes] with types: [types, queryTypes, inputTypes, mutationTypes]

... and pretty much every where I had () =>

Finally, before instantiating ApolloServer, instead of pushing tmp.types to the array of types, I used concat to use all defined graphql types in the I had defined the current file 'plus' the graphql types imported in every directory

typeDefs = typeDefs.concat(tmp.types);

0
jadiaheno On

I just had the same issue for the past 2 hours. I realized the file were i was instantiating my apollo server was being executed before the typedefs was created.

Simple way to test for this is to make a console.log(types, queryTypes, inputTypes, mutationTypes) right before the execution of const schema = new ApolloServer({ ....

One of them is undefined. Thanks.