Skip to content

Commit

Permalink
Provide isReference utility function to read/merge functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Dec 3, 2019
1 parent a0de223 commit 03e5114
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/cache/inmemory/__tests__/diffAgainstStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import gql, { disableFragmentWarnings } from 'graphql-tag';

import { Reference, makeReference, isReference } from '../../../utilities/graphql/storeUtils';
import { Reference, makeReference } from '../../../utilities/graphql/storeUtils';
import { defaultNormalizedCacheFactory } from '../entityStore';
import { StoreReader } from '../readFromStore';
import { StoreWriter } from '../writeToStore';
Expand Down Expand Up @@ -959,7 +959,7 @@ describe('diffing queries against the store', () => {
typePolicies: {
Query: {
fields: {
person(_, { args, toReference, getFieldValue }) {
person(_, { args, toReference, getFieldValue, isReference }) {
expect(typeof args.id).toBe('number');
const ref = toReference({ __typename: 'Person', id: args.id });
expect(isReference(ref)).toBe(true);
Expand Down
9 changes: 6 additions & 3 deletions src/cache/inmemory/__tests__/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import gql from "graphql-tag";
import { InMemoryCache } from "../inMemoryCache";
import { StoreValue } from "../../../utilities";
import { FieldPolicy } from "../policies";
import { isReference } from "../../../utilities/graphql/storeUtils";

describe("type policies", function () {
const bookQuery = gql`
Expand Down Expand Up @@ -538,7 +537,7 @@ describe("type policies", function () {
todos: {
keyArgs: [],

read(existing: any[], { args, toReference }) {
read(existing: any[], { args, toReference, isReference }) {
expect(!existing || Object.isFrozen(existing)).toBe(true);
expect(typeof toReference).toBe("function");
const slice = existing.slice(
Expand All @@ -549,7 +548,11 @@ describe("type policies", function () {
return slice;
},

merge(existing: any[], incoming: any[], { args, toReference }) {
merge(existing: any[], incoming: any[], {
args,
toReference,
isReference,
}) {
expect(!existing || Object.isFrozen(existing)).toBe(true);
expect(typeof toReference).toBe("function");
const copy = existing ? existing.slice(0) : [];
Expand Down
6 changes: 6 additions & 0 deletions src/cache/inmemory/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
StoreValue,
argumentsObjectFromField,
makeReference,
isReference,
} from '../../utilities/graphql/storeUtils';

import { canUseWeakMap } from '../../utilities/common/canUse';
Expand Down Expand Up @@ -90,6 +91,9 @@ interface FieldFunctionOptions {
args: Record<string, any>;
field: FieldNode;
variables?: Record<string, any>;

// Utilities for dealing with { __ref } objects.
isReference: typeof isReference;
toReference: Policies["toReference"];

// Gets the existing StoreValue for a given field within the current
Expand Down Expand Up @@ -427,6 +431,7 @@ export class Policies {
args: argumentsObjectFromField(field, variables),
field,
variables,
isReference,
toReference: policies.toReference,
getFieldValue(nameOrField) {
return getFieldValue(
Expand Down Expand Up @@ -455,6 +460,7 @@ export class Policies {
args,
field,
variables,
isReference,
toReference: policies.toReference,
getFieldValue: emptyGetFieldValueForMerge,
});
Expand Down

0 comments on commit 03e5114

Please sign in to comment.