artsy.github.io
artsy.github.io copied to clipboard
Comments for GraphQL Union vs. Interface
At Artsy we’ve been moving towards GraphQL for all of our new services. Acknowledging GraphLQ is a relatively new technology, we faced some challenging questions as we were developing one our most recent services.
Hi, I've read your blog post and trying to implement something similar to the interface example you are sharing. I was wondering if you could share what the QueryType class looks like in the interface example as I'd like to understand what should be the type for the instruments field:
query {
instruments {
id
name
category
... on StringInstrument {
numberOfStrings
}
}
}
@alice-letourneur the type of instruments field should be InstrumentInterface. In an example here we define and OrderInterface which can be any of these types.
When want to expose a field with this type, we will use the interface type like this example. Let me know if you have any questions.
@alice-letourneur the type of
instrumentsfield should beInstrumentInterface. In an example here we define andOrderInterfacewhich can be any of these types. When want to expose a field with this type, we will use the interface type like this example. Let me know if you have any questions.
Thanks a lot for your answer, helped a lot, the issue I was having was actually the same as what you are describing at the end of the post, I just didn't realise it was:
One issue we found after doing the above was, since this way we don’t reference StringInstrument and DrumInstrument types anywhere in our schema, they actually don’t end up showing in the generated schema. For them to show up we have to add them as orphan_types in the interface.
Read more about it here: https://graphql-ruby.org/type_definitions/interfaces#orphan-types and all makes sense now.
Oh yes! I remember that now! Yes, because these types are not directly accessed in our schema, they won't be included in the final schema unless we define them as Orphan Types. Happy that your problem is solved 😍
The example links that were posted now show 404 errors. I am under the impression that the interfaces can define fields and the objects can inherit the fields keeping the code DRY. I am unable to query the fields on the objects implementing the interface. I was hoping to look at the examples as I wasn't able to find what I was looking for on graphql-ruby.org
yes @kendallcarey , there are some examples here where price_field is defined on the interface and has a basic implementation.
Sorry about 404s, that repository this post is referring to is not public anymore 😞
Thank you, I have been using that resource to create interfaces. I ended creating a stackoverflow question based on issues that I've been having: https://stackoverflow.com/questions/69715042/graphql-ruby-creating-interfaces-that-objects-can-inherit-fields-from
Am I misunderstanding interfaces?
Replied to you on that thread.
Thank you so much! I can't tell you how grateful I am for your help. I responded!