apollo-feature-requests icon indicating copy to clipboard operation
apollo-feature-requests copied to clipboard

A feature to change operation

Open kazekyo opened this issue 4 years ago • 0 comments

Summary

From: https://github.com/apollographql/apollo-client/issues/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. https://github.com/apollographql/apollo-client/issues/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.

kazekyo avatar Jun 15 '21 07:06 kazekyo