Skip to content

Commit

Permalink
Allow writeFragment to infer ROOT_QUERY based on data.__typename.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Jun 26, 2020
1 parent 877c560 commit 6eb8d13
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/cache/inmemory/__tests__/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import gql from 'graphql-tag';
import {
storeKeyNameFromField,
makeReference,
isReference,
} from '../../../utilities/graphql/storeUtils';
import { addTypenameToDocument } from '../../../utilities/graphql/transform';
import { cloneDeep } from '../../../utilities/common/cloneDeep';
Expand Down Expand Up @@ -2302,4 +2303,26 @@ describe('writing to the store', () => {
},
});
});

it("writeFragment should be able to infer ROOT_QUERY", () => {
const cache = new InMemoryCache;

const ref = cache.writeFragment({
fragment: gql`fragment RootField on Query { field }`,
data: {
__typename: "Query",
field: "value",
},
});

expect(isReference(ref)).toBe(true);
expect(ref!.__ref).toBe("ROOT_QUERY");

expect(cache.extract()).toEqual({
ROOT_QUERY: {
__typename: "Query",
field: "value",
},
});
});
});
5 changes: 5 additions & 0 deletions src/cache/inmemory/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ export class Policies {
? getTypenameFromResult(object, selectionSet, fragmentMap)
: object.__typename;

if (typename) {
const rootId = this.rootIdsByTypename[typename];
if ("string" === typeof rootId) return [rootId];
}

const context: KeyFieldsContext = {
typename,
selectionSet,
Expand Down

0 comments on commit 6eb8d13

Please sign in to comment.