如何使用GraphQL查询枚举数据类型

huangapple go评论91阅读模式
英文:

how to query enum data type with grpahql

问题

我如何从GraphQL查询枚举。我正在使用Nest.js和GraphQL,并且出现错误:"message": "Enum "TraitReportType" cannot represent value: "EMBEDDED"。",
我尝试使用type: EMBEDDED,但不起作用。还尝试使用try {},但在子选择中也不起作用。

  1. 这是我的类型:@Expose()
  2. @IsDefined()
  3. @IsEnum(TraitReportType)
  4. @ApiProperty({ enum: TraitReportType })
  5. @Field(() => TraitReportType, { nullable: false })
  6. type: TraitReportType;
  7. 进一步的TraitReportTypeimport { registerEnumType } from '@nestjs/graphql';
  8. export enum TraitReportType {
  9. LAYOUT = 'layout',
  10. EMBEDDED = 'embedded',
  11. }
  12. registerEnumType(TraitReportType, {
  13. name: 'TraitReportType',
  14. description: 'Trait Report Type枚举类型',
  15. });

我如何查询:
query EntityReportsQuery($id: ID!) {
entity(id: $id) {
id
secondaryId
reports {
key
name
description
icon {
type
component
name
size
source
}
type
layout
url
}
}
}

我从数据库查询的数据是:
{
"key": "A",
"url": "https://search.mevris.app/s/app-dashboards/app/dashboards#/view/204e17a0-a6df-11ed-ac80-b52d3b051a25?embed=true&_g=(filters:!(('$state':(store:globalState),meta:(alias:!n,disabled:!f,key:entityId.keyword,negate:!f,params:(query:%2203258d26-3914-4a19-b919-f8e88f3a68dd%22),type:phrase),query:(match_phrase:(entityId:%22${id}%22)))),refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&show-time-filter=true&hide-filter-bar=true",
"icon": {
"name": "current-ac",
"size": 2.7,
"type": "type icon",
"component": "component"
},
"name": "Current",
"type": "EMBEDDED",
"description": "Sum of Current"
}

  1. 我尝试使用type: EMBEDDED,但不起作用。
  2. <details>
  3. <summary>英文:</summary>
  4. how can i query enum from graphql . i am using nest.js with graphql
  5. and error : &quot;message&quot;: &quot;Enum \&quot;TraitReportType\&quot; cannot represent value: \&quot;EMBEDDED\&quot;&quot;,
  6. i try with type:EMBEEDED but did not works . also try with try {} also did not work with sub selection .

here is my type : @Expose()
@IsDefined()
@IsEnum(TraitReportType)
@ApiProperty({ enum: TraitReportType })
@Field(() => TraitReportType, { nullable: false })
type: TraitReportType;

further TraitReportType : import { registerEnumType } from '@nestjs/graphql';

export enum TraitReportType {
LAYOUT = 'layout',
EMBEDDED = 'embedded',
}

registerEnumType(TraitReportType, {
name: 'TraitReportType',
description: 'Trait Report Type enum types',
});

how i query : query EntityReportsQuery($id: ID!) {
entity(id: $id) {
id
secondaryId
reports {
key
name
description
icon {
type
component
name
size
source
}
type
layout
url
}
}
} and data which i query from db is : {
"key": "A",
"url": "https://search.mevris.app/s/app-dashboards/app/dashboards#/view/204e17a0-a6df-11ed-ac80-b52d3b051a25?embed=true&_g=(filters:!(('$state':(store:globalState),meta:(alias:!n,disabled:!f,key:entityId.keyword,negate:!f,params:(query:%2203258d26-3914-4a19-b919-f8e88f3a68dd%22),type:phrase),query:(match_phrase:(entityId:%22${id}%22)))),refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&show-time-filter=true&hide-filter-bar=true",
"icon": {
"name": "current-ac",
"size": 2.7,
"type": "type icon",
"component": "component"
},
"name": "Current",
"type": "EMBEDDED",
"description": "Sum of Current"
}
`

  1. i try with type:EMBEEDED but did not works .
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. {
  6. "key": "A",
  7. "url": "https://search.mevris.app/s/app-dashboards/app/dashboards#/view/204e17a0-a6df-11ed-ac80-b52d3b051a25?embed=true&_g=(filters:!((%24state:(store:globalState),meta:(alias:!n,disabled:!f,key:entityId.keyword,negate:!f,params:(query:%2203258d26-3914-4a19-b919-f8e88f3a68dd%22),type:phrase),query:(match_phrase:(entityId:%22${id}%22)))),refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&show-time-filter=true&hide-filter-bar=true",
  8. "icon": {
  9. "name": "current-ac",
  10. "size": 2.7,
  11. "type": "type icon",
  12. "component": "component"
  13. },
  14. "name": "Current",
  15. "type": "embedded",
  16. "description": "Sum of Current"
  17. } i just replace in db "type": "EMBEDDED", with "type": "embedded" then it works
  18. <details>
  19. <summary>英文:</summary>
  20. {
  21. &quot;key&quot;: &quot;A&quot;,
  22. &quot;url&quot;: &quot;https://search.mevris.app/s/app-dashboards/app/dashboards#/view/204e17a0-a6df-11ed-ac80-b52d3b051a25?embed=true&amp;_g=(filters:!((&#39;$state&#39;:(store:globalState),meta:(alias:!n,disabled:!f,key:entityId.keyword,negate:!f,params:(query:%2203258d26-3914-4a19-b919-f8e88f3a68dd%22),type:phrase),query:(match_phrase:(entityId:%22${id}%22)))),refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&amp;show-time-filter=true&amp;hide-filter-bar=true&quot;,
  23. &quot;icon&quot;: {
  24. &quot;name&quot;: &quot;current-ac&quot;,
  25. &quot;size&quot;: 2.7,
  26. &quot;type&quot;: &quot;type icon&quot;,
  27. &quot;component&quot;: &quot;component&quot;
  28. },
  29. &quot;name&quot;: &quot;Current&quot;,
  30. &quot;type&quot;: &quot;EMBEDDED&quot;,
  31. &quot;description&quot;: &quot;Sum of Current&quot;
  32. } i just replace in db &quot;type&quot;: &quot;EMBEDDED&quot;, with &quot;type&quot;: &quot;embedded&quot; then it works
  33. </details>

huangapple
  • 本文由 发表于 2023年7月12日 21:05:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76670900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定