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

A feature to change operation #300

Closed
kazekyo opened this issue Jun 15, 2021 · 1 comment
Closed

A feature to change operation #300

kazekyo opened this issue Jun 15, 2021 · 1 comment
Labels
core Feature requests related to core functionality

Comments

@kazekyo
Copy link

kazekyo commented Jun 15, 2021

Summary

From: apollographql/apollo-client#8384

I would like to request a new feature to change operation when executing Query/Mutation/Subscription.

It satisfies the following:

  1. It can change operation.query. It can add and remove arguments and fields.
  2. it can change operation.variables.
  3. it can change operation.context(using operation.setContext()/operation.getContext()). This is because there are cases where I want to change the operation.query using the context.
  4. It can update the cache based on the changed operation.

Background

I'm working on a library to make Apollo Client more useful with Relay Specification.
The library includes an improved relayStylePagination, many directives such as @arguments and @argumentDefinitions, and hooks such as usePaginationFragment.

The library can infer more information than what the user wrote in a query because the Relay Specification has some conditions in the schema.
So, it is theoretically possible for the library to automatically added fields to the user's query.
For example, there is always the field called pageInfo in Connection, so if the user has not written this field, it can be added automatically.

Issue

I have implemented several features such as @arguments and @argumentDefinitions by changing operation.query and operation.variables in Link.

However, this is not the recommended way and there is an issue.
The issue is that fields and arguments that were not present in the initial query cannot be found in the cache.
apollographql/apollo-client#8384

So I can't implement a case where the library adds pageInfo and uses its value.

Ideas

The first idea is to make it possible to change the query and variables in Link.

Another idea is to create a new feature that makes it possible to change operation.
For example, introduce operationModifier like the following:

const modifier = (operation: Operation): Operation => {
  return transform(operation);
};

const client = new ApolloClient({
  operationModifier: [modifier],
});

If operationModifier is not empty, the client will execute them before requesting the API. The cache will be updated based on the changed operation.


I'm very interested in this feature. If you are busy, I can try to implement it if we can agree on some design.
The reason I like Apollo Client is its extensibility, so I hope this request will create more possibilities for Apollo Client.

@jpvajda jpvajda added the project-apollo-client (legacy) LEGACY TAG DO NOT USE label May 25, 2022
@jerelmiller jerelmiller added the core Feature requests related to core functionality label Apr 6, 2023
@jerelmiller jerelmiller removed the project-apollo-client (legacy) LEGACY TAG DO NOT USE label Jan 22, 2024
@jerelmiller
Copy link
Member

Hey @kazekyo 👋

This should now be possible with document transforms which was released in 3.8.0. As such, we are going to close this as completed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Feature requests related to core functionality
Projects
None yet
Development

No branches or pull requests

3 participants