首页 > 解决方案 > React / Apollo:显示 GraphQL 突变的错误(useMutation 挂钩)

问题描述

我很难弄清楚为什么我不能显示我的突变错误。

我在我的突变中实现了错误代码:

if location_lat < 0.1 and location_lon < 0.1:
            raise GraphQLError('E_CF_002')

现在在客户端,我调用了以下突变,它被称为表单的 onSubmit,我有目的地触发错误。

const [
    createEvent,
    { data: createData, loading: createLoading, error: createError}
  ] = useMutation(CREATE_EVENT);

如我的网络选项卡中所示,错误代码已成功返回:

{"errors": [{"message": "E_CF_002", "locations": [{"line": 

但是,当我尝试在对话框中显示错误时

        {createError && (
          <div>
            <Dialog open={openDialog} onClose={handleClose}>
              <h3>Sorry, an error occured while creating the event.</h3>
              <h3>Plese try again later. {JSON.stringify(createError)} </h3>
              <DialogActions>
                <Button onClick={handleClose} color="primary">
                  Okay
                </Button>
              </DialogActions>
            </Dialog>
          </div>
        )}

我只看到:

Sorry, an error occured while creating the event.
Plese try again later. {} 

谁能帮我理解为什么我的错误变量不包含消息等?

我首先尝试直接打印 createError.message 或 createError[0].message 甚至 createError.errors 甚至 createError.errors[0] 但是后来我通过打印 JSON.stringify(createError) 看到整个 JSON 都是空的。

我无法在 StackOverflow 或其他地方找到这个特定问题(或其解决方案)。

先感谢您!

标签: pythonreactjsdjangographqlreact-apollo

解决方案


推荐阅读