首页 > 解决方案 > React/Nextjs with GraphQL:将数据发布到后端(Spring)获取空对象

问题描述

我正在尝试使用 GraphQL Mutation 从我的前端(Nextjs)向我的后端(Java Spring)发送数据。我的问题是,当我从 GraphQL 操场调用 Mutation 时,在我的后端,我得到了一个具有空属性的对象。这是代码的一些贿赂:

我的突变定义:

export const SystemMutation = mutationType({
    definition(t) {
        t.field("addSystem",{
            type: System,
            args: {
                input: arg({
                    type: SystemInput
                })
            },
            async resolve(_parent, args, _context) {
                return await createSystem(args.input);
            }
        })
    }
})

这是调用后端存储数据的函数。

export const createSystem = async (system) => {
    const res = await axios.post(`${server}/api/system/add`, system);
    return res.data;
}

最后是应该接收数据的 Spring 控制器,当我在其中调试系统变量时,我得到了一个在所有属性中都具有空值的对象。

@PostMapping("/add")
    public ResponseEntity<?> add(@Validated(SystemDto.Create.class) @ModelAttribute SystemDto system,
                                 BindingResult result) {
        return this.systemService.createSystem(system, result);
    }

如果我在前端 console.log 参数“system”,我得到了我想要发送的数据。但是当我在后端发送它时,我得到了一个空对象。

我想念那里的东西吗?

谢谢你们的灯人。

标签: typescriptgraphqlnext.js

解决方案


推荐阅读