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

Submit custom GraphQL queries from Provider #3095

Open
danielbate opened this issue Sep 3, 2024 · 4 comments
Open

Submit custom GraphQL queries from Provider #3095

danielbate opened this issue Sep 3, 2024 · 4 comments
Labels
feat Issue is a feature

Comments

@danielbate
Copy link
Contributor

danielbate commented Sep 3, 2024

Currently we interact with GraphQL endpoints supplied by the node via wrapped operations provided by graphql-codegen, these are then exposed to SDK consumers via wrapped provider functions (Provider.getVersion(), Provider.getBlockNumber() etc). This potentially restricts the information we are providing to consumers or means they have to go around the SDK to query the node. We should provide a way for consumers to submit their own custom queries.

Potential implmentation in Provider

/**
 * Passes a custom document and variables via the provider
 */
async customQuery(query: DocumentNode, variables: Record<string, unknown>): Promise<unknown> {
  return this.operations.query(query, variables);
}
@danielbate danielbate added the feat Issue is a feature label Sep 3, 2024
@danielbate
Copy link
Contributor Author

In #3047 I reused a lot of what we already have in the SDK to create a custom query, we could do something similar but more flexibly. Have a public function on the provider that takes a document and variables and passes the whole thing to requester, similarly to how we already build our operations.

@danielbate danielbate changed the title Submit custom GraphQL queries via the Provider Submit custom GraphQL queries from Provider Sep 3, 2024
@arboleya arboleya added temp-p2 and removed temp-M labels Sep 7, 2024
@arboleya arboleya added the temp:notion label Sep 8, 2024 — with Linear
@arboleya arboleya added the temp-linear label Sep 8, 2024 — with Linear
@petertonysmith94
Copy link
Contributor

SDK Query Optimization Issue Documentation

Current Problem

Public Auto-Generated GraphQL Client

  • The SDK's auto-generated GraphQL client, provider.operations, is currently a public property.
  • This client serves two purposes:
    1. Internal needs of the SDK
    2. Usage by frontend applications

Breaking Changes

  • Removing any queried field is considered a breaking change.
  • Reason: External consumers might be directly using the auto-generated client and relying on specific fields.

Ideal Solution

Make provider.operations Private

  • This change would allow for non-breaking optimizations within the SDK.

Alternative Options for Consumers

Two proposed alternatives for SDK consumers:

  1. Auto-generate Their Own Clients

    • Consumers would create clients tailored to their specific querying needs.
    • This approach mimics the SDK's current method.
  2. Implement Ad-hoc Type-safe Queries

    • Allow consumers to write custom queries through the TypeScript SDK.
    • Example usage:
      provider.client.query('chain', { 
        name: true, 
        consensusParameters: { 
          chainId: true 
        } 
      });

Challenges with Implementation

Previous Discussions

  • The second approach (ad-hoc type-safe queries) has been discussed internally multiple times.

Proof of Concept Attempt

  • A proof of concept was implemented for the ad-hoc query approach.
  • Findings:
    • Implementation might be possible but would be a significant undertaking.
    • Main challenge: Ensuring correct API functionality and type inference for all GraphQL query types.

Complexity Levels

  • Implementing type safety for nested queries is relatively achievable.
  • Supporting more complex GraphQL features (e.g., union types) is significantly more challenging.

Lack of Existing Solutions

  • No suitable library was found that could automatically handle all required functionalities.

Conclusion

The current structure of the SDK's GraphQL client presents a barrier to query optimization. While potential solutions have been identified, implementation remains complex and resource-intensive. Further research and development may be necessary to find a balance between maintaining backwards compatibility and enabling more efficient query structures.

@petertonysmith94
Copy link
Contributor

Any thoughts on this @FuelLabs/frontend?

@nedsalk
Copy link
Contributor

nedsalk commented Oct 11, 2024

graphql-zeus looks really promising and might be the library that would enable both this and #1570 in one go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

No branches or pull requests

4 participants