|
| 1 | +import { Kind, NameNode } from 'graphql'; |
| 2 | +import { getLocation, requireGraphQLSchemaFromContext } from '../utils'; |
| 3 | +import { GraphQLESLintRule } from '../types'; |
| 4 | +import { GraphQLESTreeNode } from '../estree-parser'; |
| 5 | + |
| 6 | +const ROOT_TYPES: ('query' | 'mutation' | 'subscription')[] = ['query', 'mutation', 'subscription']; |
| 7 | + |
| 8 | +type NoRootTypeConfig = { disallow: typeof ROOT_TYPES }; |
| 9 | + |
| 10 | +const rule: GraphQLESLintRule<[NoRootTypeConfig]> = { |
| 11 | + meta: { |
| 12 | + type: 'suggestion', |
| 13 | + docs: { |
| 14 | + category: 'Validation', |
| 15 | + description: 'Disallow using root types for `read-only` or `write-only` schemas.', |
| 16 | + url: 'https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/no-root-type.md', |
| 17 | + requiresSchema: true, |
| 18 | + examples: [ |
| 19 | + { |
| 20 | + title: 'Incorrect (`read-only` schema)', |
| 21 | + usage: [{ disallow: ['mutation', 'subscription'] }], |
| 22 | + code: /* GraphQL */ ` |
| 23 | + type Mutation { |
| 24 | + createUser(input: CreateUserInput!): User! |
| 25 | + } |
| 26 | + `, |
| 27 | + }, |
| 28 | + { |
| 29 | + title: 'Incorrect (`write-only` schema)', |
| 30 | + usage: [{ disallow: ['query'] }], |
| 31 | + code: /* GraphQL */ ` |
| 32 | + type Query { |
| 33 | + users: [User!]! |
| 34 | + } |
| 35 | + `, |
| 36 | + }, |
| 37 | + { |
| 38 | + title: 'Correct (`read-only` schema)', |
| 39 | + usage: [{ disallow: ['mutation', 'subscription'] }], |
| 40 | + code: /* GraphQL */ ` |
| 41 | + type Query { |
| 42 | + users: [User!]! |
| 43 | + } |
| 44 | + `, |
| 45 | + }, |
| 46 | + ], |
| 47 | + optionsForConfig: [{ disallow: ['subscription'] }], |
| 48 | + }, |
| 49 | + schema: { |
| 50 | + type: 'array', |
| 51 | + minItems: 1, |
| 52 | + maxItems: 1, |
| 53 | + items: { |
| 54 | + type: 'object', |
| 55 | + additionalProperties: false, |
| 56 | + required: ['disallow'], |
| 57 | + properties: { |
| 58 | + disallow: { |
| 59 | + type: 'array', |
| 60 | + uniqueItems: true, |
| 61 | + minItems: 1, |
| 62 | + items: { |
| 63 | + enum: ROOT_TYPES, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + create(context) { |
| 71 | + const schema = requireGraphQLSchemaFromContext('no-root-type', context); |
| 72 | + const disallow = new Set(context.options[0].disallow); |
| 73 | + |
| 74 | + const rootTypeNames = [ |
| 75 | + disallow.has('query') && schema.getQueryType(), |
| 76 | + disallow.has('mutation') && schema.getMutationType(), |
| 77 | + disallow.has('subscription') && schema.getSubscriptionType(), |
| 78 | + ] |
| 79 | + .filter(Boolean) |
| 80 | + .map(type => type.name); |
| 81 | + |
| 82 | + if (rootTypeNames.length === 0) { |
| 83 | + return {}; |
| 84 | + } |
| 85 | + |
| 86 | + const selector = [ |
| 87 | + `:matches(${Kind.OBJECT_TYPE_DEFINITION}, ${Kind.OBJECT_TYPE_EXTENSION})`, |
| 88 | + '>', |
| 89 | + `${Kind.NAME}[value=/^(${rootTypeNames.join('|')})$/]`, |
| 90 | + ].join(' '); |
| 91 | + |
| 92 | + return { |
| 93 | + [selector](node: GraphQLESTreeNode<NameNode>) { |
| 94 | + const typeName = node.value; |
| 95 | + context.report({ |
| 96 | + loc: getLocation(node.loc, typeName), |
| 97 | + message: `Root type "${typeName}" is forbidden`, |
| 98 | + }); |
| 99 | + }, |
| 100 | + }; |
| 101 | + }, |
| 102 | +}; |
| 103 | + |
| 104 | +export default rule; |
0 commit comments