Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutation component in react native doesn't show errors returned by server #3336

Closed
bjagadeesan opened this issue Apr 17, 2018 · 5 comments
Closed

Comments

@bjagadeesan
Copy link

bjagadeesan commented Apr 17, 2018

Hello,
I'm using the new mutation component that is released recently with Apollo 2.0 and react native. I have a custom GraphQL API which servers some data, it throws apollo errors for not authorized, validation or type mismatches. I can't capture apollo errors using error variable in mutation component (network errors can be captured but not Apollo errors returned from mutation). I use the same approach for Query component and apollo errors can be captured.
Mutation:

mutation createDeceasedPerson(
    $firstName: String!
    $lastName: String!
    $profilePic: String
    $dateOfBirth: Date
    $dateOfDeath: Date
    $address: String
    $city: String
    $state: String
    $country: String
    $zipcode: String
    $description: String
    $addedBy: String!
    $editorNotes: String!
){
  createDeceasedPerson(data:{
    firstName:$firstName,
    lastName: $lastName,
    profilePic:$profilePic,
    dateOfBirth: $dateOfBirth,
    dateOfDeath: $dateOfDeath,
    address:$address,
    city: $city,
    state: $state,
    country:$country,
    zipcode:$zipcode,
    description: $description,
    addedBy: $addedBy,
    editorNotes: $editorNotes,
  }){
  	id
   }
}

input:
{
  "firstName":"Test",
"lastName":"Person",
"profilePic":null,
"dateOfBirth":"2018-04-03",
"dateOfDeath":"2017-12-12",
"address":null,
"city":null,
"state":"Ny",
"country":null,
"zipcode":null,
"description":null,
"addedBy":"test - test@gmail.com",
"editorNotes":"Created a person entry"
}
Header:
{
    Authorization: "Bearer <accessToken>"
}

Mutation component:
image

Intended outcome:
Some kind of Apollo error should be shown in Mutation component.
This is the outcome of the above mutation in GraphQL playground

{
  "data": {
    "createDeceasedPerson": null
  },
  "errors": [
    {
      "message": "Some error in validation (see details for more info)",
      "name": "ValidationError",
      "time_thrown": "2018-04-17T19:02:13.160Z",
      "data": {
        "errorID": "BkT3ETQ3M",
        "errorCode": 1550,
        "errorMessage": [
          "dateOfBirth is greater than dateOfDeath"
        ]
      }
    }
  ]
}

I was trying to create a person entry.If successfully added to db, id will be returned or else some kind of Apollo error will be returned.

Actual outcome:
No Apollo error is shown in the component. It shows if there is any network error (randomly created by turning off network) but not Apollo errors inside the request.

image

Note: I use query component similar to this and I get the Apollo errors.

How to reproduce the issue:

Version

@bjagadeesan bjagadeesan changed the title New Mutation component doesn't show errors returned by server Mutation component in react native doesn't show errors returned by server Apr 17, 2018
@bjagadeesan
Copy link
Author

Sorry for the trouble, please close this issue. The options in my ApolloClient() setup was set to ignore errors in mutation.

@deftomat
Copy link

deftomat commented May 22, 2018

@BalajiJagadeesan How did you solve this? I tried to set a default errorPolicy for mutations and still no errors. Which version of react-apollo do you using?

@bjagadeesan
Copy link
Author

@deftomat Sorry for the delay. I have removed the default options for mutation.

While initializing the Apollo client with cache, I passed default options to set policy for the query and mutation
Previously it was like this.

const defaultOptions = {
        watchQuery: {
            fetchPolicy: 'cache-and-network',
            errorPolicy: 'all',
        },
        query: {
             fetchPolicy: 'cache-and-network',
             errorPolicy: 'all',
         },
         mutate: {
             errorPolicy: 'all',
         },
    };

    return new ApolloClient({
        link: authLink.concat(httpLink),
        cache,
        defaultOptions
    });

Now I commented out the query and mutate

const defaultOptions = {
        watchQuery: {
            fetchPolicy: 'cache-and-network',
            errorPolicy: 'all',
        },
        // query: {
        //     fetchPolicy: 'cache-and-network',
        //     errorPolicy: 'all',
        // },
        // mutate: {
        //     errorPolicy: 'all',
        // },
    };

    return new ApolloClient({
        link: authLink.concat(httpLink),
        cache,
        defaultOptions
    });

To capture graphQL errors, you need to treat them as runtime errors which can be defined by setting error policy to none (This is the default behavior, so don't send any flags in options).

If you set it to errorpolicy:"all", it ignores query errors but captures network errors (According to my testing with the issue). Not entirely sure (needs clarification).

Hope this helps.

@deftomat
Copy link

Hi @BalajiJagadeesan.

Thanks for your response. I've find out that my issue was casued by an Offline Link in AWS AppSync client which is based on Apollo Client.

Your solution works for a base Apollo Client.

@Austint30
Copy link

Hi @BalajiJagadeesan.

Thanks for your response. I've find out that my issue was casued by an Offline Link in AWS AppSync client which is based on Apollo Client.

Your solution works for a base Apollo Client.

I am also using the AWSAppSyncClient and I am having a similar issue where my mutations don't return any errors (or any data for that matter). Were you able to solve the issue with AWS Appsync? I'm banging my head on my laptop in frustration looking for a solution.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants