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

"Other Pools" do not load when a wallet is connected #1

Open
Mycetism opened this issue Oct 2, 2023 · 3 comments · May be fixed by #3
Open

"Other Pools" do not load when a wallet is connected #1

Mycetism opened this issue Oct 2, 2023 · 3 comments · May be fixed by #3
Labels
bug Something isn't working

Comments

@Mycetism
Copy link

Mycetism commented Oct 2, 2023

The page for /pools contains a list of "Other Pools", which loads as long as a wallet is not connected. However as soon as this condition changes, the list refuses to load, suggesting a malformed hive-graph request or similar.

@Mycetism
Copy link
Author

Mycetism commented Oct 2, 2023

I am able to query hive-graph directly through the GraphQL Playground and get the expected results, confirming that it is not a contract issue.

@Mycetism
Copy link
Author

Mycetism commented Oct 3, 2023

Apparently the requestInChunks function does not account for multiple wasm calls per item.

An easy and naive fix, which assumes queryBuilder does not produce a query with a varying number of wasm calls for the provided items, would be the following:

export const requestInChunks = async <Item = any, Response = any>(
  chunkSize: number,
  url: string,
  items: Item[],
  queryBuilder: (chunk: Item[]) => string
): Promise<Response> => {
  const firstQuery = queryBuilder(items.slice(0, 1));

  const wasmCallsPerQuery = ((firstQuery).match(/wasm {/g) || []).length;
  const correctedChunkSize = Math.floor(chunkSize / (wasmCallsPerQuery || 1));
  const totalChunks = Math.ceil(items.length / correctedChunkSize);

  const chunks = await Promise.all(
    Array.from(Array(totalChunks).keys()).map((i) => {
      const chunk = items.slice(i * correctedChunkSize, (i + 1) * correctedChunkSize);

      console.log(queryBuilder(chunk));

      return request<Response>(url, queryBuilder(chunk));
    })
  );

  return chunks.reduce((all, chunk) => ({ ...all, ...chunk }));
};

Alternatively we could use graphql together with lodash (both already installed) to parse the query and split it into chunks, but this might have an impact on performance:

export const requestInChunks = async <Item = any, Response = any>(
  chunkSize: number,
  url: string,
  items: Item[],
  queryBuilder: (chunk: Item[]) => string
): Promise<Response> => {
  const query = queryBuilder(items);
  const { kind, loc, selections } = (parse(query).definitions[0] as OperationDefinitionNode).selectionSet;
  
  const selectionChunks = _.chunk(selections, chunkSize);
  const chunks = selectionChunks.map((chunk) => print({ kind, loc: loc!, selections: chunk ?? [] }));

  const requests = await Promise.all(chunks.map((chunk) => request<Response>(url, chunk)));

  return requests.reduce((all, chunk) => ({ ...all, ...chunk }));
};

@OhhBilboBaggins
Copy link

Alternatively we could use graphql together with lodash (both already installed) to parse the query and split it into chunks, but this might have an impact on performance:

export const requestInChunks = async <Item = any, Response = any>(
  chunkSize: number,
  url: string,
  items: Item[],
  queryBuilder: (chunk: Item[]) => string
): Promise<Response> => {
  const query = queryBuilder(items);
  const { kind, loc, selections } = (parse(query).definitions[0] as OperationDefinitionNode).selectionSet;
  
  const selectionChunks = _.chunk(selections, chunkSize);
  const chunks = selectionChunks.map((chunk) => print({ kind, loc: loc!, selections: chunk ?? [] }));

  const requests = await Promise.all(chunks.map((chunk) => request<Response>(url, chunk)));

  return requests.reduce((all, chunk) => ({ ...all, ...chunk }));
};

This seems like the better / more scalable approach to me

@Mycetism Mycetism added the bug Something isn't working label Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants