Skip to content

Commit d4d1bcb

Browse files
committed
don't capitalize "Type"
1 parent 5feb200 commit d4d1bcb

File tree

6 files changed

+274
-21
lines changed

6 files changed

+274
-21
lines changed

src/execution/__tests__/oneof-test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('Execute: Handles OneOf Input Objects', () => {
157157
{
158158
locations: [{ column: 16, line: 2 }],
159159
message:
160-
'Variable "$input" got invalid value { a: "abc", b: 123 }; Within OneOf Input Object Type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
160+
'Variable "$input" got invalid value { a: "abc", b: 123 }; Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
161161
},
162162
],
163163
});
@@ -181,7 +181,7 @@ describe('Execute: Handles OneOf Input Objects', () => {
181181
{
182182
locations: [{ column: 16, line: 2 }],
183183
message:
184-
'Variable "$input" got invalid value { a: "abc", b: null }; Within OneOf Input Object Type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
184+
'Variable "$input" got invalid value { a: "abc", b: null }; Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
185185
},
186186
],
187187
});
@@ -205,7 +205,7 @@ describe('Execute: Handles OneOf Input Objects', () => {
205205
{
206206
locations: [{ column: 16, line: 2 }],
207207
message:
208-
'Variable "$input" got invalid value { a: null, b: null }; Within OneOf Input Object Type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
208+
'Variable "$input" got invalid value { a: null, b: null }; Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
209209
},
210210
],
211211
});

src/execution/__tests__/variables-test.ts

Lines changed: 217 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ import {
2727
GraphQLDirective,
2828
GraphQLIncludeDirective,
2929
} from '../../type/directives.js';
30-
import { GraphQLBoolean, GraphQLString } from '../../type/scalars.js';
30+
import {
31+
GraphQLBoolean,
32+
GraphQLInt,
33+
GraphQLString,
34+
} from '../../type/scalars.js';
3135
import { GraphQLSchema } from '../../type/schema.js';
3236

3337
import { executeSync, experimentalExecuteIncrementally } from '../execute.js';
@@ -90,6 +94,15 @@ const TestNestedInputObject = new GraphQLInputObjectType({
9094
},
9195
});
9296

