Skip to content
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

Add addField method for object type #1775

Closed
wants to merge 2 commits into from
Closed
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
66 changes: 66 additions & 0 deletions src/type/__tests__/definition-test.js
Original file line number Diff line number Diff line change
@@ -410,6 +410,40 @@ describe('Type System: Objects', () => {
'AnotherObject must provide "isTypeOf" as a function, but got: {}.',
);
});

describe('Add field correctly', () => {
it('normal fields', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: {},
});
// $DisableFlowOnNegativeTest
objType.addField('add', { name: 'add', type: ScalarType });

expect(objType.getFields()).to.deep.equal({
add: {
name: 'add',
type: ScalarType,
},
});
});

it('thunk fields', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: () => ({}),
});
// $DisableFlowOnNegativeTest
objType.addField('add', { name: 'add', type: ScalarType });

expect(objType.getFields()).to.deep.equal({
add: {
name: 'add',
type: ScalarType,
},
});
});
});
});

describe('Type System: Interfaces', () => {
@@ -699,6 +733,38 @@ describe('Type System: Input Objects', () => {
);
});
});

describe('Add field correctly', () => {
it('normal fields', () => {
const objType = new GraphQLInputObjectType({
name: 'SomeObject',
fields: {},
});
objType.addField('add', { name: 'add', type: ScalarType });

expect(objType.getFields()).to.deep.equal({
add: {
name: 'add',
type: ScalarType,
},
});
});

it('thunk fields', () => {
const objType = new GraphQLInputObjectType({
name: 'SomeObject',
fields: () => ({}),
});
objType.addField('add', { name: 'add', type: ScalarType });

expect(objType.getFields()).to.deep.equal({
add: {
name: 'add',
type: ScalarType,
},
});
});
});
});

describe('Type System: List', () => {
14 changes: 14 additions & 0 deletions src/type/definition.js
Original file line number Diff line number Diff line change
@@ -696,6 +696,13 @@ export class GraphQLObjectType {
return this._fields;
}

addField(name: string, field: GraphQLField<*, *>) {
if (typeof this._fields === 'function') {
this._fields = this._fields();
}
this._fields[name] = field;
}

getInterfaces(): Array<GraphQLInterfaceType> {
if (typeof this._interfaces === 'function') {
this._interfaces = this._interfaces();
@@ -1338,6 +1345,13 @@ export class GraphQLInputObjectType {
return this._fields;
}

addField(name: string, field: GraphQLInputField) {
if (typeof this._fields === 'function') {
this._fields = this._fields();
}
this._fields[name] = field;
}

toConfig(): {|
...GraphQLInputObjectTypeConfig,
fields: GraphQLInputFieldConfigMap,