Search code examples
api-designgraphqlgraphql-js

Are "grouping types" a good idea when designing GraphQL APIs?


When creating types in GraphQL, the list of fields on a type can become quite long after a while. One way of keeping the number of fields down would be to put some relevant subset of the fields in to one "grouping" type, so instead of user { last_name, first_name, height, weight, friends, favorite_clubs } one would have user { identification, friends, favorite_clubs } where the schema for identification is of course { last_name, first_name, height, weight }.

However, I'm not convinced that doing this is a good idea. The advantage is that the user type is more readable, but the things that worry me are:

  • You are creating a new GraphQLObjectType just for the sake of organisation, it's not a new node in my mental model of the graph...

  • The root parameter of the identification resolver would still be the user object, which feels like I'm doing something wrong...

Is this a good way of organising your API, or are my worries correct?


Solution

  • As it all comes down to what you prefer, there is no real answer to this question.

    I would personally simply list all the individual fields. :)
    Doesn't mean the other way is wrong.

    Anyways, don't overthink it.