1---
2title: Errors
3order: 8
4---
5
6# Help!
7
8**This document lists out all errors and warnings in `@urql/exchange-graphcache`.**
9
10Any unexpected behaviour or condition will be marked by an error or warning
11in development. This will output as a helpful little message. Sometimes, however, this
12message may not actually tell you about everything that's going on.
13
14This is a supporting document that explains every error and attempts to give more
15information on how you may be able to fix some issues or avoid these errors/warnings.
16
17## (1) Invalid GraphQL document
18
19> Invalid GraphQL document: All GraphQL documents must contain an OperationDefinition
20> node for a query, subscription or mutation.
21
22There are multiple places where you're passing in GraphQL documents, either through
23methods on `Cache` (e.g. `cache.updateQuery`) or via `urql` using the `Client` or
24hooks like `useQuery`.
25
26Your queries must always contain a main operation, one of: query, mutation, or
27subscription. This error occurs when this is missing, because the `DocumentNode`
28is maybe empty or only contains fragments.
29
30## (2) Invalid Cache call
31
32> Invalid Cache call: The cache may only be accessed or mutated during
33> operations like write or query, or as part of its resolvers, updaters,
34> or optimistic configs.
35
36If you're somehow accessing the `Cache` (an instance of `Store`) outside any
37of the usual operations then this error will be thrown.
38
39Please make sure that you're only calling methods on the `cache` as part of
40configs that you pass to your `cacheExchange`. Outside these functions the cache
41must not be changed.
42
43However when you're not using the `cacheExchange` and are trying to use the
44`Store` on its own, then you may run into issues where its global state wasn't
45initialised correctly.
46
47This is a safe-guard to prevent any asynchronous work to take place, or to
48avoid mutating the cache outside any normal operation.
49
50## (3) Invalid Object type
51
52> Invalid Object type: The type `???` is not an object in the defined schema,
53> but the GraphQL document is traversing it.
54
55When you're passing an introspected schema to the cache exchange, it is
56able to check whether all your queries are valid.
57This error occurs when an unknown type is found as part of a query or
58fragment.
59
60Check whether your schema is up-to-date or whether you're using an invalid
61typename somewhere, maybe due to a typo.
62
63## (4) Invalid field
64
65> Invalid field: The field `???` does not exist on `???`,
66> but the GraphQL document expects it to exist.<br />
67> Traversal will continue, however this may lead to undefined behavior!
68
69Similarly to the previous warning, when you're passing an introspected
70schema to the cache exchange, it is able to check whether all your queries are valid.
71This warning occurs when an unknown field is found on a selection set as part
72of a query or fragment.
73
74Check whether your schema is up-to-date or whether you're using an invalid
75field somewhere, maybe due to a typo.
76
77As the warning states, this won't lead any operation to abort, or an error
78to be thrown!
79
80## (5) Invalid Abstract type
81
82> Invalid Abstract type: The type `???` is not an Interface or Union type
83> in the defined schema, but a fragment in the GraphQL document is using it
84> as a type condition.
85
86When you're passing an introspected schema to the cache exchange, it becomes
87able to deterministically check whether an entity in the cache matches a fragment's
88type condition.
89
90This applies to full fragments (`fragment _ on Interface`) or inline fragments
91(`... on Interface`), that apply to interfaces instead of to a concrete object typename.
92
93Check whether your schema is up-to-date or whether you're using an invalid
94field somewhere, maybe due to a typo.
95
96## (6) readFragment(...) was called with an empty fragment
97
98> readFragment(...) was called with an empty fragment.
99> You have to call it with at least one fragment in your GraphQL document.
100
101You probably have called `cache.readFragment` with a GraphQL
102document that doesn't contain a main fragment.
103
104This error occurs when no main fragment can be found, because the `DocumentNode`
105is maybe empty or does not contain fragments.
106
107When you're calling a fragment method, please ensure that you're only passing fragments
108in your GraphQL document. The first fragment will be used to start writing data.
109
110This also occurs when you pass in a `fragmentName` but a fragment with the given name
111can't be found in the `DocumentNode`.
112
113## (7) Can't generate a key for readFragment(...)
114
115> Can't generate a key for readFragment(...).
116> You have to pass an `id` or `_id` field or create a custom `keys` config for `???`.
117
118You probably have called `cache.readFragment` with data that the cache can't generate a
119key for.
120
121This may either happen because you're missing the `id` or `_id` field or some other
122fields for your custom `keys` config.
123
124Please make sure that you include enough properties on your data so that `readFragment`
125can generate a key.
126
127## (8) Invalid resolver data
128
129> Invalid resolver value: The resolver at `???` returned an invalid typename that
130> could not be reconciled with the cache.
131
132This error may occur when you provide a cache resolver for a field using `resolvers` config.
133
134The value that you returns needs to contain a `__typename` field and this field must
135match the `__typename` field that exists in the cache, if any. This is because it's not
136possible to return a different type for a single field.
137
138Please check your schema for the type that your resolver has to return, then add a
139`__typename` field to your returned resolver value that matches this type.
140
141## (9) Invalid resolver value
142
143> Invalid resolver value: The field at `???` is a scalar (number, boolean, etc),
144> but the GraphQL query expects a selection set for this field.
145
146The GraphQL query that has been walked contains a selection set at the place where
147your resolver is located.
148
149This means that a full entity object needs to be returned, but instead the cache
150received a number, boolean, or another scalar from your resolver.
151
152Please check that your resolvers return scalars where there's no selection set,
153and entities where there is one.
154
155## (10) writeOptimistic(...) was called with an operation that isn't a mutation
156
157> writeOptimistic(...) was called with an operation that is not a mutation.
158> This case is unsupported and should never occur.
159
160This should never happen, please open an issue if it does. This occurs when `writeOptimistic`
161attempts to write an optimistic result for a query or subscription, instead of a mutation.
162
163## (11) writeFragment(...) was called with an empty fragment
164
165> writeFragment(...) was called with an empty fragment.
166> You have to call it with at least one fragment in your GraphQL document.
167
168You probably have called `cache.writeFragment` with a GraphQL
169document that doesn't contain a main fragment.
170
171This error occurs when no main fragment can be found, because the `DocumentNode`
172is maybe empty or does not contain fragments.
173
174When you're calling a fragment method, please ensure that you're only passing fragments
175in your GraphQL document. The first fragment will be used to start writing data.
176
177This also occurs when you pass in a `fragmentName` but a fragment with the given name
178can't be found in the `DocumentNode`.
179
180## (12) Can't generate a key for writeFragment(...) or link(...)
181
182> Can't generate a key for writeFragment(...) [or link(...) data.
183> You have to pass an `id` or `_id` field or create a custom `keys` config for `???`.
184
185You probably have called `cache.writeFragment` or `cache.link` with data that the cache
186can't generate a key for.
187
188This may either happen because you're missing the `id` or `_id` field or some other
189fields for your custom `keys` config.
190
191Please make sure that you include enough properties on your data so that `writeFragment`
192or `cache.link` can generate a key. On `cache.link` the entities must either be
193an existing entity key, or a keyable entity.
194
195## (13) Invalid undefined
196
197> Invalid undefined: The field at `???` is `undefined`, but the GraphQL query expects a
198> scalar (number, boolean, etc) / selection set for this field.
199
200As data is written to the cache, this warning is issued when `undefined` is encountered.
201GraphQL results should never contain an `undefined` value, so this warning will let you
202know the part of your result that did contain `undefined`.
203
204## (14) Couldn't find \_\_typename when writing.
205
206> Couldn't find `__typename` when writing.
207> If you're writing to the cache manually have to pass a `__typename` property on each entity in your data.
208
209You probably have called `cache.writeFragment` or `cache.updateQuery` with data that is missing a
210`__typename` field for an entity where your document contains a selection set. The cache won't be
211able to generate a key for entities that are missing the `__typename` field.
212
213Please make sure that you include enough properties on your data so that `write` can generate a key.
214
215## (15) Invalid key
216
217> Invalid key: The GraphQL query at the field at `???` has a selection set,
218> but no key could be generated for the data at this field.
219> You have to request `id` or `_id` fields for all selection sets or create a
220> custom `keys` config for `???`.
221> Entities without keys will be embedded directly on the parent entity.
222> If this is intentional, create a `keys` config for `???` that always returns null.
223
224This error occurs when the cache can't generate a key for an entity. The key
225would then effectively be `null`, and the entity won't be cached by a key.
226
227Conceptually this means that an entity won't be normalized but will indeed
228be cached by the parent's key and field, which is displayed in the first
229part of the warning.
230
231This may mean that you forgot to include an `id` or `_id` field.
232
233But if your entity at that place doesn't have any `id` fields, then you may
234have to create a custom `keys` config. This `keys` function either needs to
235return a unique ID for your entity, or it needs to explicitly return `null` to silence
236this warning.
237
238## (16) Heuristic Fragment Matching
239
240> Heuristic Fragment Matching: A fragment is trying to match against the `???` type,
241> but the type condition is `???`. Since GraphQL allows for interfaces `???` may be
242> an interface.
243> A schema needs to be defined for this match to be deterministic, otherwise
244> the fragment will be matched heuristically!
245
246This warning is issued on fragment matching. Fragment matching is the process
247of matching a fragment against a piece of data in the cache and that data's `__typename`
248field.
249
250When the `__typename` field doesn't match the fragment's type, then we may be
251dealing with an interface and/or enum. In such a case the fragment may _still match_
252if it's referring to an interface (`... on Interface`). Graphcache is supposed to be
253usable without much config, so what it does in this case is apply a heuristic match.
254
255In a heuristic fragment match we check whether all fields on the fragment are present
256in the cache, which is then treated as a fragment match.
257
258When you pass an introspected schema to the cache, this warning will never be displayed
259as the cache can then do deterministic fragment matching using schema information.
260
261## (17) Invalid type
262
263> Invalid type: The type `???` is used with @populate but does not exist.
264
265When you're using the populate exchange with an introspected schema and add the
266`@populate` directive to fields it first checks whether the type is valid and
267exists on the schema.
268
269If the field does not have enough type information because it doesn't exist
270on the schema or does not match expectations then this warning is logged.
271
272Check whether your schema is up-to-date or whether you're using an invalid
273field somewhere, maybe due to a typo.
274
275## (18) Invalid TypeInfo state
276
277> Invalid TypeInfo state: Found no flat schema type when one was expected.
278
279When you're using the populate exchange with an introspected schema, it will
280start collecting used fragments and selection sets on all of your queries.
281This error may occur if it hits unexpected types or inexistent types when doing so.
282
283Check whether your schema is up-to-date or whether you're using an invalid
284field somewhere, maybe due to a typo.
285
286Please open an issue if it happens on a query that you expect to be supported
287by the `populateExchange`.
288
289## (19) Can't generate a key for invalidate(...)
290
291> Can't generate a key for invalidate(...).
292> You need to pass in a valid key (**typename:id) or an object with the "**typename" property and an "id" or "\_id" property.
293
294You probably have called `cache.invalidate` with data that the cache can't generate a key for.
295
296This may either happen because you're missing the `__typename` and `id` or `_id` field or if the last two
297aren't applicable to this entity a custom `keys` entry.
298
299## (20) Invalid Object type
300
301> Invalid Object type: The type `???` is not an object in the defined schema,
302> but the `keys` option is referencing it.
303
304When you're passing an introspected schema to the cache exchange, it is
305able to check whether your `opts.keys` is valid.
306This error occurs when an unknown type is found in `opts.keys`.
307
308Check whether your schema is up-to-date, or whether you're using an invalid
309typename in `opts.keys`, maybe due to a typo.
310
311## (21) Invalid updates type
312
313> Invalid updates field: The type `???` is not an object in the defined schema,
314> but the `updates` config is referencing it.
315
316When you're passing an introspected schema to the cache exchange, it is
317able to check whether your `opts.updates` config is valid.
318This error occurs when an unknown type is found in the `opts.updates` config.
319
320Check whether your schema is up-to-date, or whether you've got a typo in `opts.updates`.
321
322## (22) Invalid updates field
323
324> Invalid updates field: `???` on `???` is not in the defined schema,
325> but the `updates` config is referencing it.
326
327When you're passing an introspected schema to the cache exchange, it is
328able to check whether your `opts.updates` config is valid.
329This error occurs when an unknown field is found in `opts.updates[typename]`.
330
331Check whether your schema is up-to-date, or whether you're using an invalid
332field name in `opts.updates`, maybe due to a typo.
333
334## (23) Invalid resolver
335
336> Invalid resolver: `???` is not in the defined schema, but the `resolvers`
337> option is referencing it.
338
339When you're passing an introspected schema to the cache exchange, it is
340able to check whether your `opts.resolvers` is valid.
341This error occurs when an unknown query, type or field is found in `opts.resolvers`.
342
343Check whether your schema is up-to-date, or whether you've got a typo in `opts.resolvers`.
344
345## (24) Invalid optimistic mutation
346
347> Invalid optimistic mutation field: `???` is not a mutation field in the defined schema,
348> but the `optimistic` option is referencing it.
349
350When you're passing an introspected schema to the cache exchange, it is
351able to check whether your `opts.optimistic` is valid.
352This error occurs when a field in `opts.optimistic` is not in the schema's `Mutation` fields.
353
354Check whether your schema is up-to-date, or whether you've got a typo in `Mutation` or `opts.optimistic`.
355
356## (25) Invalid root traversal
357
358> Invalid root traversal: A selection was being read on `???` which is an uncached root type.
359> The `Mutation` and `Subscription` types are special Operation Root Types and cannot be read back
360> from the cache.
361
362In GraphQL every schema has three [Operation Root
363Types](https://spec.graphql.org/June2018/#sec-Root-Operation-Types). The `Query` type is the only
364one that is cached in Graphcache's normalized cache, since it's the root of all normalized cache
365data, i.e. all data is linked and connects back to the `Query` type.
366
367The `Subscription` and `Mutation` types are special and uncached; they may link to entities that
368will be updated in the normalized cache data, but are themselves not cached, since they're never
369directly queried.
370
371When your schema treats `Mutation` or `Subscription` like regular entity types you may get this
372warning. This may happen because you've used the default reserved names `Mutation` or `Subscription`
373for entities rather than as special Operation Root Types, and haven't specified this in the schema.
374Hence this issue can often be fixed by either enabling
375[Schema Awareness](./schema-awareness.md) or by
376adding a `schema` definition to your GraphQL Schema like so:
377
378```graphql
379schema {
380 query: Query
381 mutation: YourMutation
382 subscription: YourSubscription
383}
384```
385
386Where `YourMutation` and `YourSubscription` are your custom Operation Root Types, instead of relying
387on the default names `"Mutation"` and `"Subscription"`.
388
389## (26) Invalid abstract resolver
390
391> Invalid resolver: `???` does not map to a concrete type in the schema,
392> but the resolvers option is referencing it. Implement the resolver for the types that `??` instead.
393
394When you're passing an introspected schema to the cache exchange, it is
395able to check whether your `opts.resolvers` is valid.
396This error occurs when you are using an `interface` or `union` rather than an
397implemented type for these.
398
399Check the type mentioned and change it to one of the specific types.
400
401## (27) Invalid Cache write
402
403> Invalid Cache write: You may not write to the cache during cache reads.
404> Accesses to `cache.writeFragment`, `cache.updateQuery`, and `cache.link` may
405> not be made inside `resolvers` for instance.
406
407If you're using the `Cache` inside your `cacheExchange` config you receive it
408either inside callbacks that are called when the cache is queried (e.g.
409`resolvers`) or when data is written to the cache (e.g. `updates`). You may not
410write to the cache when it's being queried.
411
412Please make sure that you're not calling `cache.updateQuery`,
413`cache.writeFragment`, or `cache.link` inside `resolvers`.
414
415## (28) Resolver and directive match the same field
416
417When you have a resolver defined on a field you shouln't be combining it with a directive as the directive
418will apply and the resolver will be void.