Query GraphQL Interface Types in GraphQL Playground

Eve Porcello
InstructorEve Porcello
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

Interfaces are similar to Unions in that they provide a mechanism for dealing with different types of data. However, an interface is more suited for data types that include many of the same fields. In this lesson, we will query different types of pets.

To follow along with these queries, go to the Pet Library GraphQL Playground.

Instructor: [00:00] If we sum the AllPets query for ID and name, we are going to see ID and name returned to us, just as we expect, but in the funded pet library, these data relationships are designed pretty differently.

[00:11] Let's open up the AllPets query in our schema and we'll see that the pet is no longer a type, but instead it's an interface.

[00:18] An interface is an abstract type that includes a set of fields. These fields must be used when creating new instances of that interface. We have an interface called pet. This is the base class. It has certain fields on it.

[00:32] We have several different implementations of that interface. Remember, our numerator from before that had cat, dog, rabbit, and stingray, these are now implementations of this interface.

[00:44] If we scroll down to the cat, we'll see that cat is a separate type that implements the pet interface. It has all of the different fields that are part of that interface, but then we can extent the cat to have a couple of different ones, so sleep amount and curious are now available on that cat.

[01:00] Let's click on stingray. That's another type. We have a couple of extra fields defined on that as well. Same with the rabbit and the dog. AllPets returns a list of pets, but all of these pets are different types, different implementations of the pet interface.

[01:16] If I want to see which one is which, I can query the _ _ type name field, this will give me the data about what type of pet we're querying. The way that we write a query for an interface is also a little bit different.

[01:29] Remember those extra fields that we had on the cat. I can use an inline fragment, ...onCat, and then I can query that field. Whenever there is a cat in the response, we're going to see a sleep amount value for the cat. For all other types, we're not going to see that extra field.

[01:47] We can do this for any additional fields that we'd like to. I can say onStingray and get how chill the stingray is on a scale of 1 to 10. This will be provided in the response only for stingrays.

[02:00] An interface gives us a little bit more flexibility when we're designing our domain's objects. We have a pet, that is an interface. Some base fields on that pet interface, and then we can create implementations of that interface and then create custom fields for each additional pet type.

egghead
egghead
~ 42 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today