Skip to content

Commit f750d46

Browse files
JoviDeCroockyaacovCR
authored andcommitted
Cover empty field-selections (#4228)
Closes #3790 This follows the spec closer to the letter as we consider empty selection-sets invalid as well. This however does not cover empty operation-definitions, which should also be considered invalid. The parser itself considers empty-selections invalid as it requires you to specify a name before a closing curly, hence why testing happens a bit more manual here. Despite our parser not considering this valid, I think this is good to validate when folks use a separate parser or construct documents manually as this is valid in our type-system.
1 parent 8ea7b8f commit f750d46

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/validation/__tests__/ScalarLeafsRule-test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,39 @@ describe('Validate: Scalar leafs', () => {
4242
]);
4343
});
4444

45+
it('object type having only one selection', () => {
46+
const doc: DocumentNode = {
47+
kind: Kind.DOCUMENT,
48+
definitions: [
49+
{
50+
kind: Kind.OPERATION_DEFINITION,
51+
operation: OperationTypeNode.QUERY,
52+
selectionSet: {
53+
kind: Kind.SELECTION_SET,
54+
selections: [
55+
{
56+
kind: Kind.FIELD,
57+
name: { kind: Kind.NAME, value: 'human' },
58+
selectionSet: { kind: Kind.SELECTION_SET, selections: [] },
59+
},
60+
],
61+
},
62+
},
63+
],
64+
};
65+
66+
// We can't leverage expectErrors since it doesn't support passing in the
67+
// documentNode directly. We have to do this because this is technically
68+
// an invalid document.
69+
const errors = validate(testSchema, doc, [ScalarLeafsRule]);
70+
expectJSON(errors).toDeepEqual([
71+
{
72+
message:
73+
'Field "human" of type "Human" must have at least one field selected.',
74+
},
75+
]);
76+
});
77+
4578
it('interface type missing selection', () => {
4679
expectErrors(`
4780
{

0 commit comments

Comments
 (0)