Apollo-server: [2.6.0] TypeError: Cannot read property 'some' of undefined

Created on 31 May 2019  ·  9Comments  ·  Source: apollographql/apollo-server


Description

Just tried to update apollo-server-express from 2.5.1 → 2.6.0 but it's failing with the following:

TypeError: Cannot read property 'some' of undefined

  61 | 
> 62 |   const apolloServer = new ApolloServer({
     |                        ^

  at typeDefs.some.typeDef (node_modules/apollo-server-core/src/utils/isDirectiveDefined.ts:8:25)
      at Array.some (<anonymous>)
  at Object.<anonymous>.exports.isDirectiveDefined (node_modules/apollo-server-core/src/utils/isDirectiveDefined.ts:7:12)
  at new ApolloServerBase (node_modules/apollo-server-core/src/ApolloServer.ts:275:12)
  at new ApolloServer (node_modules/apollo-server-express/src/ApolloServer.ts:84:5)

It appears this only happens when importing typeDefs from an SDL file.

Reproduction

Glitch Remix (see server.js line 5, and open the logs)


Hacky Workaround

I modified isDirectiveDefined locally to include a truthiness check and that works, but I'm not sure if that's a real fix or just masking an actual problem (e.g. should all typeDef objects have a definitions property, or is undefined valid?).

packages/apollo-server-core/src/utils/isDirectiveDefined.ts#L8

  typeDefs.some(typeDef =>
-    typeDef.definitions.some(
+    typeDef.definitions && typeDef.definitions.some(
      definition =>

The code indicates typeDef is of type DocumentNode from graphql/language but I didn't see DocumentNode in the exported type definitions and ran out of time to chase that further. ¯\_(ツ)_/¯

Most helpful comment

Bump this issue for latest 2.6.6

Passing string to typeDefs triggers same error unless wrap it in gql.

All 9 comments

Hey @wKovacs64, thanks for taking the time to report your issue. Based on the typings for a DocumentNode, it's invalid for definitions to be undefined (it should be at least an empty array):

export interface DocumentNode {
    readonly kind: "Document";
    readonly loc?: Location;
    readonly definitions: ReadonlyArray<DefinitionNode>;
}

In the glitch you provided, you're passing a string as typeDefs, when it really wants a DocumentNode (we know those more familiarly as the result of gql).

By passing that string into graphql-tag's function, we get the resulting DocumentNode that we want, like so:

const server = new ApolloServer({
  typeDefs: gql`${typeDefs}`,
  resolvers
});

I'm fairly certain this should resolve the issue, but if not, please feel free to reopen. Thanks again!

@abernix and I have discussed this a bit and recognize it as an unintended breaking change. We're working on a patch to reintroduce support for typeDefs as strings as we speak. Once again, appreciate the report!

Fixed in 2.6.1 (#2754), thanks to the both of you! 🚀

Thank you!

Bump this issue for latest 2.6.6

Passing string to typeDefs triggers same error unless wrap it in gql.

Hi! I'm getting the same error and it's wrapped in gql.
Version 2.11

@mg9101 can you please provide a runnable reproduction for us? Also if you're able, please make sure your server is running the latest version. Thanks!

@trevor-scheer I am also facing the same TypeError even after wrapping in gql. I created a repo to run and reproduce it
here https://github.com/chrisjaimes/apollo-server-error Any suggestions what I could be missing?

    "apollo-server-express": "^2.18.2", 
    "graphql-tools": "^6.2.4",
Was this page helpful?
0 / 5 - 0 ratings