ApolloGraphQL -- ReadQuery `Field Not Found` Error?

675 views Asked by At

My readQuery is returning a field not found error, even though it seems like the field is present in the cache.

QUERY

const GETIMSFROMCACHE_QUERY = gql`
query getIMsFromCache($fromID: String!){
  instant_message(fromID:$fromID){
    id,
    fromID,
    toID,
    msgText
  }
}  `;

CACHE RESOLVER

client.cache.cacheResolvers = {
    Query: {
        instant_message: (_, args) => toIdValue(client.dataIdFromObject({
            __typename: 'instant_message',
            id: args.id
        })),
    },
};

READQUERY CALL, IN UPDATE

let instant_message = cache.readQuery({ query: GETIMSFROMCACHE_QUERY, variables: {"fromID": fromID} });

ERROR

Error: Can't find field instant_message({"fromID":"ayqNLcA7c6r8vip3i"}) on object (ROOT_QUERY) {
  "getMyUserData({\"id\":\"ayqNLcA7c6r8vip3i\"})": [
    {
      "type": "id",
      "generated": false,
      "id": "MyUserData:ayqNLcA7c6r8vip3i",
      "typename": "MyUserData"
    }
  ],
  "getMyUserData({\"id\":\"9W95z8A7Y6i34buk7\"})": [
    {
      "type": "id",
      "generated": false,
      "id": "MyUserData:9W95z8A7Y6i34buk7",
      "typename": "MyUserData"
    }
  ],
  "Appts({\"originatingUserID\":\"ayqNLcA7c6r8vip3i\"})": [],
  "instant_message({\"fromID\":\"ayqNLcA7c6r8vip3i\",\"toID\":\"9W95z8A7Y6i34buk7\"})": [
    {
      "type": "id",
      "generated": false,
      "id": "instant_message:126",
      "typename": "instant_message"
    },
    {
      "type": "id",
      "generated": false,
      "id": "instant_message:127",
      "typename": "instant_message"
    },
    {
      "type": "id",
      "generated": false,
      "id": "instant_message:128",
      "typename": "instant_message"
    }
  ]
}.

Looking at the error message, there seems to be a instant_message object present on the ROOT_QUERY object with the target user id, but I'm getting this error.

How can I correct this?

Thanks in advance to all for any info.

1

There are 1 answers

0
VikR On BEST ANSWER

Solved! This was tricky because the regular resolver for the original query, brings back any IM that is to or from either of the two users. The resolver returns any instant_messages that are from fromID and to toID, or vice-versa.

So I thought I needed some sort of cache resolver to repeat this when querying the cache.

Eventually I realized that the cache doesn't care what happened in the resolver -- it's going to store the instant_message objects as being from fromID and to toID, regardless of what happened in the resolver.

Once I realized that, I dropped the special cache resolver query, and just used the original query that retrieved the instant_messages in the first place, with the same query variables used with it in the first place, and it worked. :)