我如何将JSON对象发送到GraphQl

发布于 2025-02-11 18:45:37 字数 1501 浏览 3 评论 0原文

我将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

公布 2025-02-18 18:45:37

你说:

即使我发送了单个JSON值

 规则:“ {\” == \“:\” [{{\“ var \”:\“ a.on \”},true] \”}“}”}
 

,但上面给出的值不是JSON值,它也是字符串。如果您给出一个实际的JavaScript对象(没有“ JSON值”),例如:

rules: { "==" : [ { "var" : "a.on" }, true ]}

You say:

even if i send single json value

rules: "{ \"==\" : \"[ { \"var\" : \"a.on\" }, true ]\"}"

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

rules: { "==" : [ { "var" : "a.on" }, true ]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文