Skip to content

Commit 9f718b8

Browse files
committed
Appease codecoverage
1 parent 9a69bb1 commit 9f718b8

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

src/validation/__tests__/SingleFieldSubscriptionsRule-test.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { describe, it } from 'mocha';
22

33
import { SingleFieldSubscriptionsRule } from '../rules/SingleFieldSubscriptionsRule';
44

5-
import { expectValidationErrors } from './harness';
5+
import {
6+
expectValidationErrors,
7+
expectValidationErrorsWithSchema,
8+
emptySchema,
9+
} from './harness';
610

711
function expectErrors(queryStr: string) {
812
return expectValidationErrors(SingleFieldSubscriptionsRule, queryStr);
@@ -254,4 +258,16 @@ describe('Validate: Subscriptions with single field', () => {
254258
},
255259
]);
256260
});
261+
262+
it('skips if not subscription type', () => {
263+
expectValidationErrorsWithSchema(
264+
emptySchema,
265+
SingleFieldSubscriptionsRule,
266+
`
267+
subscription {
268+
__typename
269+
}
270+
`,
271+
).to.deep.equal([]);
272+
});
257273
});

src/validation/__tests__/harness.ts

+10
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ export const testSchema: GraphQLSchema = buildSchema(`
159159
directive @onVariableDefinition on VARIABLE_DEFINITION
160160
`);
161161

162+
export const emptySchema: GraphQLSchema = buildSchema(`
163+
type QueryRoot {
164+
empty: Boolean
165+
}
166+
167+
schema {
168+
query: QueryRoot
169+
}
170+
`);
171+
162172
export function expectValidationErrorsWithSchema(
163173
schema: GraphQLSchema,
164174
rule: ValidationRule,

src/validation/rules/SingleFieldSubscriptionsRule.ts

+17-19
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import { Kind } from '../../language/kinds';
1010

1111
import type { ValidationContext } from '../ValidationContext';
1212
import type { ExecutionContext } from '../../execution/execute';
13-
import { collectFields } from '../../execution/execute';
14-
15-
function fakeResolver() {
16-
/* noop */
17-
}
13+
import {
14+
collectFields,
15+
defaultFieldResolver,
16+
defaultTypeResolver,
17+
} from '../../execution/execute';
1818

1919
/**
2020
* Subscriptions must only include a non-introspection field.
@@ -50,8 +50,8 @@ export function SingleFieldSubscriptionsRule(
5050
contextValue: undefined,
5151
operation: node,
5252
variableValues,
53-
fieldResolver: fakeResolver,
54-
typeResolver: fakeResolver,
53+
fieldResolver: defaultFieldResolver,
54+
typeResolver: defaultTypeResolver,
5555
errors: [],
5656
};
5757
const fields = collectFields(
@@ -78,18 +78,16 @@ export function SingleFieldSubscriptionsRule(
7878
}
7979
for (const responseKey of Object.keys(fields)) {
8080
const field = fields[responseKey][0];
81-
if (field) {
82-
const fieldName = field.name.value;
83-
if (fieldName[0] === '_' && fieldName[1] === '_') {
84-
context.reportError(
85-
new GraphQLError(
86-
operationName != null
87-
? `Subscription "${operationName}" must not select an introspection top level field.`
88-
: 'Anonymous Subscription must not select an introspection top level field.',
89-
fields[responseKey],
90-
),
91-
);
92-
}
81+
const fieldName = field.name.value;
82+
if (fieldName[0] === '_' && fieldName[1] === '_') {
83+
context.reportError(
84+
new GraphQLError(
85+
operationName != null
86+
? `Subscription "${operationName}" must not select an introspection top level field.`
87+
: 'Anonymous Subscription must not select an introspection top level field.',
88+
fields[responseKey],
89+
),
90+
);
9391
}
9492
}
9593
}

0 commit comments

Comments
 (0)