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

Consider using the containing objects name as a prefix for schema types #1416

Open
lachezar opened this issue Jul 25, 2022 · 6 comments
Open
Labels
enhancement New feature or request server Issue related to caliban server

Comments

@lachezar
Copy link

I am running into naming collisions and I have to rename things (make them unique) otherwise the schema generation fails.

Example:

object PersonErrors:
  case class InvalidAge(...)
  case class InvalidName(...)

object CarErrors:
  case class InvalidAge(...)
  case class InvalidColor(...)

If you use the following validation types as part of your schema, then there will be collision since InvalidAge occurs twice. You can resolve this by either prefixing the name of the case classes with their wrapping object's name or by manually generating the schema and renaming it Schema.gen.rename("PersonErrorInvalidAge").

However, I believe it will be better experience if it is possible to just use the enwrapping object as a namespace / prefix and produce unique type names for the schema automatically.

I am not sure if this is possible / easy or that it wont break anything else, but I find it as a great feature that will make things simpler when developing with Caliban.

@ghostdogpr
Copy link
Owner

There is a simpler way which is using the @GQLName annotation.

Using the parent namespace is probably not desirable in the most cases and we can't really do it in a smart way because each Schema is independent from each other. The annotation lets you control the name easily.

@lachezar
Copy link
Author

Indeed, I think the option to use @GQLName was ignored because it "leaks" Caliban specifics inside the domain types.

This could be eliminated by having a second set of types that we translate back and forth from / to the domain types, however there is no Chimney for Scala 3 to alleviate the maintenance of these types and it seems there might never be since the macros in Scala 3 are not compatible with Chimney's code base.

Because of this we looked into an easier way to do the renaming ad-hoc and with less boilerplate.

Thank you for your response, at the moment this is not a real problem for us, so you can just close the issue if you want to, but I think it might provide a good guidance for other people trying to accomplish similar things.

@ghostdogpr
Copy link
Owner

I think it's difficult to find a heuristic that satisfies everyone but there might be ways to reduce the boilerplate so I'll keep this open and give it some thoughts 👍

@ghostdogpr ghostdogpr added enhancement New feature or request core server Issue related to caliban server labels Jul 25, 2022
@valentin-panalyt
Copy link

Indeed, I think the option to use @GQLName was ignored because it "leaks" Caliban specifics inside the domain types.

That's your root problem here. You are using domain-types for your graphql schema.
I suggest to not do that and to create specific API-types for your schema and then convert between your domain and the API. Besides getting rid of your problem here it has another advantages such as that changing your domain-types doesn't by accident impact your API and it also makes validation a lot more explicit and easy.

@lachezar
Copy link
Author

Unfortunately our project uses Scala 3 and there is no Chimney or other library to my knowledge that can make it easier to generate such DTOs without having to make them by hand. Making them by hand will quickly double the size of the project, which is just not feasible at the moment. Avoiding naming collisions as described in the first post is something that can be achieved no matter if you use dedicated DTOs or not - it just brings convenience.

@valentin-panalyt
Copy link

@lachezar have a look at https://github.com/arainko/ducktape

@ghostdogpr ghostdogpr removed the core label May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request server Issue related to caliban server
Projects
None yet
Development

No branches or pull requests

3 participants