Is it OK to have extra custom logic on a graphql (input) type? #3983
dimitarizpitaime
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In our project we have a RoR GraphQL API.
In our mutations what we do right now is we take the incoming arguments, in the resolve method we transform them into a pure hash (nested if needed) and pass them on to the many service objects we have to do their business.
I guess we do that for 2 main reasons: 1 - once the project was an MVC rails project and so hash arguments were I guess the thing , 2 - everybody is comfortable using hashes, they are easy and straightforward to use
But some time ago we started playing around with the idea to use the input type objects graphql provides instead of always transforming them into hashes. This was we don't have to "prepare" the incoming arguments, but also we can have some logic in the graphql classes that makes sense to be there.
This is an example of what we do now / are talking about doing: (We are using Rails 6.1.3.2, ruby 2.7.2, graphql 1.9.11 - updating right now to 1.12.24)
Mutation: ...
The PaymentInfoInputType for example is:
The ItemInputType for example is:
And later in the services we have all the inputs nicely prepared to use, we can easily do for example:
The build_options (mutation, services,..) is way more complex than the example but it's just for an idea.
Let's say we want to start using the input object themselves, then for example we will have no building of the options, so:
Mutation:
The PaymentInfoInputType for example will be:
Then we'll have in the service:
The difference is not at all much in this simple example, but for us our mutations will become really slim after this change. Also some logic that is spread across the services just because, can be put inside the input object (when it makes sense). We can access the argument in the input objects as methods, instead of as hashes (some prefer this way).
My questions are: Does this make sense to do, is it ok to have some logic in the graphql input object classes, is it a better pattern NOT to build and use hashes, or is it somehow an anti pattern? Any thoughts are appreciated
Beta Was this translation helpful? Give feedback.
All reactions