UNPKG

@graphql-eslint/eslint-plugin

Version:
112 lines (108 loc) 5.68 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _graphql = require('graphql'); const MUST_BE_OBJECT_TYPE = "MUST_BE_OBJECT_TYPE"; const MUST_CONTAIN_FIELD_EDGES = "MUST_CONTAIN_FIELD_EDGES"; const MUST_CONTAIN_FIELD_PAGE_INFO = "MUST_CONTAIN_FIELD_PAGE_INFO"; const MUST_HAVE_CONNECTION_SUFFIX = "MUST_HAVE_CONNECTION_SUFFIX"; const EDGES_FIELD_MUST_RETURN_LIST_TYPE = "EDGES_FIELD_MUST_RETURN_LIST_TYPE"; const PAGE_INFO_FIELD_MUST_RETURN_NON_NULL_TYPE = "PAGE_INFO_FIELD_MUST_RETURN_NON_NULL_TYPE"; const NON_OBJECT_TYPES = [ _graphql.Kind.SCALAR_TYPE_DEFINITION, _graphql.Kind.UNION_TYPE_DEFINITION, _graphql.Kind.UNION_TYPE_EXTENSION, _graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION, _graphql.Kind.INPUT_OBJECT_TYPE_EXTENSION, _graphql.Kind.ENUM_TYPE_DEFINITION, _graphql.Kind.ENUM_TYPE_EXTENSION, _graphql.Kind.INTERFACE_TYPE_DEFINITION, _graphql.Kind.INTERFACE_TYPE_EXTENSION ]; const notConnectionTypesSelector = `:matches(${NON_OBJECT_TYPES})[name.value=/Connection$/] > .name`; const hasEdgesField = (node) => _optionalChain([node, 'access', _ => _.fields, 'optionalAccess', _2 => _2.some, 'call', _3 => _3((field) => field.name.value === "edges")]); const hasPageInfoField = (node) => _optionalChain([node, 'access', _4 => _4.fields, 'optionalAccess', _5 => _5.some, 'call', _6 => _6((field) => field.name.value === "pageInfo")]); const rule = { meta: { type: "problem", docs: { category: "Schema", description: [ "Set of rules to follow Relay specification for Connection types.", "", '- Any type whose name ends in "Connection" is considered by spec to be a `Connection type`', "- Connection type must be an Object type", "- Connection type must contain a field `edges` that return a list type that wraps an edge type", "- Connection type must contain a field `pageInfo` that return a non-null `PageInfo` Object type" ].join("\n"), url: "https://the-guild.dev/graphql/eslint/rules/relay-connection-types", isDisabledForAllConfig: true, examples: [ { title: "Incorrect", code: ( /* GraphQL */ ` type UserPayload { # should be an Object type with \`Connection\` suffix edges: UserEdge! # should return a list type pageInfo: PageInfo # should return a non-null \`PageInfo\` Object type } ` ) }, { title: "Correct", code: ( /* GraphQL */ ` type UserConnection { edges: [UserEdge] pageInfo: PageInfo! } ` ) } ] }, messages: { // Connection types [MUST_BE_OBJECT_TYPE]: "Connection type must be an Object type.", [MUST_HAVE_CONNECTION_SUFFIX]: "Connection type must have `Connection` suffix.", [MUST_CONTAIN_FIELD_EDGES]: "Connection type must contain a field `edges` that return a list type.", [MUST_CONTAIN_FIELD_PAGE_INFO]: "Connection type must contain a field `pageInfo` that return a non-null `PageInfo` Object type.", [EDGES_FIELD_MUST_RETURN_LIST_TYPE]: "`edges` field must return a list type.", [PAGE_INFO_FIELD_MUST_RETURN_NON_NULL_TYPE]: "`pageInfo` field must return a non-null `PageInfo` Object type." }, schema: [] }, create(context) { return { [notConnectionTypesSelector](node) { context.report({ node, messageId: MUST_BE_OBJECT_TYPE }); }, ":matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value!=/Connection$/]"(node) { if (hasEdgesField(node) && hasPageInfoField(node)) { context.report({ node: node.name, messageId: MUST_HAVE_CONNECTION_SUFFIX }); } }, ":matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=/Connection$/]"(node) { if (!hasEdgesField(node)) { context.report({ node: node.name, messageId: MUST_CONTAIN_FIELD_EDGES }); } if (!hasPageInfoField(node)) { context.report({ node: node.name, messageId: MUST_CONTAIN_FIELD_PAGE_INFO }); } }, ":matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=/Connection$/] > FieldDefinition[name.value=edges] > .gqlType"(node) { const isListType = node.kind === _graphql.Kind.LIST_TYPE || node.kind === _graphql.Kind.NON_NULL_TYPE && node.gqlType.kind === _graphql.Kind.LIST_TYPE; if (!isListType) { context.report({ node, messageId: EDGES_FIELD_MUST_RETURN_LIST_TYPE }); } }, ":matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=/Connection$/] > FieldDefinition[name.value=pageInfo] > .gqlType"(node) { const isNonNullPageInfoType = node.kind === _graphql.Kind.NON_NULL_TYPE && node.gqlType.kind === _graphql.Kind.NAMED_TYPE && node.gqlType.name.value === "PageInfo"; if (!isNonNullPageInfoType) { context.report({ node, messageId: PAGE_INFO_FIELD_MUST_RETURN_NON_NULL_TYPE }); } } }; } }; exports.NON_OBJECT_TYPES = NON_OBJECT_TYPES; exports.rule = rule;