【问题标题】:Graphql Schema (Apollo) nested objectGraphql Schema (Apollo) 嵌套对象
【发布时间】:2017-09-14 01:39:52
【问题描述】:

我有一个返回以下数据的 API 端点:

{
  "2017": {
      "May": [
           {},
           {}
       ],
       "June": []
   },
   "2018": {}
  }

如何为这些嵌套对象创建架构,因为我似乎无法在架构中创建嵌套对象。

我正在使用 typeDefinitionsgraphql-tools,它适用于对象和对象数组,但我找不到解决此问题的方法。

典型的类型如下:

type Venue { 
  name: String, 
  url: String, 
  streetAddress: String, 
  streetAddress2: String, 
  streetAddress3: String, 
  city: String, 
  postalCode: String, 
  country: String, 
  phoneNumber: String, 
  info: String 
}

并且可以在其他类型中使用如下:

type Event {
    title: String,
    subTitle: String,
    slug: String,
    venues: [Venue],
    ...
}

但我不能这样做:

type Calendar {
    year: {
        month: [Event]
    }
}

【问题讨论】:

    标签: graphql apollo-server


    【解决方案1】:

    尝试分离嵌套对象:

    type Year {
        month: [Event]
    }    
    
    type Calendar {
        year: Year
    }
    

    【讨论】:

    • 我试过这个,但问题是没有关键的“年份”它是“2017”或“2018”
    • 所以也许fragments 对你有好处?见learngraphql.com/basics/fragments
    • 或者GraphQLScalar 可能是Calendar 类型,如stackoverflow.com/a/34229603/3883957
    • 在任何情况下,您可能希望将模型更改为在其自己的字段中与 Year 类型一起使用年份的模型,甚至作为 Year 类型本身的字段,并且然后Calendar 包含years: [Year]
    猜你喜欢
    • 2019-04-26
    • 2018-08-27
    • 2021-06-04
    • 2020-04-24
    • 2021-10-18
    • 2018-11-10
    • 2021-03-03
    • 2017-10-29
    • 2018-04-06
    相关资源
    最近更新 更多