0

Here are my typedefs:

  interface BaseTopic {
    id: ID!
    title: String!
  }

  type BaseTopicWithAuthor implements BaseTopic {
    id: ID!
    title: String!
    author: BaseUser!
  }

  type Topic implements BaseTopic {
    id: ID!
    title: String!
    author_id: ID!
    content: String!
    reply_count: Int
    visit_count: Int
    create_at: String
    author: BaseUser!
  }

  type TopicDetail implements BaseTopic {
    id: ID!
    author_id: ID!
    content: String!
    title: String!
    reply_count: Int
    visit_count: Int
    create_at: String
    author: BaseUser!
    replies: [Reply]!
    is_collect: Boolean
  }

The issue is the most of the fields are the same for Topic object type and TopicDetail object type. It's too verbose to write these same fields multiple times. What if there are more "Topic" with several different fields, I have to write these same fields(id, title, author_id, content, etc...) again and again and add one different field for new Topic object type. E.g.

type TopicNew implements BaseTopic {
    # I have to write below fields which same with Topic and TopicDetail object types again. 
    # It's too verbose.
    id: ID!
    author_id: ID!
    content: String!
    title: String!
    reply_count: Int
    visit_count: Int
    create_at: String
    author: BaseUser!
    # add a new different field
    newField: String
}

I have read this post: How to Inherit or Extend typeDefs in GraphQL

So, for reducing the duplicated fields, the only choice is to use graphql-s2s package?

I know I can define some common string type defs and use it like

type Topic implements BaseTopic {
    ${commonFields}
    author: BaseUser!
}

type TopicDetail implements BaseTopic {
    ${commonFields}
    author: BaseUser!
    replies: [Reply]!
    is_collect: Boolean
}

But it's too ugly and it's not graphql way. It's JS language way using template literals. Does graphql SDL support any way to handle this scenario?

Lin Du
  • 88,126
  • 95
  • 281
  • 483
  • Did you tried using spread operator (...) of ES6 – stchr Jan 07 '20 at 05:24
  • @stchr Sorry, I didn't catch that. How to handle this using the spread operator of ES6? I updated my post, please check. – Lin Du Jan 07 '20 at 05:31
  • 1
    @slideshowp2 I updated my original answer to that post to provide additional details. But the answer still stands -- there is no syntax in SDL that would let you avoid duplicating fields if that's what your data model calls for. – Daniel Rearden Jan 07 '20 at 06:09
  • @DanielRearden Thanks. Does GraphQL SDL plan to support this? Developers of GraphQL should be able to think of this scenario. Why doesn't GraphQL refer to statically typed languages, like TypeScript. Support the inheritance, Intersection type( it seems graphql doesn't support this, only support Union types) and so on.. – Lin Du Jan 07 '20 at 06:15
  • You can review open proposals and issues [here](https://github.com/graphql/graphql-spec). – Daniel Rearden Jan 07 '20 at 06:55

0 Answers0