This repository has been archived by the owner on May 26, 2023. It is now read-only.
forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from apollographql/no-throw-on-duplicate
No throw on duplicate
- Loading branch information
Showing
10 changed files
with
222 additions
and
71 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as shelljs from 'shelljs'; | ||
import { join } from 'path'; | ||
|
||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
describe('graphql-js module', () => { | ||
it('allows interop between two versions of iteslf', () => { | ||
const warn = console.warn; | ||
const warnedWith = []; | ||
console.warn = message => warnedWith.push(message); | ||
const root = shelljs.pwd().stdout; | ||
|
||
const g1Path = 'dist'; | ||
const g2Path = 'dist1'; | ||
|
||
shelljs.cp('-r', g1Path, g2Path); | ||
|
||
const g1 = require(join(root, g1Path)); | ||
const g2 = require(join(root, g2Path)); | ||
|
||
const obj = new g1.GraphQLObjectType({ | ||
name: 'Dog', | ||
fields: { id: { type: g1.GraphQLString } }, | ||
}); | ||
|
||
expect(g1.isObjectType(obj)).to.be.true; | ||
expect(g2.isObjectType(obj)).to.be.true; | ||
expect(warnedWith[0]).to.include( | ||
'Using GraphQLObjectType "Dog" from another module or realm.', | ||
); | ||
expect(warnedWith[1]).to.be.undefined; | ||
console.warn = warn; | ||
}).timeout(5000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// @flow strict | ||
|
||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import instanceOf from '../instanceOf'; | ||
|
||
describe('instanceOf', () => { | ||
it('fails with descriptive error message', () => { | ||
let calledWith = []; | ||
const warn = console.warn; | ||
console.warn = message => calledWith.push(message); | ||
|
||
function getFoo() { | ||
class Foo {} | ||
return Foo; | ||
} | ||
|
||
function getBar() { | ||
return Bar; | ||
} | ||
|
||
const Foo1 = getFoo(); | ||
const Foo2 = getFoo(); | ||
|
||
class Bar {} | ||
const bar = new Bar(); | ||
|
||
expect(calledWith[0]).to.be.undefined; | ||
expect(instanceOf(new Foo1(), Foo2)).to.be.true; | ||
expect(calledWith[0]).to.not.be.undefined; | ||
expect(instanceOf(new Foo2(), Foo1)).to.be.true; | ||
expect(calledWith[1]).to.be.undefined; // only warn once | ||
|
||
expect(instanceOf(bar, Foo1)).to.be.false; | ||
console.warn = warn; | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// @flow strict | ||
|
||
/** | ||
* A replacement for instanceof which includes an error warning when multi-realm | ||
* constructors are detected. | ||
*/ | ||
declare export default function instanceOf( | ||
value: mixed, | ||
constructor: mixed, | ||
): boolean %checks(value instanceof constructor); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* A replacement for instanceof which includes an error warning when multi-realm | ||
* constructors are detected. | ||
*/ | ||
|
||
// Keep the message around, but only show it once (per instance of graphql-js that encounters mismatched constructors) | ||
let warned = false; | ||
// See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production | ||
// See: https://webpack.js.org/guides/production/ | ||
export default process.env.NODE_ENV === 'production' | ||
? // eslint-disable-next-line no-shadow | ||
function instanceOf<T>( | ||
value: unknown, | ||
constructor: new () => T, | ||
): value is T { | ||
return ( | ||
value instanceof constructor || | ||
(value != null && | ||
value.constructor != null && | ||
constructor.name != null && | ||
value.constructor.name == constructor.name) | ||
); | ||
} | ||
: // eslint-disable-next-line no-shadow | ||
function instanceOf<T>( | ||
value: unknown, | ||
constructor: new () => T, | ||
): value is T { | ||
if (value instanceof constructor) { | ||
return true; | ||
} | ||
if (value) { | ||
const valueClass = value.constructor; | ||
const className = constructor.name; | ||
if (className && valueClass && valueClass.name === className) { | ||
if (!warned) { | ||
console.warn( | ||
`@apollo/graphql: Using ${className} "${value}" from another module or realm. | ||
This could produce confusing and spurious results, especially if your "graphql"/"@apollo/graphql" versions are inconsistent. | ||
You can use \`npm ls graphql\` and \`npm ls @apollo/graphql\` to ensure all versions are consistent.`, // TODO: determine what we mean by "consistent" | ||
); | ||
warned = true; | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; |