mobxjs / mst-gql

Bindings for mobx-state-tree and GraphQL
MIT License
680 stars 81 forks source link

[mobx.array] Index out of bounds #289

Closed Tojteusz closed 3 years ago

Tojteusz commented 3 years ago

Hi, I am looking for some solution on how to deal with this error Error: [mobx.array] Index out of bounds, KFZRSDF is larger than 0 I used this schema

directive @specifiedBy(url: String!) on SCALAR
type Address {
  id: ID
  firstName: String
  lastName: String
  street: String
  houseNumber: String
  postcode: String
  city: String
  cityPart: String
  locationId: String
}

type Order {
  id: ID
  status: OrderStatus
}

type OrderStatus {
  colorCode: String
  text: String
}

type Query {
  orders: [Order]
  order(id: ID): Order
  addresses: [Address]
  address(id: ID): Address
}

and perform this command npm run mst-gql --format ts --outDir app/models/ schema.graphql

and i wrote very similar queries in two different components

export const OrdersScreen: Component = observer(function OrdersScreen() {
  AsyncStorage.clear()
  const { store, loading, data, error } = useQuery(stores =>
    stores.queryOrders({},
      selectFromOrder()
        .status(status => status.text.colorCode)
        .toString(),
    ),
  )
  console.log(" Loading: ", loading)
  console.log(" Error: ", error)
  if (data) console.log(" Data: ", data)
...
export const AccountScreen: Component = observer(function AccountScreen() {
  const { store, loading, data, error } = useQuery(stores =>
    stores.queryAddresses(
      {},
      selectFromAddress().firstName.lastName.street.toString(),
    ),
  )

  console.log(" Loading: ", loading)
  console.log(" Error: ", error)
  if (data) console.log(" Data: ", data)
...

when I execute both queries I got the correct response from the server


{
  "data": {
    "addresses": [
      {
        "id": "a2P2o000001PbunEAC",
        "firstName": "Test",
        "lastName": "Tester",
        "street": "Adlerstr."
      },
      {
        "id": "a2P2o000002IwXdEAK",
        "firstName": "Test",
        "lastName": "Tester",
        "street": "Adlerstr."
      }
    ]
  }
}

{
  "data": {
    "orders": [
      {
        "id": "NFZ-4324234",
        "status": {
          "colorCode": "PENDING",
          "text": "order.status.inProgress"
        }
      },
      {
        "id": "NFZ-3323514",
        "status": {
          "colorCode": "PENDING",
          "text": "order.status.inProgress"
        }
      },
      {
        "id": "HW-23434",
        "status": {
          "colorCode": "PENDING",
          "text": "order.status.inProgress"
        }
      }
    ]
  }
}

But consol.log for error from orders show Error: [mobx.array] Index out of bounds, NFZ-4324234 is larger than 0 and data is undefined but addresses work fine

Do you ever meet this case or do you know how to deal with it?

Tojteusz commented 3 years ago

That was a mistake on our side. We change type of props in RootModel