Skip to content

Commit

Permalink
More tests of TypedDocumentNode and cache.{read,write}{Query,Fragment}.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Aug 4, 2020
1 parent af18c78 commit 2e0c725
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 1 deletion.
58 changes: 58 additions & 0 deletions src/cache/inmemory/__tests__/__snapshots__/cache.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,61 @@ Object {
},
}
`;

exports[`TypedDocumentNode<Data, Variables> should determine Data and Variables types of {write,read}{Query,Fragment} 1`] = `
Object {
"Author:{\\"name\\":\\"John C. Mitchell\\"}": Object {
"__typename": "Author",
"name": "John C. Mitchell",
},
"Book:{\\"isbn\\":\\"0262133210\\"}": Object {
"__typename": "Book",
"author": Object {
"__ref": "Author:{\\"name\\":\\"John C. Mitchell\\"}",
},
"isbn": "0262133210",
"title": "Foundations for Programming Languages",
},
"ROOT_QUERY": Object {
"__typename": "Query",
"book({\\"isbn\\":\\"0262133210\\"})": Object {
"__ref": "Book:{\\"isbn\\":\\"0262133210\\"}",
},
},
}
`;
exports[`TypedDocumentNode<Data, Variables> should determine Data and Variables types of {write,read}{Query,Fragment} 2`] = `
Object {
"Author:{\\"name\\":\\"Harold Abelson\\"}": Object {
"__typename": "Author",
"name": "Harold Abelson",
},
"Author:{\\"name\\":\\"John C. Mitchell\\"}": Object {
"__typename": "Author",
"name": "John C. Mitchell",
},
"Book:{\\"isbn\\":\\"0262133210\\"}": Object {
"__typename": "Book",
"author": Object {
"__ref": "Author:{\\"name\\":\\"John C. Mitchell\\"}",
},
"isbn": "0262133210",
"title": "Foundations for Programming Languages",
},
"Book:{\\"isbn\\":\\"0262510871\\"}": Object {
"__typename": "Book",
"author": Object {
"__ref": "Author:{\\"name\\":\\"Harold Abelson\\"}",
},
"isbn": "0262510871",
"title": "Structure and Interpretation of Computer Programs",
},
"ROOT_QUERY": Object {
"__typename": "Query",
"book({\\"isbn\\":\\"0262133210\\"})": Object {
"__ref": "Book:{\\"isbn\\":\\"0262133210\\"}",
},
},
}
`;
158 changes: 157 additions & 1 deletion src/cache/inmemory/__tests__/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import gql, { disableFragmentWarnings } from 'graphql-tag';

import { stripSymbols } from '../../../utilities/testing/stripSymbols';
import { cloneDeep } from '../../../utilities/common/cloneDeep';
import { makeReference, Reference, makeVar } from '../../../core';
import { makeReference, Reference, makeVar, TypedDocumentNode, isReference } from '../../../core';
import { InMemoryCache, InMemoryCacheConfig } from '../inMemoryCache';

disableFragmentWarnings();
Expand Down Expand Up @@ -2496,3 +2496,159 @@ describe("ReactiveVar and makeVar", () => {
});
});
});

describe('TypedDocumentNode<Data, Variables>', () => {
type Book = {
isbn?: string;
title: string;
author: {
name: string;
};
};

const query: TypedDocumentNode<
{ book: Book },
{ isbn: string }
> = gql`query GetBook($isbn: String!) {
book(isbn: $isbn) {
title
author {
name
}
}
}`;

const fragment: TypedDocumentNode<Book> = gql`
fragment TitleAndAuthor on Book {
title
isbn
author {
name
}
}
`;

it('should determine Data and Variables types of {write,read}{Query,Fragment}', () => {
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
book(existing, { args, toReference }) {
return existing ?? (args && toReference({
__typename: "Book",
isbn: args.isbn,
}));
}
}
},

Book: {
keyFields: ["isbn"],
},

Author: {
keyFields: ["name"],
},
},
});

// We need to define these objects separately from calling writeQuery,
// because passing them directly to writeQuery will trigger excess property
// warnings due to the extra __typename and isbn fields. Internally, we
// almost never pass object literals to writeQuery or writeFragment, so
// excess property checks should not be a problem in practice.
const jcmAuthor = {
__typename: "Author",
name: "John C. Mitchell",
};

const ffplBook = {
__typename: "Book",
isbn: "0262133210",
title: "Foundations for Programming Languages",
author: jcmAuthor,
};

const ffplVariables = {
isbn: "0262133210",
};

cache.writeQuery({
query,
variables: ffplVariables,
data: {
book: ffplBook,
},
});

expect(cache.extract()).toMatchSnapshot();

const ffplQueryResult = cache.readQuery({
query,
variables: ffplVariables,
});

if (ffplQueryResult === null) throw new Error("null result");
expect(ffplQueryResult.book.isbn).toBeUndefined();
expect(ffplQueryResult.book.author.name).toBe(jcmAuthor.name);
expect(ffplQueryResult).toEqual({
book: {
__typename: "Book",
title: "Foundations for Programming Languages",
author: {
__typename: "Author",
name: "John C. Mitchell",
},
},
});

const sicpBook = {
__typename: "Book",
isbn: "0262510871",
title: "Structure and Interpretation of Computer Programs",
author: {
__typename: "Author",
name: "Harold Abelson",
},
};

const sicpRef = cache.writeFragment({
fragment,
data: sicpBook,
});

expect(isReference(sicpRef)).toBe(true);
expect(cache.extract()).toMatchSnapshot();

const ffplFragmentResult = cache.readFragment({
fragment,
id: cache.identify(ffplBook),
});
if (ffplFragmentResult === null) throw new Error("null result");
expect(ffplFragmentResult.title).toBe(ffplBook.title);
expect(ffplFragmentResult.author.name).toBe(ffplBook.author.name);
expect(ffplFragmentResult).toEqual(ffplBook);

// This uses the read function for the Query.book field.
const sicpReadResult = cache.readQuery({
query,
variables: {
isbn: sicpBook.isbn,
},
});
if (sicpReadResult === null) throw new Error("null result");
expect(sicpReadResult.book.isbn).toBeUndefined();
expect(sicpReadResult.book.title).toBe(sicpBook.title);
expect(sicpReadResult.book.author.name).toBe(sicpBook.author.name);
expect(sicpReadResult).toEqual({
book: {
__typename: "Book",
title: "Structure and Interpretation of Computer Programs",
author: {
__typename: "Author",
name: "Harold Abelson",
},
},
});
});
});

0 comments on commit 2e0c725

Please sign in to comment.