This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from ardatan/graphql-tools
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use customized apollo-upload-client.
- Loading branch information
Showing
6 changed files
with
88 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; |
Oops, something went wrong.