@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
68 lines (61 loc) • 2.76 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');
const RULE_ID = "require-field-of-type-query-in-mutation-result", rule = exports.rule = {
meta: {
type: "suggestion",
docs: {
category: "Schema",
description: "Allow the client in one round-trip not only to call mutation but also to get a wagon of data to update their application.\n> Currently, no errors are reported for result type `union`, `interface` and `scalar`.",
url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
requiresSchema: !0,
examples: [
{
title: "Incorrect",
code: (
/* GraphQL */
`
type User { ... }
type Mutation {
createUser: User!
}
`
)
},
{
title: "Correct",
code: (
/* GraphQL */
`
type User { ... }
type Query { ... }
type CreateUserPayload {
user: User!
query: Query!
}
type Mutation {
createUser: CreateUserPayload!
}
`
)
}
]
},
schema: []
},
create(context) {
const schema = _utilsjs.requireGraphQLSchema.call(void 0, RULE_ID, context), mutationType = schema.getMutationType(), queryType = schema.getQueryType();
return !mutationType || !queryType ? {} : {
[`:matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=${mutationType.name}] > FieldDefinition > .gqlType Name`](node) {
const typeName = node.value, graphQLType = schema.getType(typeName);
if (_graphql.isObjectType.call(void 0, graphQLType)) {
const { fields } = graphQLType.astNode;
_optionalChain([fields, 'optionalAccess', _ => _.some, 'call', _2 => _2((field) => _utilsjs.getTypeName.call(void 0, field) === queryType.name)]) || context.report({
node,
message: `Mutation result type "${graphQLType.name}" must contain field of type "${queryType.name}"`
});
}
}
};
}
};
exports.rule = rule;