UNPKG

@graphql-eslint/eslint-plugin

Version:
112 lines (111 loc) 5.76 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: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__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: !0 }), 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", MUST_CONTAIN_FIELD_EDGES = "MUST_CONTAIN_FIELD_EDGES", MUST_CONTAIN_FIELD_PAGE_INFO = "MUST_CONTAIN_FIELD_PAGE_INFO", MUST_HAVE_CONNECTION_SUFFIX = "MUST_HAVE_CONNECTION_SUFFIX", EDGES_FIELD_MUST_RETURN_LIST_TYPE = "EDGES_FIELD_MUST_RETURN_LIST_TYPE", PAGE_INFO_FIELD_MUST_RETURN_NON_NULL_TYPE = "PAGE_INFO_FIELD_MUST_RETURN_NON_NULL_TYPE", 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 ], notConnectionTypesSelector = `:matches(${NON_OBJECT_TYPES})[name.value=/Connection$/] > .name`, hasEdgesField = (node) => node.fields?.some((field) => field.name.value === "edges"), hasPageInfoField = (node) => node.fields?.some((field) => field.name.value === "pageInfo"), 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(` `), url: "https://the-guild.dev/graphql/eslint/rules/relay-connection-types", isDisabledForAllConfig: !0, 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) { hasEdgesField(node) && hasPageInfoField(node) && context.report({ node: node.name, messageId: MUST_HAVE_CONNECTION_SUFFIX }); }, ":matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=/Connection$/]"(node) { hasEdgesField(node) || context.report({ node: node.name, messageId: MUST_CONTAIN_FIELD_EDGES }), 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) { node.kind === import_graphql.Kind.LIST_TYPE || node.kind === import_graphql.Kind.NON_NULL_TYPE && node.gqlType.kind === import_graphql.Kind.LIST_TYPE || context.report({ node, messageId: EDGES_FIELD_MUST_RETURN_LIST_TYPE }); }, ":matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=/Connection$/] > FieldDefinition[name.value=pageInfo] > .gqlType"(node) { node.kind === import_graphql.Kind.NON_NULL_TYPE && node.gqlType.kind === import_graphql.Kind.NAMED_TYPE && node.gqlType.name.value === "PageInfo" || 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 });