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

Import queries instead of duplicating them on the generated output #6605

Open
danielo515 opened this issue Sep 2, 2021 · 10 comments
Open

Import queries instead of duplicating them on the generated output #6605

danielo515 opened this issue Sep 2, 2021 · 10 comments
Labels
core Related to codegen core/cli kind/question Improvements or additions to documentation waiting-for-answer Waiting for answer from author

Comments

@danielo515
Copy link
Contributor

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

It is a problem we have with the way the output is generated, which leads to a very big file and failing static dependency analysis.

When using "typescript-operations" and "typescript-react-apollo" the generated output (which should include hooks and HOCs) the queries that are being used are duplicated on the generated file. This mean that the original file where the query is defined is detected as unused by our dependency analysis tools, because they are in fact unused. This is a problem because we used to have some big amounts of unused code and we don't want to get back to that situation. Also the generated output is huge, it is already 15k lines, which can be drastically reduced if the hooks were importing the queries rather than inlining them.

Describe the solution you'd like
I want an option where the queries are imported from their original file rather than being copy pasted to the generated output code.

Describe alternatives you've considered
I don't have any alternative

@n1ru4l
Copy link
Collaborator

n1ru4l commented Sep 3, 2021

Why not simply write your operations into .graphql files? Then you could treat those as operation definition files and your dead code elimination file could skip those but instead complain on the generated output, which will force you to remove definitions in the .graphql file (or delete the file completely).

Importing from the original file could potentially lead to cyclic dependencies.

If what I said before totally does not make sense it would be best if you could provide an example code sandbox that showcases how you use graphql-codegen so I can get a better understanding.

Furthermore, you should take a look at the TypedDocumentNode plugin, which can drastically reduce the generated output: https://www.graphql-code-generator.com/docs/plugins/typed-document-node

@n1ru4l n1ru4l added waiting-for-answer Waiting for answer from author kind/question Improvements or additions to documentation labels Sep 3, 2021
@danielo515
Copy link
Contributor Author

danielo515 commented Sep 27, 2021 via email

@n1ru4l
Copy link
Collaborator

n1ru4l commented Sep 27, 2021

Have you considered writing your GraphQL Operations within the Code, that way when you remove the usage of a operation the generated code will be removed as well? This is what the relay flow pursues and is now also possible with graphql-codegen via https://the-guild.dev/graphql/codegen/plugins/presets/preset-client

@danielo515
Copy link
Contributor Author

danielo515 commented Oct 4, 2021 via email

@n1ru4l
Copy link
Collaborator

n1ru4l commented Oct 6, 2021

@danielo515 Where in this example is an unused import?

import { gql } from @app/gql’;
import { useQuery } from ‘urql’;
// TweetsQuery is a fully typed document node/
const TweetsQuery = gql(/* GraphQL */ `
  query TweetsQuery {
    Tweets {
      id
      body
    }
  }
`);
const Tweets = () => {
  const [result] = useQuery({ query: TweetsQuery });
  const { data, fetching, error } = result;
  if (fetching) return <p>Loading...</p>;
  if (error) return <p>Oh no... {error.message}</p>;
  return (
    <ul>
      {/* data is fully typed :tada: */}
      {data.Tweets.map(tweet => (
        <li key={tweet.id}>{tweet.body}</li>
      ))}
    </ul>
  );
};

We are also working on a module augmentation mode that we will release soon. That way only type definitions are generated that overload the gql tag function.

You can have a look at the docs here: https://github.com/dotansimha/graphql-code-generator/pull/6492/files#diff-1a39726074b13ad1586a426c290e1c2213e3d691224eff1de2e3ddc92748ebc0 and already test the preset with those features by using the following packages: #6492 (comment)

By following this pattern all your GraphQL Operations are co-located with the actual code/components. Deleting a component file that includes a GraphQL Operation will automatically result in the generated code for it being deleted (when running codegen).

@danielo515
Copy link
Contributor Author

danielo515 commented Oct 9, 2021 via email

@charlypoly charlypoly added the core Related to codegen core/cli label Nov 3, 2022
@macrozone
Copy link

Have you considered writing your GraphQL Operations within the Code, that way when you remove the usage of a operation the generated code will be removed as well? This is what the relay flow pursues and is now also possible with graphql-codegen via https://www.graphql-code-generator.com/docs/presets/gql-tag-operations

this link no longer works, has this been removed?

@n1ru4l
Copy link
Collaborator

n1ru4l commented Mar 31, 2023

@macrozone
Copy link

@macrozone https://the-guild.dev/graphql/codegen/plugins/presets/preset-client

thx, i also see this is recommended in the guide. I noticed that many devs get confused on which style to use, so I raised an issue #9251

@eltonio450
Copy link

reopening the discussion here:

we have the same issue:

  • queries/mutations live in ts/tsx files in the codebase. Sometimes indeed close usage, sometimes not
    Ex:
// ClientInsuranceContainer.tsx
const updateClientInsuranceSpecificitiesMutation = gql`
  mutation updateClientInsuranceSpecificities(
    $input: UpdateClientInsuranceSpecificitiesInput!
  ) {
    updateClientInsuranceSpecificities(input: $input) {
      id
      insuranceSpecificities {
        cargoValuePercentInsured
        coverage
      }
    }
  }
`;
  • we do generated a huge file api.generated.ts with hooks (queries, suspense queries, etc.)

We use

  • typescript plugin
  • typescript-operations

We want to have static analysis on what is used or not (we use knip)
If we rely on the unused files, then we will have many false positives (ex: my mutation from dashboard.mutation.tsx that has been copied to api.generated.ts)
If we rely on unused exports (ex: in api.generated.ts), we will have many false positives (unused hooks: normal query if we use the suspense query, etc.)

near-operation-file only works with .graphql file

not sure what's best from here

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to codegen core/cli kind/question Improvements or additions to documentation waiting-for-answer Waiting for answer from author
Projects
None yet
Development

No branches or pull requests

5 participants