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

Cannot access 'typeMap' before initialization #1973

Closed
gmac opened this issue Sep 1, 2020 · 9 comments
Closed

Cannot access 'typeMap' before initialization #1973

gmac opened this issue Sep 1, 2020 · 9 comments

Comments

@gmac
Copy link
Contributor

gmac commented Sep 1, 2020

There appears to be finickiness with abstract type definitions in a merge. The example below errors with ReferenceError: Cannot access 'typeMap' before initialization when the union type is present. Interestingly though, I have a branch in QA right now that successfully implements a similar pattern. That points to one of a few scenarios:

  1. My QA branch is on 6.0.18 still, so this could be a regression in 6.1.0 (<- this appears to be the case).
  2. Maybe merging abstracts is order-dependent, and I happen to have a compatible order in my working branch but not here.

I know this works – I'm not sure why it doesn't work here.

const { makeExecutableSchema } = require('@graphql-tools/schema');
const { stitchSchemas } = require('@graphql-tools/stitch');
const { addMocksToSchema } = require('@graphql-tools/mock');

const imageSchema = makeExecutableSchema({
  typeDefs: `
    type Image {
      id: ID!
      url: String!
    }

    type Query {
      images(ids: [ID!]!): [Image]!
    }
  `
});

const contentSchema = makeExecutableSchema({
  typeDefs: `
    type Post {
      leadArt: LeadArt
    }

    type Image {
      id: ID!
    }

    type Video {
      id: ID!
      url: String!
    }

    union LeadArt = Image | Video

    type Query {
      posts(ids: [ID!]!): [Post]!
    }
  `
});

const gatewaySchema = stitchSchemas({
  subschemas: [
    {
      schema: addMocksToSchema({ schema: imageSchema }),
      merge: {
        Image: {
          selectionSet: '{ id }',
          fieldName: 'images',
          key: ({ id }) => id,
          argsFromKeys: (ids) => ({ ids }),
        },
      },
    },
    {
      schema: addMocksToSchema({ schema: contentSchema }),
      merge: {
        Post: {
          selectionSet: '{ id }',
          fieldName: 'posts',
          key: ({ id }) => id,
          argsFromKeys: (ids) => ({ ids }),
        },
      },
    },
  ],
  mergeTypes: true
});

Trace:

/Users/greg.macwilliam/Desktop/micromedia/node_modules/@graphql-tools/stitch/index.cjs.js:395
            typeMap[type.name] = type;
            ^

ReferenceError: Cannot access 'typeMap' before initialization
    at /Users/greg.macwilliam/Desktop/micromedia/node_modules/@graphql-tools/stitch/index.cjs.js:395:13
    at Array.forEach (<anonymous>)
    at /Users/greg.macwilliam/Desktop/micromedia/node_modules/@graphql-tools/stitch/index.cjs.js:394:22
    at Array.reduce (<anonymous>)
    at mergeUnionTypeCandidates (/Users/greg.macwilliam/Desktop/micromedia/node_modules/@graphql-tools/stitch/index.cjs.js:393:29)
    at mergeCandidates (/Users/greg.macwilliam/Desktop/micromedia/node_modules/@graphql-tools/stitch/index.cjs.js:277:16)
    at /Users/greg.macwilliam/Desktop/micromedia/node_modules/@graphql-tools/stitch/index.cjs.js:591:33
    at Array.forEach (<anonymous>)
    at buildTypeMap (/Users/greg.macwilliam/Desktop/micromedia/node_modules/@graphql-tools/stitch/index.cjs.js:583:33)
    at stitchSchemas (/Users/greg.macwilliam/Desktop/micromedia/node_modules/@graphql-tools/stitch/index.cjs.js:961:21)
@yaacovCR
Copy link
Collaborator

yaacovCR commented Sep 1, 2020

Hmmm. Below passes for me on master.

describe('new', () => {
  it('works', () => {
    const imageSchema = makeExecutableSchema({
      typeDefs: `
        type Image {
          id: ID!
          url: String!
        }

        type Query {
          images(ids: [ID!]!): [Image]!
        }
      `
    });

    const contentSchema = makeExecutableSchema({
      typeDefs: `
        type Post {
          leadArt: LeadArt
        }

        type Image {
          id: ID!
        }

        type Video {
          id: ID!
          url: String!
        }

        union LeadArt = Image | Video

        type Query {
          posts(ids: [ID!]!): [Post]!
        }
      `
    });

    const gatewaySchema = stitchSchemas({
      subschemas: [
        {
          schema: addMocksToSchema({ schema: imageSchema }),
          merge: {
            Image: {
              selectionSet: '{ id }',
              fieldName: 'images',
              key: ({ id }) => id,
              argsFromKeys: (ids) => ({ ids }),
            },
          },
        },
        {
          schema: addMocksToSchema({ schema: contentSchema }),
          merge: {
            Post: {
              selectionSet: '{ id }',
              fieldName: 'posts',
              key: ({ id }) => id,
              argsFromKeys: (ids) => ({ ids }),
            },
          },
        },
      ],
      mergeTypes: true
    });

    expect(gatewaySchema).toBeInstanceOf(GraphQLSchema);
  });
});

@yaacovCR
Copy link
Collaborator

yaacovCR commented Sep 1, 2020

What version of GraphQL are you using?

If you submit a PR with above test, ci will test with latest versions of 14 and 15

@gmac
Copy link
Contributor Author

gmac commented Sep 1, 2020

Curious... not rightly sure, I just did a “yarn install” on everything in a new project and assumed I got the latest of everything. Will have to do some more digging. Thanks for having a look.

@gmac
Copy link
Contributor Author

gmac commented Sep 1, 2020

Welp, I'm stumped. I've put the test into the GraphQL Tools repo and am still getting the same result: #1981.

See failure in https://github.com/ardatan/graphql-tools/pull/1981/checks?check_run_id=1056293407.

@yaacovCR
Copy link
Collaborator

yaacovCR commented Sep 1, 2020

Not sure why passed for me but looks easy though to correct

@gmac
Copy link
Contributor Author

gmac commented Sep 1, 2020 via email

@yaacovCR
Copy link
Collaborator

yaacovCR commented Sep 1, 2020

I think just should have used acc instead of typemap in that reduce,

@gmac
Copy link
Contributor Author

gmac commented Sep 1, 2020

Uh oh, this definitely appears to be a version issue. I just setup a 6.2 upgrade branch and this error appeared... Last night when I tried downgrading to 6.0.18 to smoketest, I'll bet I bumped the packaged down but not the lock file, at which time my 0.18 test was still running a more recent version.

@gmac
Copy link
Contributor Author

gmac commented Sep 1, 2020

Should be good to go in #1981, with new test coverage. Thanks for mentioning the fix – that was it!

@yaacovCR yaacovCR added the waiting-for-release Fixed/resolved, and waiting for the next stable release label Sep 1, 2020
@ardatan ardatan closed this as completed Sep 8, 2020
@yaacovCR yaacovCR removed the waiting-for-release Fixed/resolved, and waiting for the next stable release label Oct 25, 2020
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