-
Notifications
You must be signed in to change notification settings - Fork 2k
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.7] TypeError: Cannot read property 'some' of undefined #2961
Comments
Are you sure you've run |
Since the error is happening in |
Just checked now. Here's the {
"apollo-server": "^2.6.4",
"apollo-server-express": "^2.6.4",
"graphql": "^14.4.1"
} Here's the {
"apollo-server": "^2.6.7",
"apollo-server-express": "^2.6.7",
"graphql": "^14.4.1"
} Output:st/utils/isDirectiveDefined.js:11
return typeDef.definitions.some(definition => definition.kind === language_1.Kind.DIRECTIVE_DEFINITION &&
^
TypeError: Cannot read property 'some' of undefined
|
@goto1 How are you setting your |
The setup looks like this: const { gql } = require('apollo-server-express')
// Book.js
const Book = gql`
type Book {...}
`
module.exports = () => [Book]
// Author.js
const Author = gql`
type Author {...}
`
module.exports = () => [Author]
// index.js
const Author = require('./Author')
const Book = require('./Book')
const root = gql`...`
const typeDefs = [root, Author, Book]
const apollo = new ApolloServer({
typeDefs,
...
}) Let me know if you have any other questions. Thanks! |
To be honest, I'd like to figure out more concretely where our internal typings have led us awry on getting this right so a runnable reproduction would be super helpful. I'm curious what it is about the format of your
|
Ok no problem. I will work on an example that I can share when I get home from work. One thing that might be of your interest, however, if you look at the code snippet I provided above, essentially what I am doing is the following: const root = gql`...`
const type1 = () => [gql`...`]
const type2 = () => [gql`...`]
// then
const typeDefs = [root, type1, type2]; // [DocumentNode, function, function] So when it goes through the items in the return typeDefs.some(typeDef => {
console.log("typeof typeDef", typeof typeDef);
console.log('typeDef', typeDef);
if (typeof typeDef === 'string') {
typeDef = gql(typeDef);
}
return typeDef.definitions.some(
definition =>
definition.kind === Kind.DIRECTIVE_DEFINITION &&
definition.name.value === directiveName,
);
}); Which is why it fails when a function gets passed: typeof typeDef object
typeDef { kind: 'Document', ...}
typeof typeDef function # fails here
typeDef function () {
return [Author];
}
/Users/tom/Development/att/murray-run/backend/node_modules/apollo-server-core/dist/utils/isDirectiveDefined.js:12
return typeDef.definitions.some(
^
TypeError: Cannot read property 'some' of undefined This seems to work in Not sure if this is the intended use and/or behavior, however, but one fix that comes to my mind would simply to unpack the array that's passed (like in my case): if (typeof typeDef === 'string') {
typeDef = gql(typeDef);
}
const temp = typeof typeDef === 'function' ? typeDef() : typeDef
// In a case where it's an array
//
// [ { kind: 'Document',
// definitions: [ [Object], [Object] ],
// loc: { start: 0, end: 241 } } ]
//
if (Array.isArray(temp)) {
temp.forEach(item => {
// item.definitions.some()
})
}
if (temp.kind === 'Document') {
// item.definitions.some()
} This is just a quick solution that comes to my mind, but this can be handled a little better for sure. |
Thanks for boiling it down. I'd love to know where you picked up that pattern of passing |
I guess this is a historical artifact back from I'm really curious what your use-case is here because, to be honest, I'd really rather those be statically analyzable. |
Not sure if this is still the case, but there was a need to do something like the following when storing // Book.js
const Book = gql`...`
// Author.js
const Book = require('./Book.js')
const Author = gql`
type Author {
firstName: String!
lastName: String!
books: [Book]!
}`
module.exports = () => [Author, Book]
` Otherwise in I will try just to simply do the following, without actually exporting types that my other type relies on, and then just combine them at the end with const Author = gql`...`
module.exports = Author; |
As another consideration, you might also try |
Great, thanks a lot. I think this can be closed now. Thanks a lot for your help and contributions to this project! |
You're welcome! And thank you for reporting this and your prompt and thorough/detailed responses. I'll close it, but if it continues to be a pain-point, we'll consider what bringing this functionality back might look like, but I'd lean heavily on the rather not button for now for reasons I can go into if it continues to be a recurring problem. 😁 |
In my situation, where I had a setup similar to the following: // Author.js
const Book = require('./Book.js')
const Author = gql`
type Author {
books: [Book]
}
`
module.exports = () => [Author, Book]
// Book.js
const Book = gql`
type Book { ... }
`
module.exports = Book when you're passing your const isFunction = require('lodash/isFunction')
function combineTypeDefs(items) {
return items.reduce((combinedTypeDefs, item) => {
const typeDefs = isFunction(item) ? item() : item
return Array.isArray(typeDefs)
? [...combinedTypeDefs, ...typeDefs]
: [...combinedTypeDefs, typeDefs]
}, [])
const typeDefs = combineTypeDefs([
Author, // () => [Author, Book]
Book
]) Of course, this might not cover all your cases so you'll have to take care of them, but this would've been my solution if I didn't have the ability to just simply switching to const Author = gql`...`
module.exports = Author |
Same for me. It works on 2.6.1, but doesn't work on 2.9.7 |
Excellent issue and great research. We also used the The correct solution IMO is to avoid this syntax for exporting types and just export types as DocumentNode or [DocumentNode, DocumentNode] and flatten them before passing them to typeDefs. |
Facing same issue TypeError: Cannot read property 'some' of undefined
|
Hello!
First of all, thank you all for this great tool.
I've noticed this morning an error message when updating to
v2.6.7
.package.json
package.json
where the issue does not occur:Actual behavior
Environment
node v8.16.0
macOS 10.14.5
One thing I noticed is that when I updated both
apollo-server
andapollo-server-express
tov2.6.7
but left thegraphql
package atv14.3.1
the issue is still there.Please let me know if there's any additional info needed that I could provide.
Thank you!
The text was updated successfully, but these errors were encountered: