Skip to content

Commit fe34619

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 024f9f7 commit fe34619

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/type/__tests__/validation-test.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,26 @@ 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 about an Object type with reserved named fields', () => {
345+
/* eslint-disable no-console */
346+
const realConsoleError = 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+
'Name "__notPartOfIntrospection" must not begin with "__", which is reserved by GraphQL introspection.'
359+
);
360+
} finally {
361+
console.error = realConsoleError;
362+
}
363+
/* eslint-enable no-console */
353364
});
354365

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

src/utilities/assertValidName.js

+14-5
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,17 @@ 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 error = new Error(
33+
`Name "${name}" must not begin with "__", which is reserved by ` +
34+
'GraphQL introspection.'
35+
);
36+
console.error(error.stack || String(error));
37+
}
38+
/* eslint-enable no-console */
3039
}
3140
if (!NAME_RX.test(name)) {
3241
throw new Error(

0 commit comments

Comments
 (0)