简体   繁体   English

如何处理Relay Modern上的突变错误?

[英]How to handle mutation errors on Relay Modern?

I´m using ReactJS with Relay for my client. 我正在为我的客户端使用带有Relay的ReactJS。 Consider the following mutation: 考虑以下突变:

import {
    commitMutation,
    graphql
} from 'react-relay';

import environment from '../../../../environment';


const mutation = graphql`
    mutation CompanyMutation($company: CompanyInput!) {
        createCompany(data: $company) {
            id
        }
    }
`

export const createCompany = (company, callback) => {

console.log(company);

    const variables = {
        company: company
    }

    commitMutation(
        environment,
        {
            mutation,
            variables,
            onCompleted: () => {
                callback()
            },
            onError: (error) => {
                throw new Error(error)
            },
        },
    );
}

How can I handle an error response sent from my GraphQLServer: 如何处理从我的GraphQLServer发送的错误响应:

{
  "errors": [
    {
      "message": "E11000 duplicate key error collection: mom.companies index: name_1 dup key: { : \"TEST

\" }",
      "locations": [
        {
          "line": 4,
          "column": 3
        }
      ],
      "stack": "WriteError({\"code\":11000,\"index\":0,\"errmsg\":\"E11000 duplicate key error collection: mom.companies index: name_1 dup key: { : \\\"RENATO\\\" }\",\"op\":{\"createdAt\":1509103201877,\"deleted\":false,\"name\":\"TEST\",\"ein\":\"1234\",\"test\":false,\"multiSite\":true,\"siteLimit\":10,\"enabled\":false,\"_id\":\"59f316618ba865186815d3de\",\"__v\":0}})\n    at Function.MongoError.create (D:\\9. DEV\\WORKSPACE\\momejected\\node_modules\\mongodb-core\\lib\\error.js:31:11)\n    at toError (D:\\9. DEV\\WORKSPACE\\momejected\\node_modules\\mongodb\\lib\\utils.js:139:22)\n    at D:\\9. DEV\\WORKSPACE\\momejected\\node_modules\\mongodb\\lib\\collection.js:669:23\n    at handleCallback (D:\\9. DEV\\WORKSPACE\\momejected\\node_modules\\mongodb\\lib\\utils.js:120:56)\n    at D:\\9. DEV\\WORKSPACE\\momejected\\node_modules\\mongodb\\lib\\bulk\\unordered.js:465:9\n    at handleCallback (D:\\9. DEV\\WORKSPACE\\momejected\\node_modules\\mongodb\\lib\\utils.js:120:56)\n    at resultHandler (D:\\9. DEV\\WORKSPACE\\momejected\\node_modules\\mongodb\\lib\\bulk\\unordered.js:413:5)\n    at D:\\9. DEV\\WORKSPACE\\momejected\\node_modules\\mongodb-core\\lib\\connection\\pool.js:469:18\n    at _combinedTickCallback (internal/process/next_tick.js:67:7)\n    at process._tickDomainCallback (internal/process/next_tick.js:122:9)",
      "path": [
        "createCompany"
      ]
    }
  ],
  "data": {
    "createCompany": null
  }
}

On that case, the onError is not called. 在这种情况下,不会调用onError

How can I catch and handle this returned error inside the mutation ? 如何捕捉和处理突变中返回的错误?

The onCompleted will returns 2 parameters which are response and error on this document Relay Modern Mutations onCompleted将返回2个参数,分别是此文档上的responseerror 。Relay Modern Mutations

But in my opinion, the callback function does not easy to handle so I just change it to the Promise like this 但是我认为回调函数不容易处理,因此我将其更改为Promise

export const createCompany = (company) =>
  new Promise((resolve, reject) => {
    const variables = {
      company
    };

    commitMutation(
      environment,
      {
        mutation,
        variables,
        onCompleted: (resp, err) => {
          if (err) return reject(err);
          return resolve(resp);
        },
        onError: (err) => {
          return reject(err);
        }
      }
    );
  });

How to call in the component 如何调用组件

createCompany(company)
  .then((resp) => {
    // handle your response here..
  })
  .catch((err) => {
    // handle your error here..
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM