artsy.github.io icon indicating copy to clipboard operation
artsy.github.io copied to clipboard

Comments for GraphQL Union vs. Interface

Open ashkan18 opened this issue 6 years ago • 9 comments

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.

ashkan18 avatar Jan 15 '19 18:01 ashkan18

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 avatar Jul 01 '20 10:07 alice-letourneur

@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.

ashkan18 avatar Jul 01 '20 12:07 ashkan18

@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.

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.

alice-letourneur avatar Jul 02 '20 07:07 alice-letourneur

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 😍

ashkan18 avatar Jul 02 '20 13:07 ashkan18

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

kendallcarey avatar Oct 26 '21 21:10 kendallcarey

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 😞

ashkan18 avatar Oct 26 '21 23:10 ashkan18

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?

kendallcarey avatar Oct 26 '21 23:10 kendallcarey

Replied to you on that thread.

ashkan18 avatar Oct 27 '21 14:10 ashkan18

Thank you so much! I can't tell you how grateful I am for your help. I responded!

kendallcarey avatar Oct 27 '21 15:10 kendallcarey