Skip to content

Convert error to warning for non-compliant use of __ #692

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

Merged
merged 1 commit into from
Jan 26, 2017
Merged
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
25 changes: 18 additions & 7 deletions src/type/__tests__/validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,26 @@ describe('Type System: Objects must have fields', () => {
);
});

it('rejects an Object type with reserved named fields', () => {
expect(
() => schemaWithFieldType(new GraphQLObjectType({
it('warns about an Object type with reserved named fields', () => {
/* eslint-disable no-console */
const realConsoleError = console.error;
const calls = [];
console.error = function () {
calls.push(Array.prototype.slice.call(arguments));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calls.push(Array.from(arguments));

Probably works in this environment too.

};
try {
schemaWithFieldType(new GraphQLObjectType({
name: 'SomeObject',
fields: { __notPartOfIntrospection: { type: GraphQLString } }
}))
).to.throw(
'Name "__notPartOfIntrospection" must not begin with "__", which is reserved by GraphQL introspection.'
);
}));

expect(calls[0][0]).contains(
'Name "__notPartOfIntrospection" must not begin with "__", which is reserved by GraphQL introspection.'
);
} finally {
console.error = realConsoleError;
}
/* eslint-enable no-console */
});

it('rejects an Object type with incorrectly typed fields', () => {
Expand Down
19 changes: 14 additions & 5 deletions src/utilities/assertValidName.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

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

// Ensures console warnings are only issued once.
let hasWarnedAboutDunder = false;

/**
* Upholds the spec rules about naming.
*/
Expand All @@ -22,11 +25,17 @@ export function assertValidName(
`Must be named. Unexpected name: ${name}.`
);
}
if (!isIntrospection && name.slice(0, 2) === '__') {
throw new Error(
`Name "${name}" must not begin with "__", which is reserved by ` +
'GraphQL introspection.'
);
if (!isIntrospection && name.slice(0, 2) === '__' && !hasWarnedAboutDunder) {
hasWarnedAboutDunder = true;
/* eslint-disable no-console */
if (console && console.error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would console ever be defined but falsey? Just being paranoid?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just being paranoid. I'd rather not have a cascading failure because someone's running graphql-js in some environment that either has no console or has console but doesn't have console.error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would typeof console !== 'undefined' be even more robust then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what environment might have console set to null or false or something silly like that, but I think this check should robust against whatever value console might have.

Technically typeof console === 'object' would be more specific, but it wouldn't yield a different result

const error = new Error(
`Name "${name}" must not begin with "__", which is reserved by ` +
'GraphQL introspection.'
);
console.error(error.stack || String(error));
}
/* eslint-enable no-console */
}
if (!NAME_RX.test(name)) {
throw new Error(
Expand Down