简体   繁体   English

如何为复杂数组定义 Graphql 类型

[英]How to define the Graphql type for complex array

I am having a headache about defining the correct return types for this complex array I am returning via Graphql.我对为我通过 Graphql 返回的这个复杂数组定义正确的返回类型感到头疼。 I am also using the Nest.JS framework.我也在使用 Nest.JS 框架。

Here is my data:这是我的数据:

[
 [ { Id: '1' }, [ [ { x: '01-02-2021', y: 12345 } ] ] ],
 [ { Id: '172' }, [ [Object], [Object] ] ],
 [ { Id: '173' }, 
  [
  [Object], [Object], [Object], [Object],[Object], [Object], [Object], [Object],
  [Object], [Object], [Object], [Object],[Object], [Object] 
  ]
 ]
]

This is the Class I am trying to return, as [ Grouped ] for the resolver.这是我试图返回的类,作为解析器的 [ 分组 ]。

@ObjectType()
export class Grouped {
  Id: string;
  SumPerDay: SumPerDay[];
}

@ObjectType()
class SumPerDay {
  x: string;
  y: number;
}

This is however, returning null for both id and SumPerday.然而,这对于 id 和 SumPerday 都返回 null。

Any thoughts about this would be welcome!欢迎对此提出任何想法!

I solved it by adding the types.我通过添加类型解决了它。

So instead of returning the above array, I slightly changed the returning data into :因此,我没有返回上述数组,而是将返回的数据稍微更改为:

    "Grouped": [
  {
    "Id": "1",
    "_data": [
      {
        "x": "Thu Sep 30 2021",
        "y": 0.02
      }
    ]
  },
  {
    "Id": "172",
    "_data": [
      {
        "x": "Mon Oct 04 2021",
        "y": 2327.7599999999998
      },
      {
        "x": "Wed Oct 06 2021",
        "y": 172.91000000000003
      }
    ]
  },
]

Now it is working.现在它正在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM