我如何将JSON对象发送到GraphQl
我将GraphQl与Nestjs一起使用。(打字稿)
突变语法错误显示。
即使我发送单个JSON值
rules: "{ \"==\" : \"[ { \"var\" : \"a.on\" }, true ]\"}"
给出错误:“ JSON”类型的期望值,
这是我的示例突变。
condition: {
rules: {
"and" : [
{"<" : [ { "var" : "a.state.temperature" }, 27 ]},
{"==" : [ { "var" : "b.state.thermostatMode" }, "cooling" ] }
]
},
data: {
a: { source: 'Entity', id: '123' },
b: { source: 'Entity', id: '345' },
}
},
并且是mydto的
条件:
@Expose()
@IsOptional()
@ApiProperty({ type: () => RuleConditionInputDto })
@Field(() => JSON, { nullable: true })
condition?: RuleConditionInputDto;
ruleconditionInputdto
@ObjectType('RuleCondition')
export class RuleConditionDto {
@Expose()
@IsDefined()
@ApiProperty()
@Field(() => GraphQLJSON)
rules: RulesLogic;
@Expose()
@IsDefined()
@ApiPropertyOptional()
@Field(() => RuleConditionDataDto, { nullable: true })
data?: RuleConditionDataDto;
}
@InputType('RuleConditionInput')
export class RuleConditionInputDto {
@Expose()
@IsDefined()
@ApiProperty()
@Field(() => GraphQLJSON)
rules: RulesLogic;
@Expose()
@IsDefined()
@ApiPropertyOptional()
@Field(() => RuleConditionDataInputDto, { nullable: true })
data?: RuleConditionDataInputDto;
}
i am using graphql with nestjs .(typescript)
mutation syntax error shows.
even if i send single json value
rules: "{ \"==\" : \"[ { \"var\" : \"a.on\" }, true ]\"}"
give error : Expected value of type "JSON",
here is my sample mutation .
condition: {
rules: {
"and" : [
{"<" : [ { "var" : "a.state.temperature" }, 27 ]},
{"==" : [ { "var" : "b.state.thermostatMode" }, "cooling" ] }
]
},
data: {
a: { source: 'Entity', id: '123' },
b: { source: 'Entity', id: '345' },
}
},
and is myDTO
Condition :
@Expose()
@IsOptional()
@ApiProperty({ type: () => RuleConditionInputDto })
@Field(() => JSON, { nullable: true })
condition?: RuleConditionInputDto;
and RuleConditionInputDto
@ObjectType('RuleCondition')
export class RuleConditionDto {
@Expose()
@IsDefined()
@ApiProperty()
@Field(() => GraphQLJSON)
rules: RulesLogic;
@Expose()
@IsDefined()
@ApiPropertyOptional()
@Field(() => RuleConditionDataDto, { nullable: true })
data?: RuleConditionDataDto;
}
@InputType('RuleConditionInput')
export class RuleConditionInputDto {
@Expose()
@IsDefined()
@ApiProperty()
@Field(() => GraphQLJSON)
rules: RulesLogic;
@Expose()
@IsDefined()
@ApiPropertyOptional()
@Field(() => RuleConditionDataInputDto, { nullable: true })
data?: RuleConditionDataInputDto;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你说:
,但上面给出的值不是JSON值,它也是字符串。如果您给出一个实际的JavaScript对象(没有“ JSON值”),例如:
You say:
but the value given above is not a JSON value, it is a string. You may have better luck if you give an actual JavaScript object (there isn't such a thing as a "JSON value"), e.g.: