apollo-client: Unsure how to resolve `Missing field while writing result` error when using subscriptions
We’ve implement subscriptions in our application using the useSubscription
hook and since upgrading from version 3.3.21 to 3.4.7 we’ve started to see errors around Missing field...
. To be clear we realize these errors have likely always existed, but now they’re being surfaced.
We see the following error Missing field 'domainObject' while writing result {}
on initial page load when the subscription is registered. We believe that this happens because the first response from the subscription is an empty object. The subscriptions themselves work fine, this only seems to be an issue when they’re initially registered. We’d like to resolve this issue and remove the errors from the console, but have so far been unsuccessful.
We tried to update the fetchPolicy
and found that setting it to no-cache
removed the error, but this stopped the cache from being updated at all, which defeats the purpose.
Shown below is a snippet of the code that we’ve implemented. Some of the subscriptions allow apollo to do the cache updating on its own (OnObjectUpdated
) while with others (OnObjectDeleted
) we capture the return data and manually make a change to the cache. In both cases we see the Missing field
error. For the OnObjectDeleted
subscription, we added a guard in our callback function because when the subscription is registered we receive data
as an empty object.
subscription OnObjectDeleted {
objectDeleted {
object {
id
}
}
}
const removeObjectFromCache = ({ subscriptionData: { data } }) => {
if (!data || !data.objectDeleted) return;
removeObject(data.objectDeleted.object.id, client);
};
useSubscription(OnObjectDeleted, { onSubscriptionData: removeObjectFromCache });
useSubscription(OnObjectUpdated);
We’re looking to see if anyone has encountered this and if so how it was resolved.
Thanks!
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 88
- Comments: 69 (4 by maintainers)
Commits related to this issue
- test(editor): fix tests failing While upgrading @apollo/client so that all libraries/dependencies use the same version the tests started to fail. The errors were that they tried to write a field but ... — committed to wepublish/wepublish by Itrulia 2 years ago
- refactor(api-db-mongodb/api/editor): remove ts-ignores (#581) * refactor(api-db-mongodb/api/editor): remove ts-ignores Updating `@types/react` fixes the mismatch between `<root>/node_modules/@type... — committed to wepublish/wepublish by Itrulia 2 years ago
- Release 3.1.0 (#591) * WPC-657: As a publisher, when uploading an image, i want the metadata automatically captured (#545) * Working commit with ts ignores * Added type definiton for metastore ... — committed to wepublish/wepublish by tomaszdurka 2 years ago
It’s been 8 monts and no one that works on apollo has replied? This also happens if mutations have missing fields. Just set them to null they are marked as optionel in the schema
Also getting the error using
useQuery
andMockedProvider
How has nobody from Apollo commented on this post. Am I missing something?
@alessbell adding every field does solve the issue, but it would be super nice to be able to suppress that warning in tests so nullable fields we don’t care about for a test don’t need to be specified
Also getting this error now (after upgrading) while executing
cache.writeQuery
(but the cache seems to be properly updated). I’m not using subscriptions.This error only seemed to appear for us when the data returned from the subscription was formatted in a way that Apollo doesn’t expect.
Example for an
onAccountUpdated
subscription:This is the way some implementations like apollo-server-lambda return subscription data be default, but it’s incorrect, because of two reasons:
accountUpdated
property wrapper__typename
propertyWe had to manually transform the data returned to subscriptions like this:
The underlying issue (for us) is that apollo-server-lambda takes care of subscriptions through a different hander and is not aware of the normal “graph ways” of handling subscriptions and publishes, so the onus is on you to make sure the data is formatted in a “graph way”. It’s not great, but it seems to work.
Hope it helps other as well!
Hi everyone! I faced this issue a few weeks ago. In my case, the error was produced by a missing property in a mocked object used by a test. E.g.:
Error:
Mocked object:
export const mockedObject: ObjectType = { ...otherProps, category: { colorHex: '#745EA8', id: '11', }, };
In this example, the ‘name’ property was missing in the mock used by the test, which has a type of:
Solution: Make the property optional, or add a mocked value for the mocked object
I too am seeing this error surface after upgrading this morning…
Original versions.
Upgraded versions.
Hello. I’m getting this error when mocking query results. Here’s a CodeSandbox example: https://codesandbox.io/s/apollo-client-missing-field-error-srr15m
I had this error appear using
MockedProvider
/storybook-addon-apollo-client
while mocking a specific query. It was complaining aboutMissing field icon...
. In my case the mocked data being returned was in the wrong format:Query:
Mocked data that was erroring:
The fix is to change
result.data.url
withresult.data.icon.url
because that’s the name of the queryI’m still facing this issue with
"@apollo/client": "3.7.0"
, usinguseQuery
with relay style pagination. It’s a bit scary how an undefined value can break the cache and the pagination.Given these two types
I use two fragments for the type
User
The errors appears when the data from the fragment
UserBasicData
is already stored in the cache and the data is refreshed with the data fromUserFullData
.I solved mine by changing the code as above… where sortedMessages is the name of the field saved in the cache I really hope this helps someone
In our case solution was straightforward. When response fields were empty backend sent undefined instead of null due to misconfiguration. That lead to an issue with Apollo cache and missing fields error.
Not sure if it’s the best solution but this eliminated the error:
error is:
ExceptionsManager.js:184 Missing field 'chat_channel' while writing result {}
Maybe it helps someone facing that in tests:
It happened with me because a mocked resolver had an exception inside. In my case, I was accessing a property from the first item of an array inside the resolver and the array was empty. The error in the resolver didn’t show up, but I had that message from Apollo. When I added a validation for the item existence, the error went away.
Getting the same issue with apollo-client
3.5.0-beta.4
.Here is the relevant line in the source code, for those wondering: https://github.com/apollographql/apollo-client/blob/212b1e686359a3489b48d7e5d38a256312f81fde/src/cache/inmemory/writeToStore.ts#L327
Not really sure what I’m supposed to do to resolve the issue. (I know one way to solve it is to make sure the server returns a value for every field that Apollo is aware of; however I don’t want to have to do that, because the field is already marked as optional in the GraphQL defined on the server, so I want Apollo to be compatible with that without needing server-side workarounds)
EDIT: Found a temporary fix:
EDIT2: Nevermind, that seems not to have fixed it – at least not reliably.
@AndreiRailean I had exactly the same issue but I didn’t have to fiddle around with
graphql-ruby
internals.When I left the default
subscribe
method in one of my subscriptions I was getting the error described in this thread (because ofdata = {}
) but then I tried to just override thesubscribe
to returnnil
and apparently that worked.This is now sending
data = null
in the cable connection.Hope that helps people using
graphql-ruby
.We faced a similar issue and upon close investigation, we ended up blaming it on
ActionCable
and how graphql-ruby gem works with it. The main issue is that upon initial subscription, it returnsdata={}
because that’s how empty result gets serialised by.to_h
call in ruby, i.e. it whenresult.data=nil
, and the code runsresult.to_h
, you end up withresult: data: {}
.This appears to have been a deliberate change introduced to better support Relay in 2019 in https://github.com/rmosolgo/graphql-ruby/pull/2536
https://github.com/rmosolgo/graphql-ruby/pull/2536/files#diff-a1ea5af1a78917fa653eff83d9ae72180fabd3d9cf616f14dd1871739963cd73L44
We ended up changing our implementation on the ruby side, which ended up setting
data: null
. This stops Apollo client from erroring because of type mismatch. We didn’t want to manually introduce a type override for each subscription we add. This approach allows us to support initial subscription result if we ever wanted to.And we went from this in our cable stream
To this
And there were no more errors and all subscriptions kept on working as expected.
If you’re running into this error, it may help to consider adjusting the server, rather than waiting for Apollo client to ever change to support this. The issue is really with what restrictions ActionCable places on graphql-ruby for delivering subscriptions. By default, graphql-ruby is conffigured to NOT send a payload for initial subscription, but when it comes to ActionCable channels, it appears unavoidable that something will be sent and in this case it ends up being formatted incorrectly.
cc @rmosolgo
Thanks for sharing @hshoja, this fixed the issue for me when testing with React Testing Library and <MockedProvider />
fixed the error by adding
fetchPolicy: "no-cache"
inuseSubscription
I am running into this issue because i have to use an await within “updateQuery”. As soon as i set “updateQuery” async the issue pop`s up. I could not find any guidance on that. Cant i use async at all in this context?
I get this error when I manually update cache via
writeQuery
.To clarify the use case for maintainers, I have two types:
First I fetch projects with all fields:
Then I manually update cache via
writeQuery
for the following query:I’ll get
Missing field 'name' while writing result
error because the nestedProject
doesn’t containname
field. This in turn prevents my cache updates which causes all kinds of new issues.I hoped I could fix it via
typePolicies
’smerge
but the error happens beforemerge
even called. So for now I have to fetch nested types with all fields for my cache writes.I kiiiinda understand why this is the error (to be explicit about data updates/merge) and not a warning but I would really like to have a global option to make it a warning (or just ignore it completely), so my cache writes would go through and I would still be able to define the behaviour (e.g. use existing value if incoming is missing) in
typePolicies
’smerge
.I have had this issue for a while now and it’s still there in 3.7.0
This error is super misleading. In our case we had defined schema incorrectly. This error occurred when the actual field we were querying for during runtime didnt match up with the schema. Specifically it had to with returning a
string
when the type in the graph schema wasUrl
. Changing toString
in schema resolved the issue.Hey guys, the way that I was able to solve this problem was by first checking if the data was there that I was writing. For me, this happened when I was doing cache.writeQuery, and the way I was able to solve it was just by checking if the data existed with an if statement. Hopefully, that helps 😃