UNPKG

@graphql-eslint/eslint-plugin

Version:
143 lines (142 loc) 6.18 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var relay_connection_types_exports = {}; __export(relay_connection_types_exports, { NON_OBJECT_TYPES: () => NON_OBJECT_TYPES, rule: () => rule }); module.exports = __toCommonJS(relay_connection_types_exports); var import_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 = [ import_graphql.Kind.SCALAR_TYPE_DEFINITION, import_graphql.Kind.UNION_TYPE_DEFINITION, import_graphql.Kind.UNION_TYPE_EXTENSION, import_graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION, import_graphql.Kind.INPUT_OBJECT_TYPE_EXTENSION, import_graphql.Kind.ENUM_TYPE_DEFINITION, import_graphql.Kind.ENUM_TYPE_EXTENSION, import_graphql.Kind.INTERFACE_TYPE_DEFINITION, import_graphql.Kind.INTERFACE_TYPE_EXTENSION ]; const notConnectionTypesSelector = `:matches(${NON_OBJECT_TYPES})[name.value=/Connection$/] > .name`; const hasEdgesField = (node) => { var _a; return (_a = node.fields) == null ? void 0 : _a.some((field) => field.name.value === "edges"); }; const hasPageInfoField = (node) => { var _a; return (_a = node.fields) == null ? void 0 : _a.some((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 === import_graphql.Kind.LIST_TYPE || node.kind === import_graphql.Kind.NON_NULL_TYPE && node.gqlType.kind === import_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 === import_graphql.Kind.NON_NULL_TYPE && node.gqlType.kind === import_graphql.Kind.NAMED_TYPE && node.gqlType.name.value === "PageInfo"; if (!isNonNullPageInfoType) { context.report({ node, messageId: PAGE_INFO_FIELD_MUST_RETURN_NON_NULL_TYPE }); } } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { NON_OBJECT_TYPES, rule });