mugli/learning-graphql

Question about GraphQLInterfaceType

reggi opened this issue · 1 comments

reggi commented

A little confused about the GraphQLInterfaceType documentation.

var NamedType = new GraphQLInterfaceType({
  name: 'Named',
  fields: {
    name: { type: GraphQLString }
  }
});

var DogType = new GraphQLObjectType({
  name: 'Dog',
  interfaces: [ NamedType ],
  fields: {
    name: { type: GraphQLString },
    barks: { type: GraphQLBoolean }
  },
  isTypeOf: (value) => value instanceof Dog
});

var CatType = new GraphQLObjectType({
  name: 'Cat',
  interfaces: [ NamedType ],
  fields: {
    name: { type: GraphQLString },
    meows: { type: GraphQLBoolean }
  },
  isTypeOf: (value) => value instanceof Cat
});

I don't understand why both the CatType and DogType have name as well as have the interfaces: [ NamedType ]. This seems redundant. What is the purpose of GraphQLInterfaceType?

https://github.com/mugli/learning-graphql/blame/master/7.%20Deep%20Dive%20into%20GraphQL%20Type%20System.md#L65-L90

roor0 commented

You may want to use an interface to be able to get all the data in a single simplified graphql query.
one of the hardest things in CS is naming things. I maybe would use Pet instead of Named
You can then have dogs that are pets, and cats that are pets. Some things apply to dogs only and somethings apply to cats only, but lots is shared and I might want to get a list of all pets, or named items, and a few of the attributes that apply to them. See https://graphql.org/learn/schema/#interfaces