lundi 8 mai 2017

GraphQL type composition

I am exploring graphql and the question that is troubling me is if i can do some kind of type composition when defining my graphql server.

Let's assume that I have Person and Company in my collection. I having something like this:

const Person = new GraphQLObjectType({
  name: 'Person',
  fields: {
    firstName: {type: GraphQLString},
    lastName: {type: GraphQLString}
  }
});

const Company = new GraphQLObjectType({
  name: 'Company',
  fields: {
    name: {type: GraphQLString},
    website: {type: GraphQLString}
  }
});

But both Company and Person should have fields like: createdAt and id. So assume i have:

const Entity = new GraphQLObjectType({
  name: 'Entity',
  fields: {
    id: {type: GraphQLString},
    createdAt: {type: GraphQLString}
  }
});

So i was thinking about something like:

new GraphQLObjectType({
  ...
  extends: [Entity]
});

I know that there are interfaces but i think it is not what i am looking for, because then i need to implement interface anyway and what i want to achive is to keep some set of field definitions sepratly and reuse them across other types.

Any ideas? Am i trying to do something completely pointless?

Aucun commentaire:

Enregistrer un commentaire