Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
refactor(createServerHttpLink)
Browse files Browse the repository at this point in the history
Use customized apollo-upload-client.
  • Loading branch information
yaacovCR committed Feb 23, 2020
1 parent 9962a29 commit 2d7482f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 351 deletions.
5 changes: 1 addition & 4 deletions package.json
Expand Up @@ -47,7 +47,7 @@
"homepage": "https://github.com/yaacovCR/graphql-tools-fork#readme",
"dependencies": {
"apollo-link": "^1.2.13",
"apollo-link-http-common": "^0.2.15",
"apollo-upload-client": "github:yaacovCR/apollo-upload-client",
"deprecated-decorator": "^0.1.6",
"extract-files": "^7.0.0",
"form-data": "^3.0.0",
Expand All @@ -59,11 +59,9 @@
"graphql": "^14.2.0 || ^15.0.0-rc"
},
"devDependencies": {
"@types/apollo-upload-client": "^8.1.3",
"@types/chai": "^4.2.9",
"@types/dateformat": "^3.0.1",
"@types/express": "^4.17.2",
"@types/extract-files": "^3.1.0",
"@types/graphql-type-json": "^0.3.2",
"@types/graphql-upload": "^8.0.3",
"@types/mocha": "^7.0.1",
Expand All @@ -72,7 +70,6 @@
"@types/uuid": "^3.4.7",
"@typescript-eslint/eslint-plugin": "^2.19.2",
"@typescript-eslint/parser": "^2.19.2",
"apollo-upload-client": "^12.1.0",
"babel-eslint": "^10.0.3",
"body-parser": "^1.19.0",
"chai": "^4.2.0",
Expand Down
57 changes: 57 additions & 0 deletions src/links/AwaitVariablesLink.ts
@@ -0,0 +1,57 @@
import {
ApolloLink,
Operation,
NextLink,
Observable,
FetchResult,
} from 'apollo-link';

function getFinalPromise(object: any): Promise<any> {
return Promise.resolve(object).then(resolvedObject => {
if (resolvedObject == null) {
return resolvedObject;
}

if (Array.isArray(resolvedObject)) {
return Promise.all(resolvedObject.map(o => getFinalPromise(o)));
} else if (typeof resolvedObject === 'object') {
const keys = Object.keys(resolvedObject);
return Promise.all(
keys.map(key => getFinalPromise(resolvedObject[key])),
).then(awaitedValues => {
for (let i = 0; i < keys.length; i++) {
resolvedObject[keys[i]] = awaitedValues[i];
}
return resolvedObject;
});
}

return resolvedObject;
});
}

class AwaitVariablesLink extends ApolloLink {
request(operation: Operation, forward: NextLink): Observable<FetchResult> {
return new Observable(observer => {
let subscription: any;
getFinalPromise(operation.variables)
.then(resolvedVariables => {
operation.variables = resolvedVariables;
subscription = forward(operation).subscribe({
next: observer.next.bind(observer),
error: observer.error.bind(observer),
complete: observer.complete.bind(observer),
});
})
.catch(observer.error.bind(observer));

return () => {
if (subscription != null) {
subscription.unsubscribe();
}
};
});
}
}

export { AwaitVariablesLink };

0 comments on commit 2d7482f

Please sign in to comment.