Skip to content

Do not mutate passed field map in ObjectType and ObjectInput #132

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
Aug 18, 2015
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
65 changes: 65 additions & 0 deletions src/type/__tests__/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,69 @@ describe('Type System: Example', () => {
);
});
});

it('does not mutate passed field definitions', () => {
const fields = {
field1: {
type: GraphQLString,
},
field2: {
type: GraphQLString,
args: {
id: {
type: GraphQLString
}
}
}
};
const testObject1 = new GraphQLObjectType({
name: 'Test1',
fields,
});
const testObject2 = new GraphQLObjectType({
name: 'Test2',
fields,
});

expect(testObject1.getFields()).to.deep.equal(testObject2.getFields());
expect(fields).to.deep.equal({
field1: {
type: GraphQLString,
},
field2: {
type: GraphQLString,
args: {
id: {
type: GraphQLString
}
}
}
});

const testInputObject1 = new GraphQLInputObjectType({
name: 'Test1',
fields
});
const testInputObject2 = new GraphQLInputObjectType({
name: 'Test2',
fields
});

expect(testInputObject1.getFields()).to.deep.equal(
testInputObject2.getFields()
);
expect(fields).to.deep.equal({
field1: {
type: GraphQLString,
},
field2: {
type: GraphQLString,
args: {
id: {
type: GraphQLString
}
}
}
});
});
});
22 changes: 16 additions & 6 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,20 @@ function defineFieldMap(
`${type} fields must be an object with field names as keys or a ` +
`function which returns such an object.`
);

var fieldNames = Object.keys(fieldMap);
invariant(
fieldNames.length > 0,
`${type} fields must be an object with field names as keys or a ` +
`function which returns such an object.`
);

var resultFieldMap = {};
fieldNames.forEach(fieldName => {
var field = fieldMap[fieldName];
field.name = fieldName;
var field = {
...fieldMap[fieldName],
name: fieldName
};
invariant(
isOutputType(field.type),
`${type}.${fieldName} field type must be Output Type but ` +
Expand Down Expand Up @@ -408,8 +413,9 @@ function defineFieldMap(
};
});
}
resultFieldMap[fieldName] = field;
});
return fieldMap;
return resultFieldMap;
}

function isPlainObj(obj) {
Expand Down Expand Up @@ -897,16 +903,20 @@ export class GraphQLInputObjectType {
`${this} fields must be an object with field names as keys or a ` +
`function which returns such an object.`
);
var resultFieldMap = {};
fieldNames.forEach(fieldName => {
var field = fieldMap[fieldName];
field.name = fieldName;
var field = {
...fieldMap[fieldName],
name: fieldName
};
invariant(
isInputType(field.type),
`${this}.${fieldName} field type must be Input Type but ` +
`got: ${field.type}.`
);
resultFieldMap[fieldName] = field;
});
return fieldMap;
return resultFieldMap;
}

toString(): string {
Expand Down