UNPKG

11.7 kBSource Map (JSON)View Raw
1{"version":3,"file":"batchHttpLink.js","sourceRoot":"","sources":["../../../src/link/batch-http/batchHttpLink.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAA0B,MAAM,SAAS,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EACL,uBAAuB,EACvB,SAAS,EACT,yBAAyB,EACzB,YAAY,EACZ,gCAAgC,EAChC,cAAc,EACd,kBAAkB,EAElB,uBAAuB,GACxB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAarC;IAAmC,iCAAU;IAM3C,uBAAY,WAAmC;QAA/C,YACE,iBAAO,SAwLR;QAtLC,IAAI,KAYA,WAAW,IAAK,EAA4B,EAX9C,WAAgB,EAAhB,GAAG,mBAAG,UAAU,KAAA,EAET,OAAO,WAAA,EACd,aAAsB,EAAtB,KAAK,mBAAG,cAAc,KAAA,EACtB,iBAAiB,uBAAA,EACjB,kBAAkB,wBAAA,EAClB,aAAa,mBAAA,EACb,aAAa,mBAAA,EACb,QAAQ,cAAA,EACR,QAAQ,cAAA,EACL,cAAc,cAXf,8HAYH,CAA+C,CAAC;QAGjD,YAAY,CAAC,OAAO,CAAC,CAAC;QAKtB,IAAI,CAAC,OAAO,EAAE;YACZ,OAAO,GAAG,KAAK,CAAC;SACjB;QAED,IAAM,UAAU,GAAG;YACjB,IAAI,EAAE,EAAE,iBAAiB,mBAAA,EAAE,kBAAkB,oBAAA,EAAE;YAC/C,OAAO,EAAE,cAAc,CAAC,YAAY;YACpC,WAAW,EAAE,cAAc,CAAC,WAAW;YACvC,OAAO,EAAE,cAAc,CAAC,OAAO;SAChC,CAAC;QAEF,KAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,KAAI,CAAC,aAAa,GAAG,aAAa,IAAI,EAAE,CA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MAAM,CAAC,CAAC;qBAC3B;oBAED,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBACtB,CAAC,CAAC,CAAC;gBAEL,OAAO;oBAGL,IAAI,UAAU;wBAAE,UAAU,CAAC,KAAK,EAAE,CAAC;gBACrC,CAAC,CAAC;YACJ,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,QAAQ;YACN,QAAQ;gBACR,CAAC,UAAC,SAAoB;oBACpB,IAAM,OAAO,GAAG,SAAS,CAAC,UAAU,EAAE,CAAC;oBAEvC,IAAM,aAAa,GAAG;wBACpB,IAAI,EAAE,OAAO,CAAC,IAAI;wBAClB,OAAO,EAAE,OAAO,CAAC,YAAY;wBAC7B,WAAW,EAAE,OAAO,CAAC,WAAW;wBAChC,OAAO,EAAE,OAAO,CAAC,OAAO;qBACzB,CAAC;oBAGF,OAAO,SAAS,CAAC,SAAS,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC;gBACnE,CAAC,CAAC,CAAC;QAEL,KAAI,CAAC,OAAO,GAAG,IAAI,SAAS,CAAC;YAC3B,aAAa,EAAE,KAAI,CAAC,aAAa;YACjC,aAAa,EAAE,KAAI,CAAC,aAAa;YACjC,QAAQ,EAAE,KAAI,CAAC,QAAQ;YACvB,QAAQ,UAAA;YACR,YAAY,cAAA;SACb,CAAC,CAAC;;IACL,CAAC;IAEM,+BAAO,GAAd,UAAe,SAAoB;QACjC,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACzC,CAAC;IACH,oBAAC;AAAD,CAAC,AApMD,CAAmC,UAAU,GAoM5C","sourcesContent":["import { ApolloLink, Operation, FetchResult } from '../core';\nimport { Observable } from '../../utilities';\nimport { fromError } from '../utils';\nimport {\n serializeFetchParameter,\n selectURI,\n parseAndCheckHttpResponse,\n checkFetcher,\n selectHttpOptionsAndBodyInternal,\n defaultPrinter,\n fallbackHttpConfig,\n HttpOptions,\n createSignalIfSupported,\n} from '../http';\nimport { BatchLink } from '../batch';\n\nexport namespace BatchHttpLink {\n export type Options = Pick<\n BatchLink.Options,\n 'batchMax' | 'batchDebounce' | 'batchInterval' | 'batchKey'\n > & HttpOptions;\n}\n\n/**\n * Transforms Operation for into HTTP results.\n * context can include the headers property, which will be passed to the fetch function\n */\nexport class BatchHttpLink extends ApolloLink {\n private batchDebounce?: boolean;\n private batchInterval: number;\n private batchMax: number;\n private batcher: ApolloLink;\n\n constructor(fetchParams?: BatchHttpLink.Options) {\n super();\n\n let {\n uri = '/graphql',\n // use default global fetch if nothing is passed in\n fetch: fetcher,\n print = defaultPrinter,\n includeExtensions,\n preserveHeaderCase,\n batchInterval,\n batchDebounce,\n batchMax,\n batchKey,\n ...requestOptions\n } = fetchParams || ({} as BatchHttpLink.Options);\n\n // dev warnings to ensure fetch is present\n checkFetcher(fetcher);\n\n //fetcher is set here rather than the destructuring to ensure fetch is\n //declared before referencing it. Reference in the destructuring would cause\n //a ReferenceError\n if (!fetcher) {\n fetcher = fetch;\n }\n\n const linkConfig = {\n http: { includeExtensions, preserveHeaderCase },\n options: requestOptions.fetchOptions,\n credentials: requestOptions.credentials,\n headers: requestOptions.headers,\n };\n\n this.batchDebounce = batchDebounce;\n this.batchInterval = batchInterval || 10;\n this.batchMax = batchMax || 10;\n\n const batchHandler = (operations: Operation[]) => {\n const chosenURI = selectURI(operations[0], uri);\n\n const context = operations[0].getContext();\n\n const clientAwarenessHeaders: {\n 'apollographql-client-name'?: string;\n 'apollographql-client-version'?: string;\n } = {};\n if (context.clientAwareness) {\n const { name, version } = context.clientAwareness;\n if (name) {\n clientAwarenessHeaders['apollographql-client-name'] = name;\n }\n if (version) {\n clientAwarenessHeaders['apollographql-client-version'] = version;\n }\n }\n\n const contextConfig = {\n http: context.http,\n options: context.fetchOptions,\n credentials: context.credentials,\n headers: { ...clientAwarenessHeaders, ...context.headers },\n };\n\n //uses fallback, link, and then context to build options\n const optsAndBody = operations.map(operation =>\n selectHttpOptionsAndBodyInternal(\n operation,\n print,\n fallbackHttpConfig,\n linkConfig,\n contextConfig,\n ),\n );\n\n const loadedBody = optsAndBody.map(({ body }) => body);\n const options = optsAndBody[0].options;\n\n // There's no spec for using GET with batches.\n if (options.method === 'GET') {\n return fromError<FetchResult[]>(\n new Error('apollo-link-batch-http does not support GET requests'),\n );\n }\n\n try {\n (options as any).body = serializeFetchParameter(loadedBody, 'Payload');\n } catch (parseError) {\n return fromError<FetchResult[]>(parseError);\n }\n\n let controller: any;\n if (!(options as any).signal) {\n const { controller: _controller, signal } = createSignalIfSupported();\n controller = _controller;\n if (controller) (options as any).signal = signal;\n }\n\n return new Observable<FetchResult[]>(observer => {\n fetcher!(chosenURI, options)\n .then(response => {\n // Make the raw response available in the context.\n operations.forEach(operation => operation.setContext({ response }));\n return response;\n })\n .then(parseAndCheckHttpResponse(operations))\n .then(result => {\n // we have data and can send it to back up the link chain\n observer.next(result);\n observer.complete();\n return result;\n })\n .catch(err => {\n // fetch was cancelled so its already been cleaned up in the unsubscribe\n if (err.name === 'AbortError') return;\n // if it is a network error, BUT there is graphql result info\n // fire the next observer before calling error\n // this gives apollo-client (and react-apollo) the `graphqlErrors` and `networkErrors`\n // to pass to UI\n // this should only happen if we *also* have data as part of the response key per\n // the spec\n if (err.result && err.result.errors && err.result.data) {\n // if we dont' call next, the UI can only show networkError because AC didn't\n // get andy graphqlErrors\n // this is graphql execution result info (i.e errors and possibly data)\n // this is because there is no formal spec how errors should translate to\n // http status codes. So an auth error (401) could have both data\n // from a public field, errors from a private field, and a status of 401\n // {\n // user { // this will have errors\n // firstName\n // }\n // products { // this is public so will have data\n // cost\n // }\n // }\n //\n // the result of above *could* look like this:\n // {\n // data: { products: [{ cost: \"$10\" }] },\n // errors: [{\n // message: 'your session has timed out',\n // path: []\n // }]\n // }\n // status code of above would be a 401\n // in the UI you want to show data where you can, errors as data where you can\n // and use correct http status codes\n observer.next(err.result);\n }\n\n observer.error(err);\n });\n\n return () => {\n // XXX support canceling this request\n // https://developers.google.com/web/updates/2017/09/abortable-fetch\n if (controller) controller.abort();\n };\n });\n };\n\n batchKey =\n batchKey ||\n ((operation: Operation) => {\n const context = operation.getContext();\n\n const contextConfig = {\n http: context.http,\n options: context.fetchOptions,\n credentials: context.credentials,\n headers: context.headers,\n };\n\n //may throw error if config not serializable\n return selectURI(operation, uri) + JSON.stringify(contextConfig);\n });\n\n this.batcher = new BatchLink({\n batchDebounce: this.batchDebounce,\n batchInterval: this.batchInterval,\n batchMax: this.batchMax,\n batchKey,\n batchHandler,\n });\n }\n\n public request(operation: Operation): Observable<FetchResult> | null {\n return this.batcher.request(operation);\n }\n}\n"]}
\No newline at end of file