apollo-angular-link-http
Version:
An Apollo Link to allow sending a single http request per operation.
80 lines • 10.6 kB
JavaScript
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ApolloLink, Observable as LinkObservable, } from 'apollo-link';
import { print } from 'graphql';
import { fetch, mergeHeaders, prioritize, } from 'apollo-angular-link-http-common';
import { extractFiles } from 'extract-files';
// XXX find a better name for it
export class HttpLinkHandler extends ApolloLink {
constructor(httpClient, options) {
super();
this.httpClient = httpClient;
this.options = options;
this.requester = (operation) => new LinkObservable((observer) => {
const context = operation.getContext();
// decides which value to pick, Context, Options or to just use the default
const pick = (key, init) => {
return prioritize(context[key], this.options[key], init);
};
const includeQuery = pick('includeQuery', true);
const includeExtensions = pick('includeExtensions', false);
const method = pick('method', 'POST');
const url = pick('uri', 'graphql');
const withCredentials = pick('withCredentials');
const useMultipart = pick('useMultipart');
const req = {
method,
url: typeof url === 'function' ? url(operation) : url,
body: {
operationName: operation.operationName,
variables: operation.variables,
},
options: {
withCredentials,
useMultipart,
headers: this.options.headers,
},
};
if (includeExtensions) {
req.body.extensions = operation.extensions;
}
if (includeQuery) {
req.body.query = print(operation.query);
}
if (context.headers) {
req.options.headers = mergeHeaders(req.options.headers, context.headers);
}
const sub = fetch(req, this.httpClient, extractFiles).subscribe({
next: (response) => {
operation.setContext({ response });
observer.next(response.body);
},
error: (err) => observer.error(err),
complete: () => observer.complete(),
});
return () => {
if (!sub.closed) {
sub.unsubscribe();
}
};
});
}
request(op) {
return this.requester(op);
}
}
export class HttpLink {
constructor(httpClient) {
this.httpClient = httpClient;
}
create(options) {
return new HttpLinkHandler(this.httpClient, options);
}
}
HttpLink.decorators = [
{ type: Injectable }
];
HttpLink.ctorParameters = () => [
{ type: HttpClient }
];
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaHR0cC1saW5rLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2h0dHAtbGluay50cy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GhDcmVkZW50aWFscyA9IHBpY2soJ3dpdGhDcmVkZW50aWFscycpO1xuICAgICAgICBjb25zdCB1c2VNdWx0aXBhcnQgPSBwaWNrKCd1c2VNdWx0aXBhcnQnKTtcblxuICAgICAgICBjb25zdCByZXE6IFJlcXVlc3QgPSB7XG4gICAgICAgICAgbWV0aG9kLFxuICAgICAgICAgIHVybDogdHlwZW9mIHVybCA9PT0gJ2Z1bmN0aW9uJyA/IHVybChvcGVyYXRpb24pIDogdXJsLFxuICAgICAgICAgIGJvZHk6IHtcbiAgICAgICAgICAgIG9wZXJhdGlvbk5hbWU6IG9wZXJhdGlvbi5vcGVyYXRpb25OYW1lLFxuICAgICAgICAgICAgdmFyaWFibGVzOiBvcGVyYXRpb24udmFyaWFibGVzLFxuICAgICAgICAgIH0sXG4gICAgICAgICAgb3B0aW9uczoge1xuICAgICAgICAgICAgd2l0aENyZWRlbnRpYWxzLFxuICAgICAgICAgICAgdXNlTXVsdGlwYXJ0LFxuICAgICAgICAgICAgaGVhZGVyczogdGhpcy5vcHRpb25zLmhlYWRlcnMsXG4gICAgICAgICAgfSxcbiAgICAgICAgfTtcblxuICAgICAgICBpZiAoaW5jbHVkZUV4dGVuc2lvbnMpIHtcbiAgICAgICAgICAocmVxLmJvZHkgYXMgQm9keSkuZXh0ZW5zaW9ucyA9IG9wZXJhdGlvbi5leHRlbnNpb25zO1xuICAgICAgICB9XG5cbiAgICAgICAgaWYgKGluY2x1ZGVRdWVyeSkge1xuICAgICAgICAgIChyZXEuYm9keSBhcyBCb2R5KS5xdWVyeSA9IHByaW50KG9wZXJhdGlvbi5xdWVyeSk7XG4gICAgICAgIH1cblxuICAgICAgICBpZiAoY29udGV4dC5oZWFkZXJzKSB7XG4gICAgICAgICAgcmVxLm9wdGlvbnMuaGVhZGVycyA9IG1lcmdlSGVhZGVycyhcbiAgICAgICAgICAgIHJlcS5vcHRpb25zLmhlYWRlcnMsXG4gICAgICAgICAgICBjb250ZXh0LmhlYWRlcnMsXG4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