React-apollo: Error: Can't find field ChatRoom({}) on object (ROOT_QUERY)

Created on 22 Jul 2017  路  5Comments  路  Source: apollographql/react-apollo

Can't access store after mutation in update function

query:

const ChatQuery = gql`
  query chatQuery($id: ID!) {
    ChatRoom(id: $id) {
      id
      users {
        username
        id
      }
      chats {
        message
        id
        user {
          username
        }
      }
    }
  }
`

mutation:

const ChatMutation = gql`
  mutation send($message: String!, $userId: ID!, $chatRoomId: ID!) {
    createChat(message: $message, userId: $userId, chatRoomId: $chatRoomId) {
      message
      id
      user {
        username
      }
    }
  }
`

mutation call:

await this.props.mutate({
  update: (store, { data: { createChat } }) => {
    const data = store.readQuery({ query: ChatQuery }) //this throws error Error: Can't find field ChatRoom({}) on object (ROOT_QUERY) 
    data.ChatRoom.chats.push(createChat)
    store.writeQuery({ query: ChatQuery, data })
  },
  ...
})

Version

Most helpful comment

Fixed. I just needed to provide variables in readQuery.

 const data = store.readQuery({ query: ChatQuery, variables: {id: ...} })

Please close.

All 5 comments

Fixed. I just needed to provide variables in readQuery.

 const data = store.readQuery({ query: ChatQuery, variables: {id: ...} })

Please close.

This issue has been automatically labled because it has not had recent activity. If you have not received a response from anyone, please mention the repository maintainer (most likely @jbaxleyiii). It will be closed if no further activity occurs. Thank you for your contributions to React Apollo!

This issue has been automatically closed because it has not had recent activity after being marked as no recent activyt. If you belive this issue is still a problem or should be reopened, please reopen it! Thank you for your contributions to React Apollo!

Fixed. I just needed to provide variables in readQuery.
@c0z0 Thanks! I've been researching this for a while and you solved it! I couldn't find this anywhere in the docs...
A note to others: Include the variables when you're writing back to the cache also. Eg., proxy.writeQuery({ query: ChatQuery, data, variables: { id: ... } });

Fixed. I just needed to provide variables in readQuery.

 const data = store.readQuery({ query: ChatQuery, variables: {id: ...} })

Please close.

So which id we have to provide? it's id of ChatRoom ???

Was this page helpful?
0 / 5 - 0 ratings