FuelLabs / fuels-ts

Fuel Network Typescript SDK
https://docs.fuel.network/docs/fuels-ts/
Apache License 2.0
44.04k stars 1.35k forks source link

Submit custom GraphQL queries from `Provider` #3095

Open danielbate opened 2 months ago

danielbate commented 2 months ago

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 commented 2 months ago

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.

petertonysmith94 commented 1 month ago

SDK Query Optimization Issue Documentation

Current Problem

Public Auto-Generated GraphQL Client

Breaking Changes

Ideal Solution

Make provider.operations Private

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

Proof of Concept Attempt

Complexity Levels

Lack of Existing Solutions

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 commented 1 month ago

Any thoughts on this @FuelLabs/frontend?

nedsalk commented 1 month ago

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