Skip to content
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

Migrate apollo-link-persisted-queries functionality into Apollo Client #6837

Merged
merged 15 commits into from
Sep 11, 2020
Merged
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
Prev Previous commit
Next Next commit
Allow GenerateHashFunction to return string | PromiseLike<string>.
  • Loading branch information
benjamn committed Sep 11, 2020
commit ce4d70251de9c9a36ef0b4e10300390ff18f94db
12 changes: 8 additions & 4 deletions src/link/persisted-queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export interface ErrorResponse {
operation: Operation;
}

type SHA256Function = (...args: any[]) => string | Promise<string>;
type GenerateHashFunction = (document: DocumentNode) => Promise<string>;
type SHA256Function = (...args: any[]) => string | PromiseLike<string>;
type GenerateHashFunction = (document: DocumentNode) => string | PromiseLike<string>;

namespace PersistedQueryLink {
interface BaseOptions {
Expand Down Expand Up @@ -119,12 +119,16 @@ export const createPersistedQueryLink = (
let supportsPersistedQueries = true;

const hashesChildKey = 'forLink' + nextHashesChildKey++;

const getHashPromise = (query: DocumentNode) =>
new Promise<string>(resolve => resolve(generateHash(query)));

function getQueryHash(query: DocumentNode): Promise<string> {
if (!query || typeof query !== 'object') {
// If the query is not an object, we won't be able to store its hash as
// a property of query[hashesKey], so we let generateHash(query) decide
// what to do with the bogus query.
return generateHash(query);
return getHashPromise(query);
}
if (!hasOwnProperty.call(query, hashesKey)) {
Object.defineProperty(query, hashesKey, {
Expand All @@ -135,7 +139,7 @@ export const createPersistedQueryLink = (
const hashes = (query as any)[hashesKey];
return hasOwnProperty.call(hashes, hashesChildKey)
? hashes[hashesChildKey]
: (hashes[hashesChildKey] = generateHash(query));
: hashes[hashesChildKey] = getHashPromise(query);
}

return new ApolloLink((operation, forward) => {
Expand Down