Skip to content

Commit 8177dbf

Browse files
committed
Appease codecoverage
1 parent 7ea82e2 commit 8177dbf

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

src/validation/__tests__/SingleFieldSubscriptionsRule-test.js

Lines changed: 17 additions & 1 deletion
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.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ export const testSchema: GraphQLSchema = buildSchema(`
157157
directive @onVariableDefinition on VARIABLE_DEFINITION
158158
`);
159159

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

src/validation/rules/SingleFieldSubscriptionsRule.js

Lines changed: 17 additions & 19 deletions
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)