apollo-client icon indicating copy to clipboard operation
apollo-client copied to clipboard

Unsure how to resolve `Missing field while writing result` error when using subscriptions

Open blehoux17 opened this issue 4 years ago • 71 comments

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!

blehoux17 avatar Aug 20 '21 17:08 blehoux17

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:

Screenshot 2021-08-30 at 02 09 08
"@apollo/client": "^3.4.8",
"@vue/apollo-composable": "^4.0.0-alpha.14",
"@vue/apollo-util": "^4.0.0-alpha.14",

masterkain avatar Aug 30 '21 00:08 masterkain

also having this issue since upgrading from 3.4-beta-0 to 3.4.10

hsavit1 avatar Sep 03 '21 15:09 hsavit1

i have the same issue, using "@apollo/client": "^3.3.18" "react-native": "0.64.0",

Angulo66 avatar Sep 07 '21 19:09 Angulo66

@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.

benjamn avatar Sep 09 '21 21:09 benjamn

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",

rebz avatar Sep 21 '21 13:09 rebz

Also getting this error now (after upgrading) while executing cache.writeQuery (but the cache seems to be properly updated). I'm not using subscriptions.

mvaivre avatar Sep 21 '21 14:09 mvaivre

Also getting the error using useQuery and MockedProvider

"@apollo/client": "^3.4.13"

camilo-perilla-globant avatar Sep 21 '21 17:09 camilo-perilla-globant

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.

Venryx avatar Sep 23 '21 09:09 Venryx

Also getting the error using useQuery and MockedProvider

"@apollo/client": "^3.4.13"

Same.

xprazak2 avatar Sep 24 '21 10:09 xprazak2

same

glekner avatar Oct 02 '21 20:10 glekner

Hitting this too since we upgraded to "@apollo/client": "^3.4.9"

jamesopti avatar Oct 02 '21 20:10 jamesopti

same using "@apollo/client": "^3.3.12"

KranzAklilu avatar Oct 10 '21 14:10 KranzAklilu

+1 - "@apollo/client": "^3.4.10"

hatched-danny avatar Oct 12 '21 20:10 hatched-danny

+1 same

sahanatroam avatar Oct 13 '21 00:10 sahanatroam

Also getting the error using useQuery and MockedProvider

"@apollo/client": "^3.4.13"

same issue

sahanatroam avatar Oct 13 '21 05:10 sahanatroam

Running into this on "@apollo/client": "^3.4.16"

mheimark avatar Oct 21 '21 22:10 mheimark

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.

aquelehugo avatar Oct 21 '21 23:10 aquelehugo

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!

fauxparse avatar Oct 27 '21 03:10 fauxparse

I think I can make the error go away by making the field optional in my Subscription type:

Today it is not working 😔

fauxparse avatar Oct 27 '21 21:10 fauxparse

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 ....

vpaul18 avatar Oct 30 '21 14:10 vpaul18

The same issue - "@apollo/client": "^3.4.16",

IShinkarenko avatar Oct 31 '21 12:10 IShinkarenko

Also getting the error using useQuery and MockedProvider

"@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?

jcook-gresham avatar Nov 10 '21 17:11 jcook-gresham

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

fvalles avatar Nov 29 '21 19:11 fvalles

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.

Oliver-ke avatar Dec 13 '21 01:12 Oliver-ke

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;
        }

efstathiosntonas avatar Jan 13 '22 08:01 efstathiosntonas

+1 same

vicky-holcomb avatar Feb 11 '22 16:02 vicky-holcomb

+1 same "@apollo/client": "^3.3.21",

yahuio avatar Feb 15 '22 01:02 yahuio

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.

JDMathew avatar Feb 16 '22 08:02 JDMathew

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 :)

viveknadig282 avatar Feb 27 '22 18:02 viveknadig282

Missing field 'me' while writing result {} i have same issue "@apollo/client": "^3.5.8",

faerylay avatar Mar 09 '22 12:03 faerylay