简体   繁体   English

GraphQL - 突变响应后的自定义错误消息

[英]GraphQL - custom error message after mutation response

I'm leraning Graphql and i have a problem that i can't get any further.我正在学习 Graphql,但遇到了一个无法解决的问题。

In the backend (nodeJS) you can be entered in a newsletter list, it works without problems.在后端 (nodeJS) 中,您可以输入时事通讯列表,它可以正常工作。 If the email address already exists, Mailchimp will return "Member exists".如果电子邮件地址已经存在,Mailchimp 将返回“成员存在”。 So far everything is fine.到目前为止一切都很好。

If I understand it correctly, the answer must be in the format of the respective type.如果我理解正确,答案必须是相应类型的格式。 I would now like to issue a custom message if the registration does not work.如果注册不起作用,我现在想发出自定义消息。

So, how exactly do I have to proceed to adapt the correct response and send a custom message?那么,我究竟该如何调整正确的响应并发送自定义消息?

The mutation:突变:

createNewsletterSubscription(
    data: CreateNewsletterSubscriptionInput
): NewsletterSubscription!

And the type:和类型:

type NewsletterSubscription {
email: String!
name: String!

} }

Response which gives me "Member exists":给我“成员存在”的响应:

JSON.parse(error.response.text).title

The mutation method:变异方法:

async createNewsletterSubscription(parent, args, { db }, info) {
    const { name, email } = args.data;
    const newEmailSubscription = {
        email,
        name,
    };

    const FNAME = name;

    try {
        const result = await request
            .post(  `https://${mailchimpInstance}.api.mailchimp.com/3.0/lists/${MAILCHIMP_LIST_ID}/members`
            )
            .set(
                'Authorization',
                // eslint-disable-next-line no-buffer-constructor
                `Basic ${new Buffer(`any:${MAILCHIMP_APIKEY}`).toString(
                    'base64'
                )}`
            )
            .send({
                email_address: email,
                status: 'subscribed',
                merge_fields: {
                    FNAME,
                },
            });

        if (result.status === 200 && result.body.status === 'subscribed') {
            return newEmailSubscription;
        }
    } catch (error) {
        // If member exists throw new error
        return JSON.parse(error.response.text).title;
    }
},

This is the output in my GraphQL Playgrund:这是我的 GraphQL Playgrund 的输出:

{
  "data": null,
  "errors": [
    {
      "message": "Cannot return null for non-nullable field NewsletterSubscription.email.",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "createNewsletterSubscription",
        "email"
      ]
    }
  ]
}
} catch (error) { // If member exists throw new error return JSON.parse(error.response.text).title; }

You can not just return simple response - looking like normal one, not error.你不能只返回简单的响应——看起来像正常的响应,而不是错误。

Handling errors is explained in details on apollo server docs处理错误在 apollo server docs中有详细解释

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

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