You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
tl;dr: my real need is what is proposed here. But meanwhile, I can live with a type that has for properties:
Only one of its fields is filled. If no fields is filled → error, if two or more fields are filled → error
When used as a field, it returns the data of the one field it has that has been setup
let's dig into what I want, and how I solved it:
• Let's consider a business function do_stuff(args) that takes an hash described by the graphql schema.
• Within that function, I need a key to be of two different kinds: either a string id of an object I already got in my system, or a more complex thing (a hash with many values).
So basically, here is do_stuff(args)
defdo_stuff(args)ifargs[:thing].class.name == "String"print("I can do something with that #{args[:thing]}")elseprint("That thing is more complex, let's do something complex with it")endend
That function do_stuff() is being used at other places, like in the Rest API or through a controller's method, so I don't want to include logic specific to handling the GraphQL schema within it.
In GraphQL, to keep a neat strongly and statically typed schema, I chose to make a compound type, that's as follows:
What I don't like about this solution is that I'm using an instance variable on the BaseInputObject to store the information, then I use instance_variable_get to replace the data of the element within the type of the outter object.
I guess as an improvement, I could avoid the instance_variable_get() part by defining a getter on that instance variable, but then I'd still have the issue with having code contextual to what ThatCompoundThing is within the MyMutationAttributes type.
So in the end the questions are:
is there a way to build ThatCompoundThing so I don't have to hack MyMutationAttributes, and that's already designed in graphql-ruby?
if there is, how can I do that? did I miss a part of the documentation? if not, maybe I could write that missing doc 😉
if there is not, would that be possible? do you think this is a legitimate use case, and a reason to implement something that could enable that?
thank you for reading this, and thank you for the project 🙌
The text was updated successfully, but these errors were encountered:
Thanks for the detailed write-up! I'm also having trouble with expressing mutually exclusive inputs. There's nothing built-in to graphql-ruby yet, so I've been solving it in plain Ruby code, for example:
It has a downside of being a bit verbose, but it has the upside of being very straight-forward (it avoids messing with object internals, or integrating too closely with GraphQL-Ruby object lifecycle methods).
I'm hoping to reduce the boilerplate soon by supporting "field filters" that could help in a case like this, see: #1758 . But in the meantime, I'm pretty happy with the plain Ruby approach, because of its simplicity. What do you think about it?
tl;dr: my real need is what is proposed here. But meanwhile, I can live with a type that has for properties:
let's dig into what I want, and how I solved it:
• Let's consider a business function
do_stuff(args)
that takes an hash described by the graphql schema.• Within that function, I need a key to be of two different kinds: either a string
id
of an object I already got in my system, or a more complex thing (a hash with many values).So basically, here is
do_stuff(args)
That function
do_stuff()
is being used at other places, like in the Rest API or through a controller's method, so I don't want to include logic specific to handling the GraphQL schema within it.In GraphQL, to keep a neat strongly and statically typed schema, I chose to make a compound type, that's as follows:
and that type is being used in the mutations:
But my issue is that
args
will be like so:which does not match what my
do_stuff()
code expects.As a solution, here's what I implemented:
What I don't like about this solution is that I'm using an instance variable on the
BaseInputObject
to store the information, then I useinstance_variable_get
to replace the data of the element within the type of the outter object.I guess as an improvement, I could avoid the
instance_variable_get()
part by defining a getter on that instance variable, but then I'd still have the issue with having code contextual to whatThatCompoundThing
is within theMyMutationAttributes
type.So in the end the questions are:
ThatCompoundThing
so I don't have to hackMyMutationAttributes
, and that's already designed in graphql-ruby?thank you for reading this, and thank you for the project 🙌
The text was updated successfully, but these errors were encountered: