graphql突变有效载荷中缺少字段

时间:2019-10-11 16:47:41

标签: reactjs graphql relaymodern

我是graphql的新手。我需要在React应用中使用Relay Modern执行变异。根据文档,突变有效载荷可能不同于成功响应还是错误响应。成功响应一切正常,但是错误响应我在控制台收到以下警告:

警告:RelayResponseNormalizer:有效负载不包含字段objectId: objectId的值。检查您是否正在使用用于提取有效内容的相同查询进行解析。

因为错误响应不包含objectId字段。除此之外,脚本的行为正确。这是一些来自文档的示例。突变示例:

mutation{
    createObject(name: 'john') {
        objectId
        error
    }
}

成功响应:

{
    "data": {
        "createObject": {
            "objectId": "123456",
            "error": null
        }
    }
}

和错误响应:

{
    "data": {
        "createObject": {
            "error": "[Name should not be Empty]"
        }
    }
}

这是react组件中的代码:

const mutation = graphql`
    mutation ComponentCreateObjectMutation($name: String) {
        createObject(name: $name) {
            objectId
            error
        }
    }
`;

commitMutation(
    environment,
    {
        mutation,
        variables,
        onCompleted: (response, errors) => {

            /* error is in response.error */
        },
        onError: (err) => {...}
    }
);

这是schema.graphql的内容:

type ObjectResponse {
    objectId: ID,
    error: String
}

type Mutation {
    createObject(name: String): ObjectResponse
}

解决此问题的最佳方法是什么? 预先感谢您的帮助!

0 个答案:

没有答案