Skip to content

Commit

Permalink
[RFC] Additional optional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
leebyron committed Oct 21, 2015
1 parent defbb03 commit 08ffd09
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
39 changes: 36 additions & 3 deletions src/type/__tests__/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ describe('Objects must adhere to Interface they implement', () => {
}).not.to.throw();
});

it('rejects an Object which implements an Interface field along with more arguments', () => {
it('accepts an Object which implements an Interface field along with additional optional arguments', () => {
expect(() => {
var AnotherInterface = new GraphQLInterfaceType({
name: 'AnotherInterface',
Expand Down Expand Up @@ -1492,10 +1492,43 @@ describe('Objects must adhere to Interface they implement', () => {
}
});

return schemaWithFieldType(AnotherObject);
}).not.to.throw();
});

it('rejects an Object which implements an Interface field along with additional required arguments', () => {
expect(() => {
var AnotherInterface = new GraphQLInterfaceType({
name: 'AnotherInterface',
resolveType: () => null,
fields: {
field: {
type: GraphQLString,
args: {
input: { type: GraphQLString },
}
}
}
});

var AnotherObject = new GraphQLObjectType({
name: 'AnotherObject',
interfaces: [ AnotherInterface ],
fields: {
field: {
type: GraphQLString,
args: {
input: { type: GraphQLString },
anotherInput: { type: new GraphQLNonNull(GraphQLString) },
}
}
}
});

return schemaWithFieldType(AnotherObject);
}).to.throw(
'AnotherInterface.field does not define argument "anotherInput" but ' +
'AnotherObject.field provides it.'
'AnotherObject.field(anotherInput:) is of required type "String!" but ' +
'is not also provided by the interface AnotherInterface.field.'
);
});

Expand Down
15 changes: 9 additions & 6 deletions src/type/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,18 @@ function assertObjectImplementsInterface(
);
});

// Assert argument set invariance.
// Assert additional arguments must not be required.
objectField.args.forEach(objectArg => {
var argName = objectArg.name;
var ifaceArg = find(ifaceField.args, arg => arg.name === argName);
invariant(
ifaceArg,
`${iface}.${fieldName} does not define argument "${argName}" but ` +
`${object}.${fieldName} provides it.`
);
if (!ifaceArg) {
invariant(
!(objectArg.type instanceof GraphQLNonNull),
`${object}.${fieldName}(${argName}:) is of required type ` +
`"${objectArg.type}" but is not also provided by the ` +
`interface ${iface}.${fieldName}.`
);
}
});
});
}
Expand Down

0 comments on commit 08ffd09

Please sign in to comment.