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

Generating flowtypes with a graphql input puts generated type in every generated file #988

Open
bdrobinson opened this issue Feb 7, 2019 · 5 comments
Labels
🤖 component - codegen related to the codegen core packages

Comments

@bdrobinson
Copy link

Intended outcome:

I'm trying to generate flow types for a mutation. I have the following mutation:

input PlaceOrderMutationInput {
  # ... some fields
}
mutation {
  placeOrder(input: PlaceOrderMutationInput!)
}

When I define a graphql mutation client-side like this:

const PLACE_ORDER_MUTATION = gql`
  mutation PlaceOrder($input: PlaceOrderMutationInput!) {
    placeOrder(input: $input) {
      # some fields, not relevant
    }
  }
`

I expect that types for PlaceOrderMutationInput will only be generated in the PlaceOrder.jsfile.

Actual outcome:
Every single other file generated by apollo codegen in my project also contains the PlaceOrderMutationInput type. This seems incorrect, and is annoying as it adds a huge amount of noise to git diffs etc.

How to reproduce the issue:

The command I'm running is:

apollo client:codegen --addTypename --includes='src/**/*.js' --target=flow --localSchemaFile='src/js/gql/schema.json' types

Versions
apollo@2.4.4

Thanks very much!

@bdrobinson
Copy link
Author

I've had a look at the source code, and in particular the function generateSource (roughly here).

It looks to me as though inputs and enums are treated as special cases, and are all always injected into every single generated file (as part of the common variable). The way the code is written makes me think this is an intentional choice rather than a bug. Is there a reason for this? Wouldn't it make more sense to only inject enums/inputs into files that actually use them?

Cheers!

@bdrobinson
Copy link
Author

Any ideas on this? I'd be happy to look into it and potentially do a PR but would want to be sure it wasn't intentional behaviour first.

@JakeDawkins JakeDawkins added the 🤖 component - codegen related to the codegen core packages label Apr 17, 2019
@kevinbarabash
Copy link

kevinbarabash commented Jul 29, 2019

I ran into this as well. I'd rather have a single file containing all of the enums and data types and have the query/mutation type files import whatever they need from that central schema type file.

@fravic
Copy link

fravic commented Nov 13, 2019

It looks like typescript codegen already has this functionality (it generates a globalTypes.ts, optionally specified by the --globalTypesFile option). It would be great to have this functionality in flow as well.

Perhaps if --target=flow --globalTypesFile=... is specified, we write common to the specified path instead of appending it to every file? Happy to open a PR that does this.

@IanVS
Copy link

IanVS commented Jan 22, 2020

Shoot, I was hoping that would work with flow, since the docs don't specify that it's typescript only. I'm migrating from apollo-codegen to this new apollo cli method, and by default each of the over 200 files being created have nearly 1,000 lines of duplicated types. Can that be right? Is this the intended behavior, or is there a way to avoid it?

Edit: oof, I see this has been attempted at least twice. #1224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖 component - codegen related to the codegen core packages
Projects
None yet
Development

No branches or pull requests

5 participants