Skip to content

Fix: OverlappingFieldsCanBeMerged on js keyword named fields #864

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
May 18, 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
5 changes: 4 additions & 1 deletion src/jsutils/keyMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ export default function keyMap<T>(
list: Array<T>,
keyFn: (item: T) => string
): {[key: string]: T} {
return list.reduce((map, item) => ((map[keyFn(item)] = item), map), {});
return list.reduce(
(map, item) => ((map[keyFn(item)] = item), map),
Object.create(null)
);
}
2 changes: 1 addition & 1 deletion src/jsutils/keyValMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ export default function keyValMap<T, V>(
): {[key: string]: V} {
return list.reduce(
(map, item) => ((map[keyFn(item)] = valFn(item)), map),
{}
Object.create(null)
);
}
8 changes: 4 additions & 4 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ function defineInterfaces(
'an Array.'
);

const implementedTypeNames = {};
const implementedTypeNames = Object.create(null);
interfaces.forEach(iface => {
invariant(
iface instanceof GraphQLInterfaceType,
Expand Down Expand Up @@ -513,7 +513,7 @@ function defineFieldMap<TSource, TContext>(
'function which returns such an object.'
);

const resultFieldMap = {};
const resultFieldMap = Object.create(null);
fieldNames.forEach(fieldName => {
assertValidName(fieldName);
const fieldConfig = fieldMap[fieldName];
Expand Down Expand Up @@ -818,7 +818,7 @@ function defineTypes(
'Must provide Array of types or a function which returns ' +
`such an array for Union ${unionType.name}.`
);
const includedTypeNames = {};
const includedTypeNames = Object.create(null);
types.forEach(objType => {
invariant(
objType instanceof GraphQLObjectType,
Expand Down Expand Up @@ -1091,7 +1091,7 @@ export class GraphQLInputObjectType {
`${this.name} fields must be an object with field names as keys or a ` +
'function which returns such an object.'
);
const resultFieldMap = {};
const resultFieldMap = Object.create(null);
fieldNames.forEach(fieldName => {
assertValidName(fieldName);
const field = {
Expand Down
6 changes: 3 additions & 3 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ export function extendSchema(
);

// Collect the type definitions and extensions found in the document.
const typeDefinitionMap = {};
const typeExtensionsMap = {};
const typeDefinitionMap = Object.create(null);
const typeExtensionsMap = Object.create(null);

// New directives and types are separate because a directives and types can
// have the same name. For example, a type named "skip".
Expand Down Expand Up @@ -400,7 +400,7 @@ export function extendSchema(
}

function extendFieldMap(type: GraphQLObjectType | GraphQLInterfaceType) {
const newFieldMap = {};
const newFieldMap = Object.create(null);
const oldFieldMap = type.getFields();
Object.keys(oldFieldMap).forEach(fieldName => {
const field = oldFieldMap[fieldName];
Expand Down
29 changes: 29 additions & 0 deletions src/validation/__tests__/OverlappingFieldsCanBeMerged-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,35 @@ describe('Validate: Overlapping fields can be merged', () => {
);
});

it('works for field names that are JS keywords', () => {
const FooType = new GraphQLObjectType({
name: 'Foo',
fields: {
constructor: {
type: GraphQLString
},
}
});

const schemaWithKeywords = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: () => ({
foo: { type: FooType },
})
}),
});

expectPassesRuleWithSchema(
schemaWithKeywords,
OverlappingFieldsCanBeMerged,
`{
foo {
constructor
}
}`
);
});
});

});
4 changes: 2 additions & 2 deletions src/validation/rules/OverlappingFieldsCanBeMerged.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,8 @@ function getFieldsAndFragmentNames(
): [ NodeAndDefCollection, Array<string> ] {
let cached = cachedFieldsAndFragmentNames.get(selectionSet);
if (!cached) {
const nodeAndDefs = {};
const fragmentNames = {};
const nodeAndDefs = Object.create(null);
const fragmentNames = Object.create(null);
_collectFieldsAndFragmentNames(
context,
parentType,
Expand Down
2 changes: 1 addition & 1 deletion src/validation/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class ValidationContext {
frags[statement.name.value] = statement;
}
return frags;
}, {});
}, Object.create(null));
}
return fragments[name];
}
Expand Down