Graphql 如何使用大多数相同字段设计多个对象类型

Graphql 如何使用大多数相同字段设计多个对象类型,graphql,graphql-js,Graphql,Graphql Js,以下是我的typedef: interface BaseTopic { id: ID! title: String! } type BaseTopicWithAuthor implements BaseTopic { id: ID! title: String! author: BaseUser! } type Topic implements BaseTopic { id: ID! title: String!

以下是我的typedef:

  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
  }
问题是,
Topic
对象类型和
TopicDetail
对象类型的大多数字段都相同。重复写入这些字段太冗长了。如果有更多的“主题”包含几个不同的字段,我必须一次又一次地编写这些相同的字段(
id
title
author\u id
content
,等等),并为新的
主题
对象类型添加一个不同的字段。例如

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
}
我读过这篇文章:

所以,为了减少重复字段,唯一的选择是使用package

我知道我可以定义一些常见的字符串类型def并像这样使用它

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

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

但它太难看了,而且不是图形化的。这是JS语言的使用方式。graphql SDL是否支持任何处理此场景的方法

您是否尝试使用的扩展运算符(…)ES6@stchr对不起,我没听清楚。如何使用ES6的扩展运算符处理此问题?我更新了我的帖子,请检查。@slideshowp2我更新了该帖子的原始答案,以提供更多详细信息。但答案仍然是正确的——如果数据模型需要,SDL中没有语法可以避免重复字段。@DanielRearden,谢谢。GraphQL SDL是否计划支持这一点?GraphQL的开发人员应该能够想到这个场景。为什么GraphQL不引用静态类型语言,比如TypeScript。支持继承、交集类型(graphql似乎不支持这种类型,只支持联合类型)等等。您可以查看打开的建议和问题。