apollo-client
apollo-client copied to clipboard
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!
yeah I can confirm I have the same issue on initial page subscription, using Vue + ActionCable
subscribeToMore(() => ({
document: USER_APP_CREATED,
variables: {
gravatarSize: 24,
},
updateQuery: (oldData, { subscriptionData }) => {
if (Object.keys(subscriptionData.data).length === 0) {
return { ...oldData };
}
stacktrace:
"@apollo/client": "^3.4.8",
"@vue/apollo-composable": "^4.0.0-alpha.14",
"@vue/apollo-util": "^4.0.0-alpha.14",
also having this issue since upgrading from 3.4-beta-0 to 3.4.10
i have the same issue, using "@apollo/client": "^3.3.18" "react-native": "0.64.0",
@blehoux17 I agree this is not an actionable or useful warning (in this particular case), and we should find a way to hide it.
I'm collecting various subscription-related issues using this label, in case you have any others you think deserve a(nother) look. Hopefully this issue will get fixed in the process, but I've labeled it to be sure.
I too am seeing this error surface after upgrading this morning...
Original versions.
"@apollo/client": "^3.2.1",
"@vue/apollo-composable": "^4.0.0-alpha.14",
Upgraded versions.
"@apollo/client": "^3.4.13",
"@vue/apollo-composable": "^4.0.0-alpha.15",
Also getting this error now (after upgrading) while executing cache.writeQuery (but the cache seems to be properly updated). I'm not using subscriptions.
Also getting the error using useQuery and MockedProvider
"@apollo/client": "^3.4.13"
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:
apolloClient = new ApolloClient({
[...]
cache: new InMemoryCache({
typePolicies: {
// temp fix
MyClass: {
fields: {
missingField1(rawVal: boolean, {args}) { return rawVal ?? null; },
missingField2(rawVal: string, {args}) { return rawVal ?? null; },
},
},
},
}),
});
EDIT2: Nevermind, that seems not to have fixed it -- at least not reliably.
Also getting the error using
useQueryandMockedProvider"@apollo/client": "^3.4.13"
Same.
same
Hitting this too since we upgraded to "@apollo/client": "^3.4.9"
same using "@apollo/client": "^3.3.12"
+1 - "@apollo/client": "^3.4.10"
+1 same
Also getting the error using
useQueryandMockedProvider"@apollo/client": "^3.4.13"
same issue
Running into this on "@apollo/client": "^3.4.16"
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.
I think I can make the error go away by making the field optional in my Subscription type:
type Subscription {
"""
this field shows the error
"""
joinedSession(sessionId: ID!): JoinedSessionPayload!
"""
this one doesn't
"""
leftSession(sessionId: ID!): LeftSessionPayload
}
In my case, I'm using graphql-ruby, so the solution was adding null: true to the field in my SubscriptionType class.
I do still get an onSubscriptionData fire with the initial (empty) payload, but as long as I check that I'm golden.
HTH!
I think I can make the error go away by making the field optional in my
Subscriptiontype:
Today it is not working 😔
So what's a fix to this problem ? I don't know if you experience the same behaviour, but my subscription doesn't subscribe because of this , which is quite a major problem ....
The same issue - "@apollo/client": "^3.4.16",
Also getting the error using
useQueryandMockedProvider"@apollo/client": "^3.4.13"
It seems like there is no ability to have an empty subscription mock, for when we want to only test a Query.
If the subscription isn't included in the mocks prop provided to MockedProvider, I get:
No more mocked responses for the query: subscription
If the subscription is included but with no data. something like this:
{
request: {
query: SUBSCRIPTION_,
variables: {},
},
result: {
data: undefined,
},
},
Then Apollo will complain with:
Missing field while writing result
Can we get someway of ignoring subscriptions or providing a empty mock, to fix this error?
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:
console.error
Missing field 'name' while writing result {
"id": "1",
"colorHex": "white"
}
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:
interface Category {
id: string;
name: string;
colorHex: string;
}
Solution: Make the property optional, or add a mocked value for the mocked object
Hi everyone still facing this issue after upgrading @apollo/client to 3.5.6,
Missing field 'id' while writing result {
"property": value,
....
If anyone has a working fix, please kindly share.
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 {}
updateQuery: (prev, { subscriptionData }) => {
if (Object.keys(prev || {}).length === 0) {
// there's nothing in cache, return empty array otherwise crash!
return { chat_channel: [] }; <---------- instead of return prev;
}
+1 same
+1 same "@apollo/client": "^3.3.21",
The last working version for me was @apollo/client": "^3.3.11.
This is a real problem as one may not have control over the graphql server they are connecting to.
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 :)
Missing field 'me' while writing result {} i have same issue "@apollo/client": "^3.5.8",