Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.6.0] TypeError: Cannot read property 'some' of undefined #2753

Closed
wKovacs64 opened this issue May 31, 2019 · 9 comments
Closed

[2.6.0] TypeError: Cannot read property 'some' of undefined #2753

wKovacs64 opened this issue May 31, 2019 · 9 comments

Comments

@wKovacs64
Copy link

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. ¯\_(ツ)_/¯

@trevor-scheer
Copy link
Member

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!

@trevor-scheer
Copy link
Member

@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!

@abernix
Copy link
Member

abernix commented May 31, 2019

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

@wKovacs64
Copy link
Author

Thank you!

@itsnotrisky
Copy link

Bump this issue for latest 2.6.6

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

@mg9101
Copy link

mg9101 commented May 13, 2020

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

@trevor-scheer
Copy link
Member

@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!

@chrisjaimes
Copy link

chrisjaimes commented Oct 9, 2020

@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",

@chrisjaimes
Copy link

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants