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?