97+
const TestOneOfInputObject = new GraphQLInputObjectType({
98+
name: 'TestOneOfInputObject',
99+
fields: {
100+
a: { type: GraphQLString },
101+
b: { type: GraphQLInt },
102+
},
103+
isOneOf: true,
104+
});
105+
93106
const TestEnum = new GraphQLEnumType({
94107
name: 'TestEnum',
95108
values: {
@@ -140,6 +153,9 @@ const TestType = new GraphQLObjectType({
140153
type: TestNestedInputObject,
141154
defaultValue: 'Hello World',
142155
}),
156+
fieldWithOneOfObjectInput: fieldWithInputArg({
157+
type: TestOneOfInputObject,
158+
}),
143159
list: fieldWithInputArg({ type: new GraphQLList(GraphQLString) }),
144160
nested: {
145161
type: NestedType,
@@ -1114,6 +1130,206 @@ describe('Execute: Handles inputs', () => {
11141130
});
11151131
});
11161132

1133+
describe('Handles OneOf Input Object types', () => {
1134+
it('allows OneOf Input Object with single field', () => {
1135+
const result = executeQuery(`
1136+
{
1137+
fieldWithOneOfObjectInput(input: { a: "abc" })
1138+
}
1139+
`);
1140+
1141+
expectJSON(result).toDeepEqual({
1142+
data: {
1143+
fieldWithOneOfObjectInput: '{ a: "abc" }',
1144+
},
1145+
});
1146+
});
1147+
});
1148+
1149+
it('errors with OneOf Input Object with more than one field', () => {
1150+
const result = executeQuery(`
1151+
{
1152+
fieldWithOneOfObjectInput(input: { a: "abc", b: 123 })
1153+
}
1154+
`);
1155+
1156+
expectJSON(result).toDeepEqual({
1157+
data: {
1158+
fieldWithOneOfObjectInput: null,
1159+
},
1160+
errors: [
1161+
{
1162+
message:
1163+
'Argument "input" of type "TestOneOfInputObject" has invalid value { a: "abc", b: 123 }.',
1164+
path: ['fieldWithOneOfObjectInput'],
1165+
locations: [{ line: 3, column: 42 }],
1166+
},
1167+
],
1168+
});
1169+
});
1170+
1171+
it('errors with OneOf Input Object with a single null value', () => {
1172+
const result = executeQuery(`
1173+
{
1174+
fieldWithOneOfObjectInput(input: { a: null })
1175+
}
1176+
`);
1177+
1178+
expectJSON(result).toDeepEqual({
1179+
data: {
1180+
fieldWithOneOfObjectInput: null,
1181+
},
1182+
errors: [
1183+
{
1184+
message:
1185+
'Argument "input" of type "TestOneOfInputObject" has invalid value { a: null }.',
1186+
path: ['fieldWithOneOfObjectInput'],
1187+
locations: [{ line: 3, column: 42 }],
1188+
},
1189+
],
1190+
});
1191+
});
1192+
1193+
it('errors with OneOf Input Object with multiple values, only one non-null', () => {
1194+
const result = executeQuery(`
1195+
{
1196+
fieldWithOneOfObjectInput(input: { a: "abc", b: null })
1197+
}
1198+
`);
1199+
1200+
expectJSON(result).toDeepEqual({
1201+
data: {
1202+
fieldWithOneOfObjectInput: null,
1203+
},
1204+
errors: [
1205+
{
1206+
message:
1207+
'Argument "input" of type "TestOneOfInputObject" has invalid value { a: "abc", b: null }.',
1208+
path: ['fieldWithOneOfObjectInput'],
1209+
locations: [{ line: 3, column: 42 }],
1210+
},
1211+
],
1212+
});
1213+
});
1214+
1215+
it('allows a variable for the entire OneOf Object with a single value', () => {
1216+
const result = executeQuery(
1217+
`
1218+
query ($input: TestOneOfInputObject) {
1219+
fieldWithOneOfObjectInput(input: $input)
1220+
}
1221+
`,
1222+
{ input: { a: 'abc' } },
1223+
);
1224+
1225+
expectJSON(result).toDeepEqual({
1226+
data: {
1227+
fieldWithOneOfObjectInput: '{ a: "abc" }',
1228+
},
1229+
});
1230+
});
1231+
1232+
it('allows a variable for the entire OneOf Object with a single defined value', () => {
1233+
const result = executeQuery(
1234+
`
1235+
query ($input: TestOneOfInputObject) {
1236+
fieldWithOneOfObjectInput(input: $input)
1237+
}
1238+
`,
1239+
{ input: { a: 'abc', b: undefined } },
1240+
);
1241+
1242+
expectJSON(result).toDeepEqual({
1243+
data: {
1244+
fieldWithOneOfObjectInput: '{ a: "abc" }',
1245+
},
1246+
});
1247+
});
1248+
1249+
it('errors with variable with no value', () => {
1250+
const result = executeQuery(
1251+
`
1252+
query ($input: TestOneOfInputObject) {
1253+
fieldWithOneOfObjectInput(input: $input)
1254+
}
1255+
`,
1256+
{ input: {} },
1257+
);
1258+
1259+
expectJSON(result).toDeepEqual({
1260+
errors: [
1261+
{
1262+
message:
1263+
'Variable "$input" got invalid value {}; Within OneOf Input Object type "TestOneOfInputObject", exactly one field must be specified, and the value for that field must be non-null.',
1264+
locations: [{ line: 2, column: 16 }],
1265+
},
1266+
],
1267+
});
1268+
});
1269+
1270+
it('errors with variable with multiple values', () => {
1271+
const result = executeQuery(
1272+
`
1273+
query ($input: TestOneOfInputObject) {
1274+
fieldWithOneOfObjectInput(input: $input)
1275+
}
1276+
`,
1277+
{ input: { a: 'abc', b: 123 } },
1278+
);
1279+
1280+
expectJSON(result).toDeepEqual({
1281+
errors: [
1282+
{
1283+
message:
1284+
'Variable "$input" got invalid value { a: "abc", b: 123 }; Within OneOf Input Object type "TestOneOfInputObject", exactly one field must be specified, and the value for that field must be non-null.',
1285+
locations: [{ line: 2, column: 16 }],
1286+
},
1287+
],
1288+
});
1289+
});
1290+
1291+
it('errors with variable with single null value', () => {
1292+
const result = executeQuery(
1293+
`
1294+
query ($input: TestOneOfInputObject) {
1295+
fieldWithOneOfObjectInput(input: $input)
1296+
}
1297+
`,
1298+
{ input: { a: null } },
1299+
);
1300+
1301+
expectJSON(result).toDeepEqual({
1302+
errors: [
1303+
{
1304+
message:
1305+
'Variable "$input" got invalid value null at "input.a"; Within OneOf Input Object type "TestOneOfInputObject", exactly one field must be specified, and the value for that field must be non-null.',
1306+
locations: [{ line: 2, column: 16 }],
1307+
},
1308+
],
1309+
});
1310+
});
1311+
1312+
it('errors with variable with multiple values, only one non-null', () => {
1313+
const result = executeQuery(
1314+
`
1315+
query ($input: TestOneOfInputObject) {
1316+
fieldWithOneOfObjectInput(input: $input)
1317+
}
1318+
`,
1319+
{ input: { a: 'abc', b: null } },
1320+
);
1321+
1322+
expectJSON(result).toDeepEqual({
1323+
errors: [
1324+
{
1325+
message:
1326+
'Variable "$input" got invalid value { a: "abc", b: null }; Within OneOf Input Object type "TestOneOfInputObject", exactly one field must be specified, and the value for that field must be non-null.',
1327+
locations: [{ line: 2, column: 16 }],
1328+
},
1329+
],
1330+
});
1331+
});
1332+
11171333
describe('getVariableValues: limit maximum number of coercion errors', () => {
11181334
const doc = parse(`
11191335
query ($input: [String!]) {

src/utilities/__tests__/coerceInputValue-test.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,24 @@ describe('coerceInputValue', () => {
302302
expectValue(result).to.deep.equal({ foo: 123 });
303303
});
304304

305+
it('returns an error if no fields are specified', () => {
306+
const result = coerceValue({}, TestInputObject);
307+
expectErrors(result).to.deep.equal([
308+
{
309+
error:
310+
'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
311+
path: [],
312+
value: {},
313+
},
314+
]);
315+
});
316+
305317
it('returns an error if more than one field is specified', () => {
306318
const result = coerceValue({ foo: 123, bar: 456 }, TestInputObject);
307319
expectErrors(result).to.deep.equal([
308320
{
309321
error:
310-
'Within OneOf Input Object Type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
322+
'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
311323
path: [],
312324
value: { foo: 123, bar: 456 },
313325
},
@@ -319,25 +331,31 @@ describe('coerceInputValue', () => {
319331
expectErrors(result).to.deep.equal([
320332
{
321333
error:
322-
'Within OneOf Input Object Type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
334+
'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
323335
path: ['bar'],
324336
value: null,
325337
},
326338
]);
327339
});
328340

329-
it('returns an error if more than one field is null', () => {
330-
const result = coerceValue({ foo: null, bar: null }, TestInputObject);
341+
it('returns an error if there are multiple fields with only a single non-null', () => {
342+
const result = coerceValue({ foo: 123, bar: null }, TestInputObject);
331343
expectErrors(result).to.deep.equal([
332344
{
333345
error:
334-
'Within OneOf Input Object Type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
346+
'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
335347
path: [],
336-
value: { foo: null, bar: null },
348+
value: { foo: 123, bar: null },
337349
},
338350
]);
339351
});
340352

353+
// special non-normative graphql-js behavior
354+
it('returns no error for an additional fields with value of undefined', () => {
355+
const result = coerceValue({ foo: 123, bar: undefined }, TestInputObject);
356+
expectValue(result).to.deep.equal({ foo: 123 });
357+
});
358+
341359
it('returns an error for an invalid field', () => {
342360
const result = coerceValue({ foo: NaN }, TestInputObject);
343361
expectErrors(result).to.deep.equal([
@@ -364,7 +382,7 @@ describe('coerceInputValue', () => {
364382
},
365383
{
366384
error:
367-
'Within OneOf Input Object Type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
385+
'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
368386
path: [],
369387
value: { foo: 'abc', bar: 'def' },
370388
},
@@ -385,7 +403,7 @@ describe('coerceInputValue', () => {
385403
},
386404
{
387405
error:
388-
'Within OneOf Input Object Type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
406+
'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
389407
path: [],
390408
value: { foo: 123, unknownField: 123 },
391409
},
@@ -403,7 +421,7 @@ describe('coerceInputValue', () => {
403421
},
404422
{
405423
error:
406-
'Within OneOf Input Object Type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
424+
'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.',
407425
path: [],
408426
value: { bart: 123 },
409427
},
@@ -804,6 +822,25 @@ describe('coerceInputLiteral', () => {
804822
testWithVariables({ var: null }, '$var', nonNullBool, undefined);
805823
});
806824

825+
it('accepts variable values for fields of OneOf Input Objects', () => {
826+
testWithVariables({ a: 'abc' }, '{ a: $a }', testOneOfInputObj, {
827+
a: 'abc',
828+
});
829+
testWithVariables({ a: null }, '{ a: $a }', testOneOfInputObj, undefined);
830+
testWithVariables(
831+
{ a: 'abc', b: 'def' },
832+
'{ a: $a, b: $b }',
833+
testOneOfInputObj,
834+
undefined,
835+
);
836+
testWithVariables(
837+
{ a: 'abc', b: null },
838+
'{ a: $a, b: $b }',
839+
testOneOfInputObj,
840+
undefined,
841+
);
842+
});
843+
807844
it('asserts variables are provided as items in lists', () => {
808845
test('[ $foo ]', listOfBool, [null]);
809846
test('[ $foo ]', listOfNonNullBool, undefined);

0 commit comments

Comments
 (0)