Handling apollo-server mutation error with apollo-client











up vote
0
down vote

favorite












I'm trying to solve how is the best way to create an error response on apollo-server side and handle it in appropriate way on client (apollo).



I was googling a lot but didn't get satisfactory answer.



So here is my actual implementation:



I created an interface and then new type MutationResponse (This is my own implementation of mutation response below).



  interface IMutationResponse {
code: Int!
path: String!
fields: String!
message: String!
success: Boolean!
typeError: String!
}



type MutationResponse implements IMutationResponse {
code: Int!
path: String!
fields: String!
message: String!
success: Boolean!
typeError: String!
}


I created another type where Im using MutationResponse:



 type CustomerResponse {
customer: Customer
photoFile: File
serverResponse: [MutationResponse]
}


This is my simplified implementation of mutation (createCustomer):



  Mutation: {
createCustomer: async (_, { photoFile, customer }) => {
try {
const {
dataValues: { customerID, firstname, lastname, email, phone }
} = await models.Customer.create(customer);
const customerUniqueDir = path.join(customerPath,
`${customerID}`);
} catch (createCustomerError) {
console.log("createCustomerError", createCustomerError);
const serverResponse = createValidationErrorResponse(
400,
createCustomerError
);
// throw new ApolloError({ serverResponse });
return {
serverResponse
};
}
},


creating mutation response on apollo-server:




  1. First scenario


**If I'm in the catch block, server return serverResponse object **
...



 catch(customerPhotoError) {
const serverResponse = createValidationErrorResponse(
400,
createCustomerError
);
return {
serverResponse
};
}


but on the apollo client I fall into the try block so it looks like that server didn't throw any exception and response was successful (200).
There is an option to parse the response and handle it in appropriate way (f.e. according to received status code in response).



Is this right solution ? It's good way how to thrown error on server and it's normal that you are handling this error in try block (or in then callback if you are not using async await) on client ?




  1. scenario


Apollo server 2 introduced error handling: https://www.apollographql.com/docs/apollo-server/features/errors.html



Okey nice ! As is written in article (posted above) apollo provide rich error feedback in response (errors:)



but
when I'm in catch block in my mutation and I throw on of provided apollo-error let say :



 import { ApolloError } from "apollo-server";
.....
catch(createCustomerError) {
throw new ApolloError(serverResponse);
}


Mutation response is part of the array errors: provided by apollo and finally I fell into the catch block on client side but inside of the block I get Cannot read property 'data' of undefined. So I'm not able to access to data in catch block.



here is the response:



enter image description here



Is there any solution how I could solve it ?



Thanks for any help.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm trying to solve how is the best way to create an error response on apollo-server side and handle it in appropriate way on client (apollo).



    I was googling a lot but didn't get satisfactory answer.



    So here is my actual implementation:



    I created an interface and then new type MutationResponse (This is my own implementation of mutation response below).



      interface IMutationResponse {
    code: Int!
    path: String!
    fields: String!
    message: String!
    success: Boolean!
    typeError: String!
    }



    type MutationResponse implements IMutationResponse {
    code: Int!
    path: String!
    fields: String!
    message: String!
    success: Boolean!
    typeError: String!
    }


    I created another type where Im using MutationResponse:



     type CustomerResponse {
    customer: Customer
    photoFile: File
    serverResponse: [MutationResponse]
    }


    This is my simplified implementation of mutation (createCustomer):



      Mutation: {
    createCustomer: async (_, { photoFile, customer }) => {
    try {
    const {
    dataValues: { customerID, firstname, lastname, email, phone }
    } = await models.Customer.create(customer);
    const customerUniqueDir = path.join(customerPath,
    `${customerID}`);
    } catch (createCustomerError) {
    console.log("createCustomerError", createCustomerError);
    const serverResponse = createValidationErrorResponse(
    400,
    createCustomerError
    );
    // throw new ApolloError({ serverResponse });
    return {
    serverResponse
    };
    }
    },


    creating mutation response on apollo-server:




    1. First scenario


    **If I'm in the catch block, server return serverResponse object **
    ...



     catch(customerPhotoError) {
    const serverResponse = createValidationErrorResponse(
    400,
    createCustomerError
    );
    return {
    serverResponse
    };
    }


    but on the apollo client I fall into the try block so it looks like that server didn't throw any exception and response was successful (200).
    There is an option to parse the response and handle it in appropriate way (f.e. according to received status code in response).



    Is this right solution ? It's good way how to thrown error on server and it's normal that you are handling this error in try block (or in then callback if you are not using async await) on client ?




    1. scenario


    Apollo server 2 introduced error handling: https://www.apollographql.com/docs/apollo-server/features/errors.html



    Okey nice ! As is written in article (posted above) apollo provide rich error feedback in response (errors:)



    but
    when I'm in catch block in my mutation and I throw on of provided apollo-error let say :



     import { ApolloError } from "apollo-server";
    .....
    catch(createCustomerError) {
    throw new ApolloError(serverResponse);
    }


    Mutation response is part of the array errors: provided by apollo and finally I fell into the catch block on client side but inside of the block I get Cannot read property 'data' of undefined. So I'm not able to access to data in catch block.



    here is the response:



    enter image description here



    Is there any solution how I could solve it ?



    Thanks for any help.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to solve how is the best way to create an error response on apollo-server side and handle it in appropriate way on client (apollo).



      I was googling a lot but didn't get satisfactory answer.



      So here is my actual implementation:



      I created an interface and then new type MutationResponse (This is my own implementation of mutation response below).



        interface IMutationResponse {
      code: Int!
      path: String!
      fields: String!
      message: String!
      success: Boolean!
      typeError: String!
      }



      type MutationResponse implements IMutationResponse {
      code: Int!
      path: String!
      fields: String!
      message: String!
      success: Boolean!
      typeError: String!
      }


      I created another type where Im using MutationResponse:



       type CustomerResponse {
      customer: Customer
      photoFile: File
      serverResponse: [MutationResponse]
      }


      This is my simplified implementation of mutation (createCustomer):



        Mutation: {
      createCustomer: async (_, { photoFile, customer }) => {
      try {
      const {
      dataValues: { customerID, firstname, lastname, email, phone }
      } = await models.Customer.create(customer);
      const customerUniqueDir = path.join(customerPath,
      `${customerID}`);
      } catch (createCustomerError) {
      console.log("createCustomerError", createCustomerError);
      const serverResponse = createValidationErrorResponse(
      400,
      createCustomerError
      );
      // throw new ApolloError({ serverResponse });
      return {
      serverResponse
      };
      }
      },


      creating mutation response on apollo-server:




      1. First scenario


      **If I'm in the catch block, server return serverResponse object **
      ...



       catch(customerPhotoError) {
      const serverResponse = createValidationErrorResponse(
      400,
      createCustomerError
      );
      return {
      serverResponse
      };
      }


      but on the apollo client I fall into the try block so it looks like that server didn't throw any exception and response was successful (200).
      There is an option to parse the response and handle it in appropriate way (f.e. according to received status code in response).



      Is this right solution ? It's good way how to thrown error on server and it's normal that you are handling this error in try block (or in then callback if you are not using async await) on client ?




      1. scenario


      Apollo server 2 introduced error handling: https://www.apollographql.com/docs/apollo-server/features/errors.html



      Okey nice ! As is written in article (posted above) apollo provide rich error feedback in response (errors:)



      but
      when I'm in catch block in my mutation and I throw on of provided apollo-error let say :



       import { ApolloError } from "apollo-server";
      .....
      catch(createCustomerError) {
      throw new ApolloError(serverResponse);
      }


      Mutation response is part of the array errors: provided by apollo and finally I fell into the catch block on client side but inside of the block I get Cannot read property 'data' of undefined. So I'm not able to access to data in catch block.



      here is the response:



      enter image description here



      Is there any solution how I could solve it ?



      Thanks for any help.










      share|improve this question













      I'm trying to solve how is the best way to create an error response on apollo-server side and handle it in appropriate way on client (apollo).



      I was googling a lot but didn't get satisfactory answer.



      So here is my actual implementation:



      I created an interface and then new type MutationResponse (This is my own implementation of mutation response below).



        interface IMutationResponse {
      code: Int!
      path: String!
      fields: String!
      message: String!
      success: Boolean!
      typeError: String!
      }



      type MutationResponse implements IMutationResponse {
      code: Int!
      path: String!
      fields: String!
      message: String!
      success: Boolean!
      typeError: String!
      }


      I created another type where Im using MutationResponse:



       type CustomerResponse {
      customer: Customer
      photoFile: File
      serverResponse: [MutationResponse]
      }


      This is my simplified implementation of mutation (createCustomer):



        Mutation: {
      createCustomer: async (_, { photoFile, customer }) => {
      try {
      const {
      dataValues: { customerID, firstname, lastname, email, phone }
      } = await models.Customer.create(customer);
      const customerUniqueDir = path.join(customerPath,
      `${customerID}`);
      } catch (createCustomerError) {
      console.log("createCustomerError", createCustomerError);
      const serverResponse = createValidationErrorResponse(
      400,
      createCustomerError
      );
      // throw new ApolloError({ serverResponse });
      return {
      serverResponse
      };
      }
      },


      creating mutation response on apollo-server:




      1. First scenario


      **If I'm in the catch block, server return serverResponse object **
      ...



       catch(customerPhotoError) {
      const serverResponse = createValidationErrorResponse(
      400,
      createCustomerError
      );
      return {
      serverResponse
      };
      }


      but on the apollo client I fall into the try block so it looks like that server didn't throw any exception and response was successful (200).
      There is an option to parse the response and handle it in appropriate way (f.e. according to received status code in response).



      Is this right solution ? It's good way how to thrown error on server and it's normal that you are handling this error in try block (or in then callback if you are not using async await) on client ?




      1. scenario


      Apollo server 2 introduced error handling: https://www.apollographql.com/docs/apollo-server/features/errors.html



      Okey nice ! As is written in article (posted above) apollo provide rich error feedback in response (errors:)



      but
      when I'm in catch block in my mutation and I throw on of provided apollo-error let say :



       import { ApolloError } from "apollo-server";
      .....
      catch(createCustomerError) {
      throw new ApolloError(serverResponse);
      }


      Mutation response is part of the array errors: provided by apollo and finally I fell into the catch block on client side but inside of the block I get Cannot read property 'data' of undefined. So I'm not able to access to data in catch block.



      here is the response:



      enter image description here



      Is there any solution how I could solve it ?



      Thanks for any help.







      reactjs apollo-client apollo-server






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Morty

      53211




      53211





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343707%2fhandling-apollo-server-mutation-error-with-apollo-client%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343707%2fhandling-apollo-server-mutation-error-with-apollo-client%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]