dotansimha / graphql-code-generator-community

MIT License
113 stars 143 forks source link

`useBackgroundQuery` and `useReadQuery` support (typescript-react-apollo) #477

Open fforres opened 10 months ago

fforres commented 10 months ago

Is your feature request related to a problem? Please describe.

Add support for useBackgroundQuery and useReadQuery released in apollo-client 3.8

Describe the solution you'd like

Allow typescript-react-apollo to generate useBackgroundQuery and useReadQuery hooks with the current withHooks: true option

Describe alternatives you've considered

Additional context

fforres commented 10 months ago

Happy to work on the PR for this, though it might take me a bit 🙏

fforres commented 10 months ago

Will keep the WIP here -> https://github.com/dotansimha/graphql-code-generator-community/compare/main...fforres:graphql-code-generator-community:fforres/use_background_query?expand=1

saihaj commented 10 months ago

great let me know once you are ready for review @fforres assigned you this issue. Thank you

nirus commented 10 months ago

@fforres

Will keep the WIP here -> https://github.com/dotansimha/graphql-code-generator-community/compare/main...fforres:graphql-code-generator-community:fforres/use_background_query?expand=1

I corrected your generator code and it works.

      // BackgroundQuery Hook
      const backgroundOperationName: string =
        this.convertName(nodeName, {
          suffix: pascalCase('BackgroundQuery'),
          useTypesPrefix: false,
        }) + this.config.hooksSuffix;

      hookFns.push(
        `export function use${backgroundOperationName}(baseOptions?: ${this.getApolloReactHooksIdentifier()}.BackgroundQueryHookOptions<${operationResultType}, ${operationVariablesTypes}>) {
             const options = {...defaultOptions, ...baseOptions}
             return ${this.getApolloReactHooksIdentifier()}.useBackgroundQuery<${operationResultType}, ${operationVariablesTypes}>(${this.getDocumentNodeVariable(
          node,
          documentVariableName,
        )}, options);
           }`,
      );
      hookResults.push(
        `export type ${backgroundOperationName}HookResult = ReturnType<typeof use${backgroundOperationName}>;`,
      );

      // ReadQuery Hook
      const readOperationName: string =
        this.convertName(nodeName, {
          suffix: pascalCase('ReadQuery'),
          useTypesPrefix: false,
        }) + this.config.hooksSuffix;

      hookFns.push(
        `export function use${readOperationName}(backgroundQueryRef: ${this.getApolloReactHooksIdentifier()}.QueryReference<${operationResultType}>) {
          return ${this.getApolloReactHooksIdentifier()}.useReadQuery<${operationResultType}>(backgroundQueryRef);
        }`,
      );

      hookResults.push(
        `export type ${readOperationName}HookResult = ReturnType<typeof use${readOperationName}>;`,
      );
draftcode commented 1 month ago

Since this issue has been stale, I've created a PR for this.

https://github.com/dotansimha/graphql-code-generator-community/pull/815

@saihaj Do you mind reviewing the PR?