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

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

how to query enum data type with grpahql

问题

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

这是我的类型:@Expose()
@IsDefined()
@IsEnum(TraitReportType)
@ApiProperty({ enum: TraitReportType })
@Field(() => TraitReportType, { nullable: false })
type: TraitReportType;

进一步的TraitReportTypeimport { registerEnumType } from '@nestjs/graphql';

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

registerEnumType(TraitReportType, {
	name: 'TraitReportType',
	description: 'Trait Report Type枚举类型',
});

我如何查询:
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"
}

我尝试使用type: EMBEDDED,但不起作用。

<details>
<summary>英文:</summary>

how can i query enum from graphql . i am using nest.js with graphql 

and error : &quot;message&quot;: &quot;Enum \&quot;TraitReportType\&quot; cannot represent value: \&quot;EMBEDDED\&quot;&quot;,




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"
}
`


i try with type:EMBEEDED but did not works .

</details>


# 答案1
**得分**: 0

{
  "key": "A",
  "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",
  "icon": {
    "name": "current-ac",
    "size": 2.7,
    "type": "type icon",
    "component": "component"
  },
  "name": "Current",
  "type": "embedded",
  "description": "Sum of Current"
} i just replace in db "type": "EMBEDDED", with "type": "embedded" then it works

<details>
<summary>英文:</summary>

    {
      &quot;key&quot;: &quot;A&quot;,
      &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;,
      &quot;icon&quot;: {
        &quot;name&quot;: &quot;current-ac&quot;,
        &quot;size&quot;: 2.7,
        &quot;type&quot;: &quot;type icon&quot;,
        &quot;component&quot;: &quot;component&quot;
      },
      &quot;name&quot;: &quot;Current&quot;,
      &quot;type&quot;: &quot;EMBEDDED&quot;,
      &quot;description&quot;: &quot;Sum of Current&quot;
    }  i just replace in db &quot;type&quot;: &quot;EMBEDDED&quot;, with &quot;type&quot;: &quot;embedded&quot; then it works 

</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:

确定