Skip to content

Using link/schema and @graphql-tools/schema in combination and bundled by webpack results in "Cannot use GraphQLSchema from another module or realm" #7184

@stoically

Description

@stoically

Intended outcome:

Using SchemaLink with @graphql-tools/schema's makeExecutableSchema works as expected when bundled with webpack

Actual outcome:

Error: Cannot use GraphQLSchema "[object GraphQLSchema]" from another module or realm.

It seems that link/schema pulls in CommonJS instead of ESM when bundled with webpack, so it's different classes.

Related issue in the @graphql-tools repo: ardatan/graphql-tools#1790

How to reproduce the issue:

Minimal repo: https://github.com/stoically/apollo-client-link-schema-webpack

Fix

Instead of directly reaching into graphql like so

import { execute } from 'graphql/execution/execute';
import { GraphQLSchema } from 'graphql/type/schema';

import from the top level, like so

import { execute, GraphQLSchema } from 'graphql';

Though, I guess the actual fix would be to dive into webpack and check what's going wrong

Workaround

webpack.config.js

const path = require("path")

  resolve: {
    alias: {
      "graphql/execution/execute": path.resolve(
        __dirname,
        "./node_modules/graphql/execution/execute.mjs"
      ),
      "graphql/validation/validate": path.resolve(
        __dirname,
        "./node_modules/graphql/validation/validate.mjs"
      ),
    },

Other things I tried which didn't work out

Versions

System:
OS: Linux 5.4 Ubuntu 20.04.1 LTS (Focal Fossa)
Binaries:
Node: 12.18.4 - ~/.nvm/versions/node/v12.18.4/bin/node
Yarn: 1.22.10 - ~/.nvm/versions/node/v12.18.4/bin/yarn
npm: 6.14.8 - ~/.nvm/versions/node/v12.18.4/bin/npm
Browsers:
Chrome: 86.0.4240.75
Firefox: 81.0.2
npmPackages:
@apollo/client: 3.3.0-beta.13 => 3.3.0-beta.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions