Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly detect unused object types when using custom schema roots #312

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/rules/defined_types_are_used.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,58 @@
import { ValidationError } from '../validation_error';

export function DefinedTypesAreUsed(context) {
var ignoredTypes = ['Query', 'Mutation', 'Subscription'];
var defaultRootNames = {
Query: 'query',
Mutation: 'mutation',
Subscription: 'subscription',
};
var definedTypes = [];
var referencedTypes = new Set();
var specifiedRoots = new Set();

var recordDefinedType = (node) => {
if (ignoredTypes.indexOf(node.name.value) == -1) {
definedTypes.push(node);
}
definedTypes.push(node);
};

return {
SchemaDefinition: (node) => {
node.operationTypes.forEach((operationType) =>
specifiedRoots.add(operationType.operation)
);
},
SchemaExtension: (node) => {
node.operationTypes.forEach((operationType) =>
specifiedRoots.add(operationType.operation)
);
},
ScalarTypeDefinition: recordDefinedType,
ObjectTypeDefinition: recordDefinedType,
InterfaceTypeDefinition: recordDefinedType,
UnionTypeDefinition: recordDefinedType,
EnumTypeDefinition: recordDefinedType,
InputObjectTypeDefinition: recordDefinedType,

NamedType: (node, key, parent, path, ancestors) => {
NamedType: (node) => {
referencedTypes.add(node.name.value);
},

Document: {
leave: (node) => {
definedTypes.forEach((node) => {
// If a schema root operation type is undefined, we can assume the object type that is named after the operation type is used (https://spec.graphql.org/draft/#sec-Root-Operation-Types.Default-Root-Operation-Type-Names)
//
// For example, if no mutation root is specified, we treat the Mutation object type (if any) as referenced.

let isDefaultRootName =
Object.keys(defaultRootNames).indexOf(node.name.value) > -1;

if (
isDefaultRootName &&
!specifiedRoots.has(defaultRootNames[node.name.value])
) {
referencedTypes.add(node.name.value);
}

if (node.kind == 'ObjectTypeDefinition') {
let implementedInterfaces = node.interfaces.map((node) => {
return node.name.value;
Expand Down
25 changes: 17 additions & 8 deletions test/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,28 @@ export function expectFailsRule(
rule,
schemaSDL,
expectedErrors = [],
configurationOptions = {}
configurationOptions = {},
omitDefaultSchema = false
) {
return expectFailsRuleWithConfiguration(
rule,
schemaSDL,
`${schemaSDL}`,
configurationOptions,
expectedErrors
expectedErrors,
omitDefaultSchema
);
}

export function expectFailsRuleWithConfiguration(
rule,
schemaSDL,
configurationOptions,
expectedErrors = []
expectedErrors = [],
omitDefaultSchema = false
) {
const errors = validateSchemaWithRule(rule, schemaSDL, configurationOptions);
var schema = omitDefaultSchema ? schemaSDL : `${schemaSDL}${DefaultSchema}`;

const errors = validateSchemaWithRule(rule, schema, configurationOptions);

assert(errors.length > 0, "Expected rule to fail but didn't");

Expand All @@ -52,15 +57,19 @@ export function expectFailsRuleWithConfiguration(
}

export function expectPassesRule(rule, schemaSDL, configurationOptions = {}) {
expectPassesRuleWithConfiguration(rule, schemaSDL, configurationOptions);
expectPassesRuleWithConfiguration(rule, `${schemaSDL}`, configurationOptions);
}

export function expectPassesRuleWithConfiguration(
rule,
schemaSDL,
configurationOptions
) {
const errors = validateSchemaWithRule(rule, schemaSDL, configurationOptions);
const errors = validateSchemaWithRule(
rule,
`${schemaSDL}${DefaultSchema}`,
configurationOptions
);

assert(
errors.length == 0,
Expand All @@ -70,7 +79,7 @@ export function expectPassesRuleWithConfiguration(

function validateSchemaWithRule(rule, schemaSDL, configurationOptions) {
const rules = [rule];
const schema = new Schema(`${schemaSDL}${DefaultSchema}`, null);
const schema = new Schema(schemaSDL, null);
const configuration = new Configuration(schema, configurationOptions);
const errors = validateSchemaDefinition(schema, rules, configuration);
const transformedErrors = errors.map((error) => ({
Expand Down
163 changes: 157 additions & 6 deletions test/rules/defined_types_are_used.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ describe('DefinedTypesAreUsed rule', () => {
);
});

it('ignores unreferenced Mutation object type', () => {
it('ignores unreferenced Mutation object type when schema definition is omitted', () => {
expectPassesRule(
DefinedTypesAreUsed,
`
Expand All @@ -190,7 +190,7 @@ describe('DefinedTypesAreUsed rule', () => {
);
});

it('ignores unreferenced Subscription object type', () => {
it('ignores unreferenced Subscription object type when schema definition is omitted', () => {
expectPassesRule(
DefinedTypesAreUsed,
`
Expand All @@ -201,14 +201,165 @@ describe('DefinedTypesAreUsed rule', () => {
);
});

it('ignores unreferenced Query object type', () => {
expectPassesRule(
it('reports unused Mutation object type when schema definition is provided', () => {
expectFailsRule(
DefinedTypesAreUsed,
`
extend type Query {
type Mutation {
c: String
}
`

type Something {
a: String
}

schema {
query: Query
mutation: Something
}
`,
[
{
message:
'The type `Mutation` is defined in the schema but not used anywhere.',
locations: [{ line: 2, column: 7 }],
},
]
);
});

it('reports unused Subscription object type when schema definition is provided', () => {
expectFailsRule(
DefinedTypesAreUsed,
`
type Subscription {
c: String
}

type Something {
a: String
}

schema {
query: Query
subscription: Something
}
`,
[
{
message:
'The type `Subscription` is defined in the schema but not used anywhere.',
locations: [{ line: 2, column: 7 }],
},
]
);
});

it('reports unused Query object type when schema definition is provided', () => {
expectFailsRule(
DefinedTypesAreUsed,
`
type Query {
c: String
}

type Something {
a: String
}

schema {
query: Something
}
`,
[
{
message:
'The type `Query` is defined in the schema but not used anywhere.',
locations: [{ line: 2, column: 7 }],
},
],
{},
true
);
});

it('reports unused Query object type when schema has been extended', () => {
expectFailsRule(
DefinedTypesAreUsed,
`
type Query {
c: String
}

type Something {
a: String
}

extend schema {
query: Something
}
`,
[
{
message:
'The type `Query` is defined in the schema but not used anywhere.',
locations: [{ line: 2, column: 7 }],
},
],
{},
true
);
});

it('reports unused Mutation object type when schema has been extended', () => {
expectFailsRule(
DefinedTypesAreUsed,
`
type Mutation {
a: String
}

type Something {
a: String
}

extend schema {
mutation: Something
}
`,
[
{
message:
'The type `Mutation` is defined in the schema but not used anywhere.',
locations: [{ line: 2, column: 7 }],
},
]
);
});

it('reports unused Subscription object type when schema has been extended', () => {
expectFailsRule(
DefinedTypesAreUsed,
`
type Subscription {
a: String
}

type Something {
a: String
}

extend schema {
subscription: Something
}
`,
[
{
message:
'The type `Subscription` is defined in the schema but not used anywhere.',
locations: [{ line: 2, column: 7 }],
},
]
);
});
});