UNPKG

graphql

Version:

A Query Language and Runtime which can target any service.

1 lines 2.3 kB
{"version":3,"file":"KnownOperationTypesRule.js","sourceRoot":"","sources":["../../../src/validation/rules/KnownOperationTypesRule.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,qCAAoC;AAiC3D,MAAM,UAAU,uBAAuB,CACrC,OAA0B;IAE1B,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;IACnC,OAAO;QACL,mBAAmB,CAAC,IAAI;YACtB,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;YACjC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE,CAAC;gBACnC,OAAO,CAAC,WAAW,CACjB,IAAI,YAAY,CACd,OAAO,SAAS,4CAA4C,EAC5D,EAAE,KAAK,EAAE,IAAI,EAAE,CAChB,CACF,CAAC;YACJ,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["/** @category Validation Rules */\n\nimport { GraphQLError } from '../../error/GraphQLError.ts';\n\nimport type { ASTVisitor } from '../../language/visitor.ts';\n\nimport type { ValidationContext } from '../ValidationContext.ts';\n\n/**\n * Known Operation Types\n *\n * A GraphQL document is only valid if when it contains an operation,\n * the root type for the operation exists within the schema.\n *\n * See https://spec.graphql.org/draft/#sec-Operation-Type-Existence\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 { parse } from 'graphql/language';\n * import { buildSchema } from 'graphql/utilities';\n * import { validate, KnownOperationTypesRule } from 'graphql/validation';\n *\n * const schema = buildSchema(`\n * type Query {\n * greeting: String\n * }\n * `);\n * const invalidDocument = parse('mutation { greeting }');\n * const validDocument = parse('{ greeting }');\n *\n * validate(schema, invalidDocument, [KnownOperationTypesRule])[0].message; // => 'The mutation operation is not supported by the schema.'\n * validate(schema, validDocument, [KnownOperationTypesRule]); // => []\n * ```\n */\nexport function KnownOperationTypesRule(\n context: ValidationContext,\n): ASTVisitor {\n const schema = context.getSchema();\n return {\n OperationDefinition(node) {\n const operation = node.operation;\n if (!schema.getRootType(operation)) {\n context.reportError(\n new GraphQLError(\n `The ${operation} operation is not supported by the schema.`,\n { nodes: node },\n ),\n );\n }\n },\n };\n}\n"]}