简体   繁体   中英

AWS Amplify GraphQL mutation is not updating a boolean field

I'm playing with the AWS Amplify GraphQL console and I've noticed that updating a boolean field is not working and I'm not sure exactly why. This is also not working from my React Native app. Basically, what I'm trying to do, is at onPress, change the isOnline to true or false (see full code below):

isOnline: !car.isOnline

schema.graphql

type Car @model 
@key(name: "byUser", fields: ["userId"]) 
{
  id: ID!
  type: String!
  latitude: Float
  longitude: Float
  heading: Float

  plate: String

  isOnline: Boolean
  isActive: Boolean

  orders: [Order] @connection(keyName: "byCar", fields: ["id"])

  userId: ID!
  user: User @connection(fields: ["userId"])
}

mutations.js

export const updateCar = /* GraphQL */ `
  mutation UpdateCar(
    $input: UpdateCarInput!
    $condition: ModelCarConditionInput
  ) {
    updateCar(input: $input, condition: $condition) {
      id
      type
      latitude
      longitude
      heading
      plate
      isOnline
      isActive
      userId
      _version
      _deleted
      _lastChangedAt
      createdAt
      updatedAt
    }
  }
`;

index.js

try {
  const userData = await Auth.currentAuthenticatedUser();
            
  const input = {
    id: userData.attributes.sub,
    isOnline: !car.isOnline,
  }

  const updatedCarData = await API.graphql(
     graphqlOperation(
        updateCar, { 
          input 
        })
  )

  console.log("Updated car: ", updatedCarData.data.updateCar)
            
  setCar(updatedCarData.data.updateCar);
 } catch (e) {
   console.error(e);
 }

From the app, every time I get the isOnline field set to true, even if I tried setting it to false instead of !car.isOnline. I also tried creating a new field called isActive which was null initially, I created a mutation on AWS AppSync GraphQL console and was able to set it to false, but then, when trying to set it to true, updating is not working, it's always returning false.

As a note, updating the other fields, for example String fields, is working.

Can you please guide me into this issue?

I was able to solve this issue by:

Running the below command:

amplify update api

Select GraphQL , then Disable Conflict Detection

Last, but not least, run:

amplify push

I haven't found any explanation for this on the Amplify Docs, but if you're running into this issue, make sure to follow the above steps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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