Skip to content

Add ability to chain rest links #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 61 additions & 16 deletions src/restLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,11 +1174,23 @@ const DEFAULT_JSON_SERIALIZER: RestLink.Serializer = (
};
};

const CONNECTION_REMOVE_CONFIG = {
test: (directive: DirectiveNode) => directive.name.value === 'rest',
remove: true,
};

const CONNECTION_REMOVE_CONFIG = (endpoints: Record<string, any>, removeRestLinksThatHaveEndpoints: boolean) => {
return {
test: (directive: DirectiveNode) => {
/** Composability Modification
* if we can't find an endpoint associated with this restlink, then assume the next restlink will define it
* and don't remove the current restlink directive
*/
const foundEndpoint = directive.arguments.find(val => {
if (val.name.value === 'endpoint' && Object.keys(endpoints).includes(val.value.value)) {
return val;
}
});
return directive.name.value === 'rest' && (removeRestLinksThatHaveEndpoints ? foundEndpoint : !foundEndpoint);
},
remove: true,
};
}
/**
* RestLink is an apollo-link for communicating with REST services using GraphQL on the client-side
*/
Expand Down Expand Up @@ -1295,34 +1307,51 @@ export class RestLink extends ApolloLink {
this.processedDocuments = new Map();
}

private removeRestSetsFromDocument(query: DocumentNode): DocumentNode {
const cached = this.processedDocuments.get(query);
private removeRestSetsFromDocument(query: DocumentNode, removeRestLinksThatHaveEndpoints: boolean): DocumentNode {
/** Composability Modification:
* the original code removed all of the rest links - when we added the ability to only remove some of
* the rest links, the cache key can no longer be just "query" because the response can be different for
* the same query (depending on if we remove rest links that have defined endpoints)
*/
const cacheKey = query + removeRestLinksThatHaveEndpoints;
const cached = this.processedDocuments.get(cacheKey);
if (cached) return cached;

checkDocument(query);

const docClone = removeDirectivesFromDocument(
[CONNECTION_REMOVE_CONFIG],
[CONNECTION_REMOVE_CONFIG(this.endpoints, removeRestLinksThatHaveEndpoints)],
query,
);

this.processedDocuments.set(query, docClone);
this.processedDocuments.set(cacheKey, docClone);
return docClone;
}

public request(
operation: Operation,
forward?: NextLink,
): Observable<FetchResult> | null {
const { query, variables, getContext, setContext } = operation;
const { variables, getContext } = operation;
let query = operation.query;
const context: LinkChainContext | any = getContext() as any;
const isRestQuery = hasDirectives(['rest'], query);
if (!isRestQuery) {
return forward(operation);
}

const nonRest = this.removeRestSetsFromDocument(query);


/** Composability Modification:
* nonRest is the query forwarded to the next link. We want to keep the restLinks
* in that query that don't have endpoints defined because we're going to assume
* that the next link in the chain will handle thos
*/
const nonRest = this.removeRestSetsFromDocument(query, true);
/** Composability Modification:
* we want to remove the rest links that don't have an endpoint defined
* because otherwise, they'll be requested with an "undefined" in the url
*/
query = this.removeRestSetsFromDocument(query, false);

// 1. Use the user's merge policy if any
let headersMergePolicy: RestLink.HeadersMergePolicy =
context.headersMergePolicy;
Expand Down Expand Up @@ -1393,12 +1422,28 @@ export class RestLink extends ApolloLink {
resolverOptions,
)
.then(data => {
setContext({
restResponses: (context.restResponses || []).concat(
/** Composability Modification:
* not sure why the code is caching setContext but we want to make sure
* we're setting context the the right point in the chain and using a cached
* setContext can break this
*/
operation.setContext({
/** Composability Modification:
* get the context fresh since it could have changed between when it was cached above
* and when this code is called
* we use this data field to preserve the data from previous rest link requests
* aside: what is restResponses used for? I can't find refs to it
*/
data: Object.assign((operation.getContext().data || {}), data),
restResponses: (operation.getContext().restResponses || []).concat(
requestContext.responses,
),
});
observer.next({ data, errors });
/** Composability Modification:
* the next field merge operations on the query need all of the data in order to fill in the fields
* previously there was only one data response so this didn't matter
*/
observer.next({ data: operation.getContext().data, errors });
observer.complete();
})
.catch(err => {
Expand Down