UNPKG

@aws-amplify/api

Version:

Api category of aws-amplify

101 lines (99 loc) • 4.13 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.InternalAPI = exports.InternalAPIClass = void 0; const internals_1 = require("@aws-amplify/api-graphql/internals"); const core_1 = require("@aws-amplify/core"); const utils_1 = require("@aws-amplify/core/internals/utils"); const rxjs_1 = require("rxjs"); /** * NOTE! * * This is used only by DataStore. * * This can probably be pruned and/or removed. Just leaving it as much of the same * state as possible for V6 to reduce number of potentially impactful changes to DataStore. */ /** * @deprecated * Use RestApi or GraphQLAPI to reduce your application bundle size * Export Cloud Logic APIs */ class InternalAPIClass { /** * Initialize API */ constructor() { this.Cache = core_1.Cache; this._graphqlApi = new internals_1.InternalGraphQLAPIClass(); } getModuleName() { return 'InternalAPI'; } /** * to get the operation type * @param operation */ getGraphqlOperationType(operation) { return this._graphqlApi.getGraphqlOperationType(operation); } graphql(options, additionalHeaders, customUserAgentDetails) { const apiUserAgentDetails = { category: utils_1.Category.API, action: utils_1.ApiAction.GraphQl, ...customUserAgentDetails, }; /** * IMPORTANT: do NOT resolve the global context eagerly (e.g. by passing * `getGlobalContext()` directly as an argument). Because this method is * synchronous, an eager call would throw SYNCHRONOUSLY out of `graphql()` * when no global context has been set — breaking callers that expect * setup errors on their async boundary. Most notably, DataStore's * subscription processor expects an Observable whose errors arrive via * `subscribe({ error })`, and query/mutation callers expect a rejected * Promise. * * Instead we determine the operation type first (which does not touch the * context) and defer context resolution to the appropriate boundary: * - subscription -> resolved lazily inside `defer()` so a missing context * surfaces on the Observable's error channel. * - query/mutation -> resolved inside a try/catch so a missing context * surfaces as a rejected Promise. */ const operationType = this.getGraphqlOperationTypeFromOptions(options.query); switch (operationType) { case 'subscription': return (0, rxjs_1.defer)(() => this._graphqlApi.graphql((0, core_1.getGlobalContext)(), options, additionalHeaders, apiUserAgentDetails)); default: // `query` | `mutation` try { return this._graphqlApi.graphql((0, core_1.getGlobalContext)(), options, additionalHeaders, apiUserAgentDetails); } catch (error) { return Promise.reject(error); } } } /** * Resolves the GraphQL operation type from the request options WITHOUT * touching the Amplify context. This lets {@link graphql} pick the correct * lazy-resolution strategy (rejected Promise vs. Observable error channel) * before any context is resolved. */ getGraphqlOperationTypeFromOptions(query) { if (typeof query === 'string') { return this._graphqlApi.getGraphqlOperationType(query); } // `query` is a `DocumentNode`. Read the operation type off its first // operation definition directly, avoiding a `graphql` runtime dependency // in this package. for (const definition of query.definitions) { if (definition.kind === 'OperationDefinition') { return definition.operation; } } throw new Error('invalid operation: no operation definition found'); } } exports.InternalAPIClass = InternalAPIClass; exports.InternalAPI = new InternalAPIClass(); //# sourceMappingURL=InternalAPI.js.map