Skip to content

Commit e801580

Browse files
committed
Convert error to warning for non-compliant use of __
This unbreaks various uses of graphql-js which were defining fields with __ while retaining some form of reporting warning for non-compliant use. Fixes #690
1 parent 47c2ba6 commit e801580

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/type/__tests__/validation-test.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,27 @@ describe('Type System: Objects must have fields', () => {
341341
);
342342
});
343343

344-
it('rejects an Object type with reserved named fields', () => {
345-
expect(
346-
() => schemaWithFieldType(new GraphQLObjectType({
344+
it('warns when an Object type with reserved named fields', () => {
345+
/* eslint-disable no-console */
346+
const prevConsoleError = console.error;
347+
const calls = [];
348+
console.error = function () {
349+
calls.push(Array.prototype.slice.call(arguments));
350+
};
351+
try {
352+
schemaWithFieldType(new GraphQLObjectType({
347353
name: 'SomeObject',
348354
fields: { __notPartOfIntrospection: { type: GraphQLString } }
349-
}))
350-
).to.throw(
351-
'Name "__notPartOfIntrospection" must not begin with "__", which is reserved by GraphQL introspection.'
352-
);
355+
}));
356+
357+
expect(calls[0][0]).contains([
358+
[ 'GraphQL: Name "__notPartOfIntrospection" must not begin with ' +
359+
'"__", which is reserved by GraphQL introspection.' ]
360+
]);
361+
} finally {
362+
console.error = prevConsoleError;
363+
}
364+
/* eslint-enable no-console */
353365
});
354366

355367
it('rejects an Object type with incorrectly typed fields', () => {

src/utilities/assertValidName.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
const NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/;
1212

13+
// Ensures console warnings are only issued once.
14+
let hasWarnedAboutDunder = false;
15+
1316
/**
1417
* Upholds the spec rules about naming.
1518
*/
@@ -22,11 +25,20 @@ export function assertValidName(
2225
`Must be named. Unexpected name: ${name}.`
2326
);
2427
}
25-
if (!isIntrospection && name.slice(0, 2) === '__') {
26-
throw new Error(
27-
`Name "${name}" must not begin with "__", which is reserved by ` +
28-
'GraphQL introspection.'
29-
);
28+
if (!isIntrospection && name.slice(0, 2) === '__' && !hasWarnedAboutDunder) {
29+
hasWarnedAboutDunder = true;
30+
/* eslint-disable no-console */
31+
if (console && console.error) {
32+
const stack = new Error().stack;
33+
const stackOnly = stack ?
34+
'\n' + stack.split('\n').slice(1).join('\n') :
35+
'';
36+
console.error(
37+
`GraphQL: Name "${name}" must not begin with "__", which is ` +
38+
'reserved by GraphQL introspection.' + stackOnly
39+
);
40+
}
41+
/* eslint-enable no-console */
3042
}
3143
if (!NAME_RX.test(name)) {
3244
throw new Error(

0 commit comments

Comments
 (0)