dotansimha / dotansimha/graphql-code-generator-community
Pass operation type to the requester in the `typescript-generic-sdk` plugin
- Dominant language
- TypeScript
- Stars
- 137
- Forks
- 195
- Avg merge
- 6h 20m
- Merged PRs (30d)
- 16
Description
I'm using the `typescript-generic-sdk` plugin and my custom requester implementation includes a simple caching mechanism. I'm also setting the `documentMode` option to `string` because I don't need the `graphql-tag` package or whatever to be imported to the client (I'm building a web app so bundle size is important, you know).
Now, nearly everything is working flawlessly. That being said, I don't want caching for my mutations, only for the queries. But only three arguments are passed to the requester: `doc`, `vars`, and `options`. In my requester, I can't really tell by looking at these arguments whether the operation is a query or a mutation, I need a way to be able to do that.
A new option for the `typescript-generic-sdk` plugin like `passOperationType` would solve this problem, which when set to `true`, the SDK functions will also pass the the name of the operation they represent to the requester as an additional argument (its type would be `'query' | 'mutation' | 'subscription'`):
```diff
export function getSdk(requester: Requester) {
return {
addCartItem(variables: AddCartItemMutationVariables, options?: C): Promise {
- return requester(AddCartItemDocument, variables, options);
+ return requester(AddCartItemDocument, variables, options, 'mutation');
},
getCartInfo(variables?: GetCartQueryVariables, options?: C): Promise {
- return requester(GetCartDocument, variables, options);
+ return requester(GetCartDocument, variables, options, 'query');
},
};
}
```
This way, the requester could very simply figure out which type of operation is being sent, and act accordingly:
```ts
getSdk((doc, vars, options, operationType) => {
if (operationType == 'query') {
// Use cache
}
});
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.