-
Notifications
You must be signed in to change notification settings - Fork 469
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
Why does codegen:generate require query names to be unique? #670
Comments
It generates type names based on the query name, so I believe you would have type name collisions when |
@lukepighetti When you're colocating queries to components it makes no sense. I want to have types, but I don't want to think of globally unique names for each query. |
I just ran into this too. Yeah, if I'm colocating the generated types near the component, I would like to opt out of the de-duplication of query names. it took me a while to figure this out, as there was no output telling me that duplicates are ignored and only one component would get a type written. (apollo@2.4.4) |
Thanks for the thoughts, everyone! I've definitely thought about this a good bit. As mentioned above, currently typenames are dependent on the operation name. While unique operation naming may not be a requirement in any way for GraphQL, I'd argue that it's also a best practice. Naming operations has many more purposes than just a display name in code. Naming them uniquely makes them easier to find in a codebase, in debugging tools, etc. Not to mention the possibility of importing the wrong types when they're not unique. In addition to that, a lot of our current/future features rely on operation name for things like operation registries, metrics tracking, etc. Right now, for those reasons, we don't have any plans to build in support for non-unique operation names, especially since the workaround is so simple. I'm open to the idea of additional warnings and errors though, if that's what it takes to make these things easier. If anyone has any ideas, you can open an additional issue suggesting those. Hopefully that answers this 😄 |
That sounds reasonable, and I would think that 95% of the time the operation names would be unique anyway. I came across this in making a demo in which I was creating the same react component three different ways. But it would be great to have a warning logged to the console like "Detected duplicate operation names, only one will have types generated", or something to that effect. |
@JakeDawkins are these necessary for |
I think unique names are good practice, but it shouldn't be mandatory. It's like react component's naming, with unique names it's easier to debug tree, but sometimes (especially for big applications) you can't guaranties unique names without starting to be too verbose, like |
@JakeDawkins I'm using apollo codegen in a monorepo. Two separate apps in the same repo should be able to define their own version of Please reconsider your previous decision.
While I agree this is true some of the time, this strikes me as a very opinionated stance for a tool that is simply generating types for a project. I didn't expect my type generation tool to enforce it's own (unspoken) naming conventions. At minimum, Apollo codegen should output a warning whenever it skips generating types for a query. As @warmbowski said, the current behavior is unexpected and led to some confused debugging. |
@thefliik the current version of the CLI isn't meant to be used on multiple projects within the same repo at the same time. Each project in the monorepo should have their own As for warning, I don't disagree. But that should be opened as a separate issue. We're also pretty heavily backlogged on codegen issues at the moment, so I'm not sure how quickly something like this could be added/reviewed. |
@JakeDawkins gotcha. I created a separate issue to track the addition of a console warning for suppressed type generation #1427.
I also created a PR (#1428) as a "quick fix" to clarify usage of apollo codegen. |
For anyone else encountering this issue, the |
This is now a closed issue, so I'm not sure it is worth to comment it again. I think that forcing A valid use case is that a developer (or developers) needs two almost (or by chance even exactly!) same queries in different modules (as mentioned in #670 (comment)) and writes these queries alongside code that uses them. Why should he invent two different names for operations? A
I fear that many When working on larger project, two indendent developers may want the same/similar queries. They happily write queries for their needs, spawn independent PRs for different modules in project. But what if they chose the same names for their queries? When one of PRs is merged, the developer of the other has to change now- I don't say that having option to force unique operations names is bad. Maybe it can be a default. But There is still a problem (not mentioned above, surprisingly) how |
It's been a one year since last message and it seems like unsolved issue. Did anyone find workaround for this? |
See my comment above for a work-around. |
I agree with @marekscholle. Its particularly annoying when the queries are exactly the same (and thus I wish to use the same name), and do not wish to place them into a separate module for the same reasons that @marekscholle pointed out. (Using VUE SFC, so its nice to the structure of the data you are getting back within the same component). As such I would propose that the query check should take in to account more than just the name, if the query is exactly the same it should not throw this error (As theres no conflict, just duplication). On another point, is this also the same reason why Anonymous names are not allowed? I have noticed the VSCode Apollo GraphQL extension complains about Apollo not supporting anonymous names. However it appears to work fine, Unfortunately there does not appear to be a way to suppress the extension from flagging this as an issue with the editor. |
a workaround: assuming the following project structure
you can generate types by adding the following line to your {
...
"scripts": {
...
"generate": "./scripts/generate-types.sh src/"
},
} usage npm run generate
#!/bin/bash
set -euo pipefail
echo $PWD
cd $1
echo $PWD
for dir in */
do
dir=${dir%*/} # remove the trailing "/"
cd $dir
config=apollo.config.js
if test -f "$config"; then
echo "$dir/ contains $config file, generating types."
npx apollo client:codegen generated \
--config=apollo.config.js \
--target=typescript \
--globalTypesFile=../../src/global-types/$dir.ts \
--passthroughCustomScalars \
--useReadOnlyTypes
else
echo "$dir/ contains no $config file, skipping."
fi
cd ..
done drawbacks:
|
@marekscholle is right. What makes GraphQL so powerful is that you can request exactly what you want, even if it's the same endpoint. I have a use case where I want to call query I would be fine using the bigger query except that it takes several seconds to run with many parameters, so I don't want to call the larger query when I can call a smaller query for the same endpoint that takes milliseconds to run. But I can't with this current implementation of Apollo. I'm sure I'm not the only person in 2021 experiencing this issue so I would strongly encourage you to take a second look at this. |
Each operation in document must be unique (if follow GraphQL specification) |
@stelmakhivan Yes, but this is kind of unrelated to the discussion above. The problem would someone use two queries with the same name in the same document, in other words in single HTTP request / response. By enforcing different names we can enforce this can't happen, but IMO the cost would be two high. |
It makes no sense.
I have a component with a query like this (simplified example):
And another componentn with a query like this:
When I run
codegen:generate
to generate typescript type, I get an error:This forces me to think of unique query names for each query. But why? I don't need a unique query name here. The query is unique based on its content. The name is only helpful for debugging purposes.
And everything works fine. Apollo and graphql don't care about two queries having the same name. But I cannot generate types.
This seems to me to be horribly inconvenient. I want to colocate queries with my components. With this rule you could just as well disallow having two functions with the same name across the project.
Can this check be disabled somehow? Or do you think that my approach is wrong? If so, can you please try to explain the best course of action?
The text was updated successfully, but these errors were encountered: