question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[typescript-react-query] Does not use own types when combined with import-types and typescript-operations

See original GitHub issue

Describe the bug I would like to separate out my schema-types from operation-types+hooks, and I believe I’ve found the config which should accomplish it. However, when using import-types-preset, all of the types are referenced from the Types import, even if they’re being exported from the same file.

To Reproduce Steps to reproduce the behavior:

https://codesandbox.io/s/graphql-code-generator-6909-lu5rm?file=/hooks.ts

Note typescript errors for Types.UserQuery, since UserQuery is defined in the same file, not ./types.

  1. My GraphQL schema:

See reproduction

  1. My GraphQL operations:

See reproduction

  1. My codegen.yml config file:
generates:
  ./types.ts:
    plugins:
      - typescript
  ./hooks.ts:
    preset: import-types
    presetConfig:
      typesPath: ./types
    plugins:
      - typescript-operations
      - typescript-react-query

Expected behavior I expect that types being exported from hooks.ts are not attempted to be imported from types.ts.

Environment:

  • OS: mac
  • @graphql-codegen/...: latest, see reproduction
  • NodeJS: 16

Additional context A similar issue which was recently fixed: https://github.com/dotansimha/graphql-code-generator/issues/6520

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
StampixSMOcommented, Jan 17, 2022

Sure, I added this file:

fix-types.js

const fs = require('fs');
const path = require('path');

const run = async () => {
  const file = path.join(__dirname, 'RELATIVE_PATH_TO_YOUR_GENERATED_FILE');
  let generatedContent = (await fs.promises.readFile(file)).toString();
  const reactQueryTypeMatches = Array.from(generatedContent.matchAll(/type (.+) =/g));
  for (const match of reactQueryTypeMatches) {
    const type = match[1];
    console.log(type);
    generatedContent = generatedContent.replace(new RegExp(`Types.${type}`, 'g'), type);
  }
  await fs.promises.writeFile(file, generatedContent);
};

run().catch((err) => {
  console.error(err);
  process.exit(1);
});

Then I run a hook in my codegen.yml file

hooks:
      afterOneFileWrite:
        - yarn graphql:codegen:fix

Then I added a graphql:codegen:fix script in my package.json that runs fix-types.js

1reaction
StampixSMOcommented, Jan 14, 2022

If anyone’s interested, I have temporarily solved my issue in a hacky way. I run an automated post-codegen script that replaces Types.X with X if X’s type is present in the file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript-react-query - GraphQL Code Generator
This codegen plugin just burns the generated TypeScript types into the operation, and provides flexibility to choose your fetcher . Using ...
Read more >
[Plugin Request] Add typescript-react-query plugin #4792
Currently there are no alternatives. You can use the types generated by typescript and typescript-operations plugins, but they will only ...
Read more >
React Query and TypeScript - TkDodo's blog
React Query heavily uses Generics. This is necessary because the library does not actually fetch data for you, and it cannot know what...
Read more >
Simplify GraphQL requests with React Query, GraphQL Code ...
Take advantage of GraphQL on the frontend by automating the creation of TypeScript types and the generation of custom React Query hooks.
Read more >
Type error with react-query and UseQueryResult
your queryFn returns any because fetch is untyped, so even though you are using TypeScript, you won't get any typesafety that way. Share....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found