graphql
Version:
A Query Language and Runtime which can target any service.
1 lines • 5.31 kB
Source Map (JSON)
{"version":3,"file":"NoDeprecatedCustomRule.js","sourceRoot":"","sources":["../../../../src/validation/rules/custom/NoDeprecatedCustomRule.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,wCAAuC;AAI9D,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,qCAAoC;AAwD9E,MAAM,UAAU,sBAAsB,CAAC,OAA0B;IAC/D,OAAO;QACL,KAAK,CAAC,IAAI;YACR,MAAM,QAAQ,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;YACvC,MAAM,iBAAiB,GAAG,QAAQ,EAAE,iBAAiB,CAAC;YACtD,IAAI,QAAQ,IAAI,iBAAiB,IAAI,IAAI,EAAE,CAAC;gBAC1C,OAAO,CAAC,WAAW,CACjB,IAAI,YAAY,CACd,aAAa,QAAQ,mBAAmB,iBAAiB,EAAE,EAC3D,EAAE,KAAK,EAAE,IAAI,EAAE,CAChB,CACF,CAAC;YACJ,CAAC;QACH,CAAC;QACD,QAAQ,CAAC,IAAI;YACX,MAAM,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;YACrC,MAAM,iBAAiB,GAAG,MAAM,EAAE,iBAAiB,CAAC;YACpD,IAAI,MAAM,IAAI,iBAAiB,IAAI,IAAI,EAAE,CAAC;gBACxC,OAAO,CAAC,WAAW,CACjB,IAAI,YAAY,CACd,iBAAiB,MAAM,oBAAoB,iBAAiB,EAAE,EAC9D,EAAE,KAAK,EAAE,IAAI,EAAE,CAChB,CACF,CAAC;YACJ,CAAC;QACH,CAAC;QACD,WAAW,CAAC,IAAI;YACd,MAAM,cAAc,GAAG,YAAY,CAAC,OAAO,CAAC,kBAAkB,EAAE,CAAC,CAAC;YAClE,IAAI,iBAAiB,CAAC,cAAc,CAAC,EAAE,CAAC;gBACtC,MAAM,aAAa,GAAG,cAAc,CAAC,SAAS,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;gBAClE,MAAM,iBAAiB,GAAG,aAAa,EAAE,iBAAiB,CAAC;gBAC3D,IAAI,iBAAiB,IAAI,IAAI,EAAE,CAAC;oBAC9B,OAAO,CAAC,WAAW,CACjB,IAAI,YAAY,CACd,mBAAmB,aAAa,mBAAmB,iBAAiB,EAAE,EACtE,EAAE,KAAK,EAAE,IAAI,EAAE,CAChB,CACF,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC;QACD,SAAS,CAAC,IAAI;YACZ,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;YAC5C,MAAM,iBAAiB,GAAG,YAAY,EAAE,iBAAiB,CAAC;YAC1D,IAAI,YAAY,IAAI,iBAAiB,IAAI,IAAI,EAAE,CAAC;gBAC9C,OAAO,CAAC,WAAW,CACjB,IAAI,YAAY,CACd,mBAAmB,YAAY,oBAAoB,iBAAiB,EAAE,EACtE,EAAE,KAAK,EAAE,IAAI,EAAE,CAChB,CACF,CAAC;YACJ,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["/** @category Custom Rules */\n\nimport { GraphQLError } from '../../../error/GraphQLError.ts';\n\nimport type { ASTVisitor } from '../../../language/visitor.ts';\n\nimport { getNamedType, isInputObjectType } from '../../../type/definition.ts';\n\nimport type { ValidationContext } from '../../ValidationContext.ts';\n\n/**\n * No deprecated\n *\n * A GraphQL document is only valid if all selected fields and all used enum values have not been\n * deprecated.\n *\n * Note: This rule is optional and is not part of the Validation section of the GraphQL\n * Specification. The main purpose of this rule is detection of deprecated usages and not\n * necessarily to forbid their use when querying a service.\n * @param context - The validation context used while checking the document.\n * @returns A visitor that reports validation errors for this rule.\n * @example\n * ```ts\n * import {\n * GraphQLObjectType,\n * GraphQLSchema,\n * GraphQLString,\n * parse,\n * validate,\n * } from 'graphql';\n * import { NoDeprecatedCustomRule } from 'graphql/validation';\n *\n * const schema = new GraphQLSchema({\n * query: new GraphQLObjectType({\n * name: 'Query',\n * fields: {\n * name: { type: GraphQLString },\n * oldName: {\n * type: GraphQLString,\n * deprecationReason: 'Use name instead.',\n * },\n * },\n * }),\n * });\n *\n * const invalidDocument = parse(`\n * { oldName }\n * `);\n * const invalidErrors = validate(schema, invalidDocument, [\n * NoDeprecatedCustomRule,\n * ]);\n *\n * invalidErrors.length; // => 1\n *\n * const validDocument = parse(`\n * { name }\n * `);\n * const validErrors = validate(schema, validDocument, [NoDeprecatedCustomRule]);\n *\n * validErrors; // => []\n * ```\n */\nexport function NoDeprecatedCustomRule(context: ValidationContext): ASTVisitor {\n return {\n Field(node) {\n const fieldDef = context.getFieldDef();\n const deprecationReason = fieldDef?.deprecationReason;\n if (fieldDef && deprecationReason != null) {\n context.reportError(\n new GraphQLError(\n `The field ${fieldDef} is deprecated. ${deprecationReason}`,\n { nodes: node },\n ),\n );\n }\n },\n Argument(node) {\n const argDef = context.getArgument();\n const deprecationReason = argDef?.deprecationReason;\n if (argDef && deprecationReason != null) {\n context.reportError(\n new GraphQLError(\n `The argument \"${argDef}\" is deprecated. ${deprecationReason}`,\n { nodes: node },\n ),\n );\n }\n },\n ObjectField(node) {\n const inputObjectDef = getNamedType(context.getParentInputType());\n if (isInputObjectType(inputObjectDef)) {\n const inputFieldDef = inputObjectDef.getFields()[node.name.value];\n const deprecationReason = inputFieldDef?.deprecationReason;\n if (deprecationReason != null) {\n context.reportError(\n new GraphQLError(\n `The input field ${inputFieldDef} is deprecated. ${deprecationReason}`,\n { nodes: node },\n ),\n );\n }\n }\n },\n EnumValue(node) {\n const enumValueDef = context.getEnumValue();\n const deprecationReason = enumValueDef?.deprecationReason;\n if (enumValueDef && deprecationReason != null) {\n context.reportError(\n new GraphQLError(\n `The enum value \"${enumValueDef}\" is deprecated. ${deprecationReason}`,\n { nodes: node },\n ),\n );\n }\n },\n };\n}\n"]}