@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
97 lines (94 loc) • 4.36 kB
JavaScript
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');
var _utilsjs = require('../../utils.js');
var _indexjs = require('../relay-connection-types/index.js');
const RULE_ID = "relay-page-info";
const MESSAGE_MUST_EXIST = "MESSAGE_MUST_EXIST";
const MESSAGE_MUST_BE_OBJECT_TYPE = "MESSAGE_MUST_BE_OBJECT_TYPE";
const notPageInfoTypesSelector = `:matches(${_indexjs.NON_OBJECT_TYPES})[name.value=PageInfo] > .name`;
let hasPageInfoChecked = false;
const rule = {
meta: {
type: "problem",
docs: {
category: "Schema",
description: [
"Set of rules to follow Relay specification for `PageInfo` object.",
"",
"- `PageInfo` must be an Object type",
"- `PageInfo` must contain fields `hasPreviousPage` and `hasNextPage`, that return non-null Boolean",
"- `PageInfo` must contain fields `startCursor` and `endCursor`, that return either String or Scalar, which can be null if there are no results"
].join("\n"),
url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
examples: [
{
title: "Correct",
code: (
/* GraphQL */
`
type PageInfo {
hasPreviousPage: Boolean!
hasNextPage: Boolean!
startCursor: String
endCursor: String
}
`
)
}
],
isDisabledForAllConfig: true,
requiresSchema: true
},
messages: {
[MESSAGE_MUST_EXIST]: "The server must provide a `PageInfo` object.",
[MESSAGE_MUST_BE_OBJECT_TYPE]: "`PageInfo` must be an Object type."
},
schema: []
},
create(context) {
const schema = _utilsjs.requireGraphQLSchema.call(void 0, RULE_ID, context);
if (process.env.NODE_ENV === "test" || !hasPageInfoChecked) {
const pageInfoType = schema.getType("PageInfo");
if (!pageInfoType) {
context.report({
loc: _utilsjs.REPORT_ON_FIRST_CHARACTER,
messageId: MESSAGE_MUST_EXIST
});
}
hasPageInfoChecked = true;
}
return {
[notPageInfoTypesSelector](node) {
context.report({ node, messageId: MESSAGE_MUST_BE_OBJECT_TYPE });
},
"ObjectTypeDefinition[name.value=PageInfo]"(node) {
const fieldMap = Object.fromEntries(
_optionalChain([node, 'access', _ => _.fields, 'optionalAccess', _2 => _2.map, 'call', _3 => _3((field) => [field.name.value, field])]) || []
);
const checkField = (fieldName, typeName) => {
const field = fieldMap[fieldName];
let isAllowedType = false;
if (field) {
const type = field.gqlType;
if (typeName === "Boolean") {
isAllowedType = type.kind === _graphql.Kind.NON_NULL_TYPE && type.gqlType.kind === _graphql.Kind.NAMED_TYPE && type.gqlType.name.value === "Boolean";
} else if (type.kind === _graphql.Kind.NAMED_TYPE) {
isAllowedType = type.name.value === "String" || _graphql.isScalarType.call(void 0, schema.getType(type.name.value));
}
}
if (!isAllowedType) {
const returnType = typeName === "Boolean" ? "non-null Boolean" : "either String or Scalar, which can be null if there are no results";
context.report({
node: field ? field.name : node.name,
message: field ? `Field \`${fieldName}\` must return ${returnType}.` : `\`PageInfo\` must contain a field \`${fieldName}\`, that return ${returnType}.`
});
}
};
checkField("hasPreviousPage", "Boolean");
checkField("hasNextPage", "Boolean");
checkField("startCursor", "String");
checkField("endCursor", "String");
}
};
}
};
exports.rule = rule;
;