UNPKG

graphql

Version:

A Query Language and Runtime which can target any service.

1 lines 11.5 kB
{"version":3,"file":"graphql.js","sourceRoot":"","sources":["../src/graphql.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,gCAA+B;AAGnD,OAAO,EAAE,kBAAkB,EAAE,uCAAsC;AAQnE,OAAO,EAAE,cAAc,EAAE,4BAA2B;AASpD,OAAO,EAAE,cAAc,EAAE,sBAAqB;AAmJ9C,MAAM,UAAU,OAAO,CAAC,IAAiB;IAEvC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC9D,CAAC;AA0DD,MAAM,UAAU,WAAW,CAAC,IAAiB;IAC3C,MAAM,MAAM,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IAGjC,IAAI,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;IACzE,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,WAAW,CAAC,IAAiB;IACpC,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,cAAc,CAAC;IAC/C,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAGhC,MAAM,sBAAsB,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;IACtD,IAAI,sBAAsB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtC,OAAO,EAAE,MAAM,EAAE,sBAAsB,EAAE,CAAC;IAC5C,CAAC;IAGD,IAAI,QAAQ,CAAC;IACb,IAAI,CAAC;QACH,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACzC,CAAC;IAAC,OAAO,WAAW,EAAE,CAAC;QACrB,OAAO,EAAE,MAAM,EAAE,CAAC,kBAAkB,CAAC,WAAW,CAAC,C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C","sourcesContent":["/** @category Request Pipeline */\n\nimport { isPromise } from './jsutils/isPromise.ts';\nimport type { PromiseOrValue } from './jsutils/PromiseOrValue.ts';\n\nimport { ensureGraphQLError } from './error/ensureGraphQLError.ts';\nimport type { GraphQLError } from './error/GraphQLError.ts';\n\nimport type { DocumentNode } from './language/ast.ts';\nimport type { ParseOptions } from './language/parser.ts';\nimport type { Source } from './language/source.ts';\n\nimport type { GraphQLSchema } from './type/schema.ts';\nimport { validateSchema } from './type/validate.ts';\n\nimport type { ValidationOptions } from './validation/validate.ts';\nimport type { ValidationRule } from './validation/ValidationContext.ts';\n\nimport type { ExecutionArgs } from './execution/ExecutionArgs.ts';\nimport type { ExecutionResult } from './execution/Executor.ts';\n\nimport type { GraphQLHarness } from './harness.ts';\nimport { defaultHarness } from './harness.ts';\n\n/**\n * Describes the input object accepted by `graphql` and `graphqlSync`.\n *\n * These arguments describe the full parse, validate, and execute lifecycle for\n * a GraphQL request. They include parser options, validation options, execution\n * options, and an optional harness for replacing pipeline stages.\n *\n * `graphql` and `graphqlSync` do not support incremental delivery (`@defer` and\n * `@stream`); use `experimentalExecuteIncrementally` after parsing and\n * validating when incremental delivery is required.\n */\nexport interface GraphQLArgs\n extends ParseOptions, ValidationOptions, Omit<ExecutionArgs, 'document'> {\n /**\n * Custom parse, validate, execute, and subscribe functions for this request\n * pipeline.\n */\n harness?: GraphQLHarness | undefined;\n /**\n * A GraphQL language-formatted string or source object representing the\n * requested operation.\n */\n source: string | Source;\n /** Validation rules to use instead of the specified rules. */\n rules?: ReadonlyArray<ValidationRule> | undefined;\n}\n\n/**\n * Parses, validates, and executes a GraphQL document against a schema.\n *\n * This is the primary entry point for fulfilling GraphQL operations. Use this\n * when you want a single-call request lifecycle that returns a promise in all\n * cases.\n *\n * More sophisticated GraphQL servers, such as those which persist queries, may\n * wish to separate the validation and execution phases to a static-time tooling\n * step and a server runtime step.\n * @param args - Request execution arguments, including schema and source.\n * @returns A promise that resolves to an execution result or validation errors.\n * @example\n * ```ts\n * // Execute a complete asynchronous request with variables.\n * import { graphql, buildSchema } from 'graphql';\n *\n * const schema = buildSchema(`\n * type Query {\n * greeting(name: String!): String\n * }\n * `);\n *\n * const result = await graphql({\n * schema,\n * source: 'query SayHello($name: String!) { greeting(name: $name) }',\n * rootValue: {\n * greeting: ({ name }) => `Hello, ${name}!`,\n * },\n * variableValues: { name: 'Ada' },\n * operationName: 'SayHello',\n * });\n *\n * result; // => { data: { greeting: 'Hello, Ada!' } }\n * ```\n * @example\n * ```ts\n * // This variant supplies context plus custom field and type resolvers.\n * import { graphql, buildSchema } from 'graphql';\n *\n * const schema = buildSchema(`\n * interface Named {\n * name: String!\n * }\n *\n * type User implements Named {\n * name: String!\n * }\n *\n * type Query {\n * viewer: Named\n * }\n * `);\n *\n * const result = await graphql({\n * schema,\n * source: '{ viewer { __typename name } }',\n * rootValue: { viewer: { kind: 'user', name: 'Ada' } },\n * contextValue: { locale: 'en' },\n * fieldResolver: (source, _args, context, info) => {\n * context.locale; // => 'en'\n * return source[info.fieldName];\n * },\n * typeResolver: (value) => {\n * return value.kind === 'user' ? 'User' : undefined;\n * },\n * });\n *\n * result; // => { data: { viewer: { __typename: 'User', name: 'Ada' } } }\n * ```\n * @example\n * ```ts\n * // This variant customizes the request pipeline with a harness.\n * import { buildSchema, defaultHarness, graphql } from 'graphql';\n *\n * const schema = buildSchema(`\n * type Query {\n * greeting: String\n * }\n * `);\n * const stages = [];\n * const abortController = new AbortController();\n * const harness = {\n * parse: (...args) => {\n * stages.push('parse');\n * return defaultHarness.parse(...args);\n * },\n * validate: (...args) => {\n * stages.push('validate');\n * return defaultHarness.validate(...args);\n * },\n * execute: (...args) => {\n * stages.push('execute');\n * return defaultHarness.execute(...args);\n * },\n * subscribe: (...args) => {\n * stages.push('subscribe');\n * return defaultHarness.subscribe(...args);\n * },\n * };\n *\n * const result = await graphql({\n * schema,\n * source: '{ greeting }',\n * rootValue: { greeting: 'Hello' },\n * rules: [],\n * maxErrors: 25,\n * hideSuggestions: true,\n * noLocation: true,\n * abortSignal: abortController.signal,\n * harness,\n * });\n *\n * result; // => { data: { greeting: 'Hello' } }\n * stages; // => ['parse', 'validate', 'execute']\n * ```\n * @category Request Pipeline\n */\nexport function graphql(args: GraphQLArgs): Promise<ExecutionResult> {\n // Always return a Promise for a consistent API.\n return new Promise((resolve) => resolve(graphqlImpl(args)));\n}\n\n/**\n * Parses, validates, and executes a GraphQL document synchronously.\n *\n * This function guarantees that execution completes synchronously, or throws an\n * error, assuming that all field resolvers are also synchronous. It throws when\n * any resolver returns a promise.\n * @param args - Request execution arguments, including schema and source.\n * @returns Completed execution output, or request errors if parsing or\n * validation fails.\n * @example\n * ```ts\n * // Execute a complete synchronous request with variables.\n * import { graphqlSync, buildSchema } from 'graphql';\n *\n * const schema = buildSchema(`\n * type Query {\n * greeting(name: String!): String\n * }\n * `);\n *\n * const result = graphqlSync({\n * schema,\n * source: 'query SayHello($name: String!) { greeting(name: $name) }',\n * rootValue: {\n * greeting: ({ name }) => `Hello, ${name}!`,\n * },\n * variableValues: { name: 'Ada' },\n * operationName: 'SayHello',\n * });\n *\n * result; // => { data: { greeting: 'Hello, Ada!' } }\n * ```\n * @example\n * ```ts\n * // This variant uses a synchronous custom field resolver and context.\n * import { graphqlSync, buildSchema } from 'graphql';\n *\n * const schema = buildSchema(`\n * type Query {\n * greeting: String\n * }\n * `);\n *\n * const result = graphqlSync({\n * schema,\n * source: '{ greeting }',\n * fieldResolver: (_source, _args, contextValue) => {\n * return contextValue.defaultGreeting;\n * },\n * contextValue: { defaultGreeting: 'Hello' },\n * });\n *\n * result; // => { data: { greeting: 'Hello' } }\n * ```\n * @category Request Pipeline\n */\nexport function graphqlSync(args: GraphQLArgs): ExecutionResult {\n const result = graphqlImpl(args);\n\n // Assert that the execution was synchronous.\n if (isPromise(result)) {\n throw new Error('GraphQL execution failed to complete synchronously.');\n }\n\n return result;\n}\n\nfunction graphqlImpl(args: GraphQLArgs): PromiseOrValue<ExecutionResult> {\n const harness = args.harness ?? defaultHarness;\n const { schema, source } = args;\n\n // Validate Schema\n const schemaValidationErrors = validateSchema(schema);\n if (schemaValidationErrors.length > 0) {\n return { errors: schemaValidationErrors };\n }\n\n // Parse\n let document;\n try {\n document = harness.parse(source, args);\n } catch (syntaxError) {\n return { errors: [ensureGraphQLError(syntaxError)] };\n }\n\n if (isPromise(document)) {\n return document.then(\n (resolvedDocument) =>\n validateAndExecute(harness, args, schema, resolvedDocument),\n (syntaxError: unknown) => ({ errors: [ensureGraphQLError(syntaxError)] }),\n );\n }\n\n return validateAndExecute(harness, args, schema, document);\n}\n\nfunction validateAndExecute(\n harness: GraphQLHarness,\n args: GraphQLArgs,\n schema: GraphQLSchema,\n document: DocumentNode,\n): PromiseOrValue<ExecutionResult> {\n // Validate\n const validationResult = harness.validate(schema, document, args.rules, args);\n\n if (isPromise(validationResult)) {\n return validationResult.then((resolvedValidationResult) =>\n checkValidationAndExecute(\n harness,\n args,\n resolvedValidationResult,\n document,\n ),\n );\n }\n\n return checkValidationAndExecute(harness, args, validationResult, document);\n}\n\nfunction checkValidationAndExecute(\n harness: GraphQLHarness,\n args: GraphQLArgs,\n validationResult: ReadonlyArray<GraphQLError>,\n document: DocumentNode,\n): PromiseOrValue<ExecutionResult> {\n if (validationResult.length > 0) {\n return { errors: validationResult };\n }\n\n // Execute\n return harness.execute({ ...args, document });\n}\n"]}