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

TypeError: Cannot read property 'filter' of undefined when generating flow/typescript file from schema #219

Open
andiwinata opened this issue Aug 23, 2018 · 6 comments

Comments

@andiwinata
Copy link

andiwinata commented Aug 23, 2018

Hello guys, first of all, thanks for creating the library!

However, just recently we are unable to generate flow type from our schema.graphql file.
I think this is because we recently added an interface type in our schema file. This is the part that is just recently added:

interface Error {
  message: String!
  type: ErrorType!
}

If I debug this, it is happening from this code:

// @gql2ts/from-schema/dist/index.js
const generateAbstractTypeDeclaration = (type, ignoredTypes) => {
    const poss = schemaInput.getPossibleTypes(type);
    let possibleTypes = poss
        .filter(t => !ignoredTypes.has(t.name))
        .map(t => generateInterfaceName(t.name));
    return generateTypeDeclaration(type.description, generateTypeName(type.name), possibleTypes.join(' | '));
};

When the error happens, the value of type is Error, and poss is undefined, the type correlates with the newly added interface Error in the schema.graphql, that's why I suspect it causes the problem.

If I wrap the .filter part with try catch, the file is generated again and it seems the content is correct (need to double check).
So are you guys aware of any issue related to interface declaration in schema file?

This is the stacktrace:

TypeError: Cannot read property 'filter' of undefined
    at generateAbstractTypeDeclaration (/Users/admin/test/node_modules/@gql2ts/from-schema/dist/index.js:134:18)
    at typeToInterface (/Users/admin/test/node_modules/@gql2ts/from-schema/dist/index.js:162:30)
    at typeArr.filter.filter.map.type (/Users/admin/test/node_modules/@gql2ts/from-schema/dist/index.js:179:10)
    at Array.map (<anonymous>)
    at typesToInterfaces (/Users/admin/test/node_modules/@gql2ts/from-schema/dist/index.js:178:14)
    at run (/Users/admin/test/node_modules/@gql2ts/from-schema/dist/index.js:185:12)
    at Object.exports.schemaToInterfaces (/Users/admin/test/node_modules/@gql2ts/from-schema/dist/index.js:187:73)
    at Object.exports.generateNamespace (/Users/admin/test/node_modules/@gql2ts/from-schema/dist/index.js:190:85)
    at run (/Users/admin/test/node_modules/gql2ts/dist/index.js:42:35)
    at Object.<anonymous> (/Users/admin/test/node_modules/gql2ts/dist/index.js:53:5)

This is the command (both typescript and flow have the same error)

gql2ts schema.graphql -o test.flow.js -e @gql2ts/language-flow --ignore-type-name-declaration
gql2ts schema.graphql -o test.d.ts
@brettjurgens
Copy link
Contributor

brettjurgens commented Aug 23, 2018

huh, that's pretty strange. I'll take a look and see if i can reproduce/fix. thanks for the report @andiwinata

edit: would it be possible to get a small reproduction case? are there any types that implement Error?

it seems like getPossibleTypes is yielding undefined

@andiwinata
Copy link
Author

this is a simple schema.graphql, let me know if it is missing something

interface Error {
  message: String!
  type: ErrorType!
}

type RuleError implements Error {
  message: String!
  type: ErrorType!
}

type RuleResponse {
  error: RuleError
  rule: [String!]!
}

type Query {
  Rule(story: String): RuleResponse!
}

@andiwinata
Copy link
Author

I think we found the issue, the above sample case probably won't replicate the issue.

The issue is we are declaring interface Error {} we don't use it anywhere else, so probably this test case will be able to replicate the issue:

interface Error {
  message: String!
  type: ErrorType!
}

type RuleError {
  message: String!
  type: ErrorType!
}

type RuleResponse {
  error: RuleError
  rule: [String!]!
}

type Query {
  Rule(story: String): RuleResponse!
}

@marco-amoroso
Copy link

marco-amoroso commented Aug 24, 2018

I saw that error when our schema had an interface but it was not used/implemented, basically there is no type Example implements Error { ... } in the schema.

Basically we need to exit sooner than:

let possibleTypes = poss
        .filter(...)

doing a check on poss being undefined or changing getPossibleTypes to return an empty array instead.

@andiwinata
Copy link
Author

So I guess it is up to whether declaring interface without usage is treated as a bug.
If not, which means interface must be used, I'm happy for this issue to be closed

@brettjurgens
Copy link
Contributor

IMO i think it's an issue to have an interface that isn't being implemented. when we do interfaces we also generate a union type, i.e.

type Product = ProductA | ProductB;

so I'm unsure what we'd do in this case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants