UNPKG

@aws-amplify/api

Version:

Api category of aws-amplify

1 lines • 5.7 kB
{"version":3,"file":"InternalAPI.mjs","sources":["../../../src/internals/InternalAPI.ts"],"sourcesContent":["import { InternalGraphQLAPIClass } from '@aws-amplify/api-graphql/internals';\nimport { Cache, getGlobalContext } from '@aws-amplify/core';\nimport { ApiAction, Category, } from '@aws-amplify/core/internals/utils';\nimport { defer } from 'rxjs';\n/**\n * NOTE!\n *\n * This is used only by DataStore.\n *\n * This can probably be pruned and/or removed. Just leaving it as much of the same\n * state as possible for V6 to reduce number of potentially impactful changes to DataStore.\n */\n/**\n * @deprecated\n * Use RestApi or GraphQLAPI to reduce your application bundle size\n * Export Cloud Logic APIs\n */\nexport class InternalAPIClass {\n /**\n * Initialize API\n */\n constructor() {\n this.Cache = Cache;\n this._graphqlApi = new InternalGraphQLAPIClass();\n }\n getModuleName() {\n return 'InternalAPI';\n }\n /**\n * to get the operation type\n * @param operation\n */\n getGraphqlOperationType(operation) {\n return this._graphqlApi.getGraphqlOperationType(operation);\n }\n graphql(options, additionalHeaders, customUserAgentDetails) {\n const apiUserAgentDetails = {\n category: Category.API,\n action: ApiAction.GraphQl,\n ...customUserAgentDetails,\n };\n /**\n * IMPORTANT: do NOT resolve the global context eagerly (e.g. by passing\n * `getGlobalContext()` directly as an argument). Because this method is\n * synchronous, an eager call would throw SYNCHRONOUSLY out of `graphql()`\n * when no global context has been set — breaking callers that expect\n * setup errors on their async boundary. Most notably, DataStore's\n * subscription processor expects an Observable whose errors arrive via\n * `subscribe({ error })`, and query/mutation callers expect a rejected\n * Promise.\n *\n * Instead we determine the operation type first (which does not touch the\n * context) and defer context resolution to the appropriate boundary:\n * - subscription -> resolved lazily inside `defer()` so a missing context\n * surfaces on the Observable's error channel.\n * - query/mutation -> resolved inside a try/catch so a missing context\n * surfaces as a rejected Promise.\n */\n const operationType = this.getGraphqlOperationTypeFromOptions(options.query);\n switch (operationType) {\n case 'subscription':\n return defer(() => this._graphqlApi.graphql(getGlobalContext(), options, additionalHeaders, apiUserAgentDetails));\n default:\n // `query` | `mutation`\n try {\n return this._graphqlApi.graphql(getGlobalContext(), options, additionalHeaders, apiUserAgentDetails);\n }\n catch (error) {\n return Promise.reject(error);\n }\n }\n }\n /**\n * Resolves the GraphQL operation type from the request options WITHOUT\n * touching the Amplify context. This lets {@link graphql} pick the correct\n * lazy-resolution strategy (rejected Promise vs. Observable error channel)\n * before any context is resolved.\n */\n getGraphqlOperationTypeFromOptions(query) {\n if (typeof query === 'string') {\n return this._graphqlApi.getGraphqlOperationType(query);\n }\n // `query` is a `DocumentNode`. Read the operation type off its first\n // operation definition directly, avoiding a `graphql` runtime dependency\n // in this package.\n for (const definition of query.definitions) {\n if (definition.kind === 'OperationDefinition') {\n return definition.operation;\n }\n }\n throw new Error('invalid operation: no operation definition found');\n }\n}\nexport const InternalAPI = new InternalAPIClass();\n"],"names":[],"mappings":";;;;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAM,gBAAgB,CAAC;AAC9B;AACA;AACA;AACA,IAAI,WAAW,GAAG;AAClB,QAAQ,IAAI,CAAC,KAAK,GAAG,KAAK;AAC1B,QAAQ,IAAI,CAAC,WAAW,GAAG,IAAI,uBAAuB,EAAE;AACxD,IAAI;AACJ,IAAI,aAAa,GAAG;AACpB,QAAQ,OAAO,aAAa;AAC5B,IAAI;AACJ;AACA;AACA;AACA;AACA,IAAI,uBAAuB,CAAC,SAAS,EAAE;AACvC,QAAQ,OAAO,IAAI,CAAC,WAAW,CAAC,uBAAuB,CAAC,SAAS,CAAC;AAClE,IAAI;AACJ,IAAI,OAAO,CAAC,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAE;AAChE,QAAQ,MAAM,mBAAmB,GAAG;AACpC,YAAY,QAAQ,EAAE,QAAQ,CAAC,GAAG;AAClC,YAAY,MAAM,EAAE,SAAS,CAAC,OAAO;AACrC,YAAY,GAAG,sBAAsB;AACrC,SAAS;AAC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