Skip to content

Commit

Permalink
Appease codecoverage
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed May 27, 2021
1 parent 9a69bb1 commit 9f718b8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
18 changes: 17 additions & 1 deletion src/validation/__tests__/SingleFieldSubscriptionsRule-test.ts
Expand Up @@ -2,7 +2,11 @@ import { describe, it } from 'mocha';

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

import { expectValidationErrors } from './harness';
import {
expectValidationErrors,
expectValidationErrorsWithSchema,
emptySchema,
} from './harness';

function expectErrors(queryStr: string) {
return expectValidationErrors(SingleFieldSubscriptionsRule, queryStr);
Expand Down Expand Up @@ -254,4 +258,16 @@ describe('Validate: Subscriptions with single field', () => {
},
]);
});

it('skips if not subscription type', () => {
expectValidationErrorsWithSchema(
emptySchema,
SingleFieldSubscriptionsRule,
`
subscription {
__typename
}
`,
).to.deep.equal([]);
});
});
10 changes: 10 additions & 0 deletions src/validation/__tests__/harness.ts
Expand Up @@ -159,6 +159,16 @@ export const testSchema: GraphQLSchema = buildSchema(`
directive @onVariableDefinition on VARIABLE_DEFINITION
`);

export const emptySchema: GraphQLSchema = buildSchema(`
type QueryRoot {
empty: Boolean
}
schema {
query: QueryRoot
}
`);

export function expectValidationErrorsWithSchema(
schema: GraphQLSchema,
rule: ValidationRule,
Expand Down
36 changes: 17 additions & 19 deletions src/validation/rules/SingleFieldSubscriptionsRule.ts
Expand Up @@ -10,11 +10,11 @@ import { Kind } from '../../language/kinds';

import type { ValidationContext } from '../ValidationContext';
import type { ExecutionContext } from '../../execution/execute';
import { collectFields } from '../../execution/execute';

function fakeResolver() {
/* noop */
}
import {
collectFields,
defaultFieldResolver,
defaultTypeResolver,
} from '../../execution/execute';

/**
* Subscriptions must only include a non-introspection field.
Expand Down Expand Up @@ -50,8 +50,8 @@ export function SingleFieldSubscriptionsRule(
contextValue: undefined,
operation: node,
variableValues,
fieldResolver: fakeResolver,
typeResolver: fakeResolver,
fieldResolver: defaultFieldResolver,
typeResolver: defaultTypeResolver,
errors: [],
};
const fields = collectFields(
Expand All @@ -78,18 +78,16 @@ export function SingleFieldSubscriptionsRule(
}
for (const responseKey of Object.keys(fields)) {
const field = fields[responseKey][0];
if (field) {
const fieldName = field.name.value;
if (fieldName[0] === '_' && fieldName[1] === '_') {
context.reportError(
new GraphQLError(
operationName != null
? `Subscription "${operationName}" must not select an introspection top level field.`
: 'Anonymous Subscription must not select an introspection top level field.',
fields[responseKey],
),
);
}
const fieldName = field.name.value;
if (fieldName[0] === '_' && fieldName[1] === '_') {
context.reportError(
new GraphQLError(
operationName != null
? `Subscription "${operationName}" must not select an introspection top level field.`
: 'Anonymous Subscription must not select an introspection top level field.',
fields[responseKey],
),
);
}
}
}
Expand Down

0 comments on commit 9f718b8

Please sign in to comment.