Skip to content

suggestion: alternative check for multiple graphql packages #3915

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

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integrationTests/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "graphql-js should be compatible with Webpack",
"type": "module",
"scripts": {
"test": "webpack && node test.js"
"test": "webpack && node test.js && node test-esm.js"
},
"dependencies": {
"graphql": "file:../graphql.tgz",
Expand Down
14 changes: 14 additions & 0 deletions integrationTests/webpack/test-esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import assert from 'assert';

/* eslint-disable n/no-missing-import */
import mjs from './dist/main-mjs.cjs';
/* eslint-enable n/no-missing-import */

assert.deepStrictEqual(mjs.result, {
data: {
__proto__: null,
hello: 'world',
},
});

console.log('Test script: Got correct result from Webpack esm bundle!');
10 changes: 1 addition & 9 deletions integrationTests/webpack/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'assert';

/* eslint-disable n/no-missing-import */
import cjs from './dist/main-cjs.cjs';
import mjs from './dist/main-mjs.cjs';
/* eslint-enable n/no-missing-import */

assert.deepStrictEqual(cjs.result, {
Expand All @@ -12,11 +11,4 @@ assert.deepStrictEqual(cjs.result, {
},
});

assert.deepStrictEqual(mjs.result, {
data: {
__proto__: null,
hello: 'world',
},
});

console.log('Test script: Got correct result from Webpack bundle!');
console.log('Test script: Got correct result from Webpack cjs bundle!');
49 changes: 49 additions & 0 deletions src/jsutils/__tests__/checkForMultiplePackageInstances-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable no-console */
import { expect } from 'chai';
import { afterEach, beforeEach, describe, it } from 'mocha';

import { checkForMultiplePackageInstances } from '../checkForMultiplePackageInstances.js';

describe('check for different library versions', () => {
class OtherPackageClass {}
const graphqlPackageInstanceCheckSymbol = Symbol.for(
'graphql-js:check-multiple-package-instances',
);
const globalObject = globalThis as {
[graphqlPackageInstanceCheckSymbol]?: unknown;
};
const orig = globalObject[graphqlPackageInstanceCheckSymbol];
const origError = console.error;
let errors: Array<unknown> = [];
beforeEach(() => {
errors = [];
console.error = (...args) => {
errors = args;
};
});

afterEach(() => {
globalObject[graphqlPackageInstanceCheckSymbol] = orig;
console.error = origError;
});

it('does not log an error under normal circumstances', () => {
checkForMultiplePackageInstances();
expect(errors).to.deep.equal([]);
checkForMultiplePackageInstances();
expect(errors).to.deep.equal([]);
checkForMultiplePackageInstances();
expect(errors).to.deep.equal([]);
});

it('logs an error if another package has been loaded first', () => {
// simulate other version of this lib to have been loaded before this version
globalObject[graphqlPackageInstanceCheckSymbol] = new OtherPackageClass();

checkForMultiplePackageInstances();

expect(errors[0]).to.match(
/Multiple colliding versions of the `graphql` package detected\./m,
);
});
});
37 changes: 37 additions & 0 deletions src/jsutils/checkForMultiplePackageInstances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const graphqlPackageInstanceCheckSymbol = Symbol.for(
'graphql-js:check-multiple-package-instances',
);

class Check {}

/**
* A check which throws an error warning when multi-realm constructors are detected.
*/
export function checkForMultiplePackageInstances() {
const globalObject = globalThis as {
[graphqlPackageInstanceCheckSymbol]?: Check;
};
if (!globalObject[graphqlPackageInstanceCheckSymbol]) {
globalObject[graphqlPackageInstanceCheckSymbol] = new Check();
return;
}
if (!(globalObject[graphqlPackageInstanceCheckSymbol] instanceof Check)) {
// eslint-disable-next-line no-console
console.error(
new Error(
`Multiple colliding versions of the \`graphql\` package detected.

Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.

https://yarnpkg.com/en/docs/selective-version-resolutions

Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results.`,
),
);
}
}
3 changes: 3 additions & 0 deletions src/jsutils/instanceOf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { checkForMultiplePackageInstances } from './checkForMultiplePackageInstances.js';
import { inspect } from './inspect.js';

checkForMultiplePackageInstances();

/**
* A replacement for instanceof which includes an error warning when multi-realm
* constructors are detected.
Expand Down