Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom document transforms #10509

Merged
merged 132 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
132 commits
Select commit Hold shift + click to select a range
c455aa6
First pass at adding the ability to configure custom document transfo…
jerelmiller Feb 3, 2023
b72fcdd
Create a DocumentTransformer class to handle transforming the document.
jerelmiller Feb 3, 2023
e07d1c6
Add tests to validate custom document transforms work correctly
jerelmiller Feb 3, 2023
518be12
Add tests to ensure default transforms use existing behavior
jerelmiller Feb 3, 2023
c57c7d0
Remove transformDocument options that sends @client fields through th…
jerelmiller Feb 3, 2023
5739704
Add changeset
jerelmiller Feb 3, 2023
5bb2afc
Enforce immutability for document argument for the transform.
jerelmiller Feb 3, 2023
9c76e09
Adjust hwo document transforms are added to save some bundle size
jerelmiller Feb 6, 2023
1e9c666
Remove Readonly modifier since DocumentNode is already readonly
jerelmiller Feb 7, 2023
bdfbfc2
Create a new DocumentTransform class to handle individual transforms
jerelmiller May 12, 2023
52f371c
Add documentTransform option to ApolloClient
jerelmiller May 12, 2023
2ec0a52
Minor refactor to destructure client and server query together in get…
jerelmiller May 12, 2023
020629c
Run queries and mutations through the document transform
jerelmiller May 12, 2023
e4a7bee
Run subscriptions through the document transform
jerelmiller May 12, 2023
9ee7fc9
Pass query to getResultsFromLink instead of pulling it from queryInfo
jerelmiller May 12, 2023
c75e0e4
Remove DocumentTransformer and revert cache to original implementation
jerelmiller May 12, 2023
e107546
Replace typenameDocumentCache with DocumentTransform
jerelmiller May 12, 2023
91beba0
Update snapshots
jerelmiller May 12, 2023
af101b2
Update document transform test to ensure caching has no effect when c…
jerelmiller May 12, 2023
2c49428
Add test to ensure concatenated transforms run in proper order
jerelmiller May 12, 2023
536d258
Create a custom toMatchDocument jest matcher
jerelmiller May 12, 2023
0e23529
Update DocumentTransform tests to use new custom matcher
jerelmiller May 12, 2023
5d21430
Create a default transform that runs transform through link
jerelmiller May 12, 2023
1c656fc
Remove transformForLink from default transform for now
jerelmiller May 17, 2023
04c89f6
Fix lint errors with import type
jerelmiller May 18, 2023
56e5ef5
Add tests for document transforms on client.query() calls
jerelmiller May 18, 2023
e98ee23
Add tests for document transforms on client.mutate() calls
jerelmiller May 18, 2023
63cc53e
Add tests for document transforms on client.subscribe() calls
jerelmiller May 19, 2023
ec9c708
Run document transforms before setting the query on ObservableQuery
jerelmiller May 19, 2023
26cbd0c
Disable cache in tests that check number of times transform is called
jerelmiller May 19, 2023
e8b8924
Don't run transforms via `watchQuery` until request is kicked off
jerelmiller May 19, 2023
8f6a25e
Strip accidentally commited trailing whitespace
jerelmiller May 19, 2023
52d6b40
Add test to validate document transforms are run on refetch
jerelmiller May 19, 2023
75044f9
Add more tests for verifying transfrom is re-run on fetchMore/setOpti…
jerelmiller May 19, 2023
63aedb4
Update test name
jerelmiller May 19, 2023
851fa7c
Add test to ensure fetchMore with different query runs transforms
jerelmiller May 19, 2023
b58b731
Check against full result for calls to mock
jerelmiller May 19, 2023
a489d44
Add failing assertions for value of computed `query` property for tra…
jerelmiller May 19, 2023
a4a87c8
Add tests to ensure default transforms are called with watchQuery
jerelmiller May 19, 2023
ae1b664
Add fragments from the fragment registry in transformDocument instead…
jerelmiller May 20, 2023
d042be4
Remove it.only
jerelmiller May 22, 2023
48ea041
Minor update to comment
jerelmiller May 22, 2023
2d5c0ef
Refactor out fetchQueryObservable that forwarded the call to another …
jerelmiller May 22, 2023
e25830e
Remove call to this.transform in fetchConcastWithInfo
jerelmiller May 22, 2023
9962e3a
Create function to get information about query
jerelmiller May 22, 2023
be5076e
Call new getDocumentInfo from getVariables
jerelmiller May 22, 2023
dff62b4
Add assertion on number of times transform is called
jerelmiller May 22, 2023
2e7cb13
Don't call transform on query passed to fetchConcastWithInfo
jerelmiller May 22, 2023
dbe965a
Add a lastQuery property and use for computed query property in Obser…
jerelmiller May 22, 2023
18a9118
Transform the document in reobserveAsConcast and set as lastQuery
jerelmiller May 22, 2023
f124b1d
Transform query in fetchQuery when calling client.query
jerelmiller May 22, 2023
5dd31a9
Don't run this.transform on mutation document, only call transformDoc…
jerelmiller May 22, 2023
9ea514e
Use toHaveBeenLastCalledWith in document transform tests
jerelmiller May 22, 2023
e3c5ec7
Always set lastQuery in ObservableQuery when calling reobserve
jerelmiller May 22, 2023
e6e7f60
Move transformation of query on client.query up to query function
jerelmiller May 22, 2023
338ac3a
Properly handle document transforms with fetchMore
jerelmiller May 22, 2023
196aecb
Add some edge case expectations for fetchMore with query
jerelmiller May 22, 2023
2557aaf
Fix incorrect test setup resulting in false negative
jerelmiller May 22, 2023
8ec4ee4
Create transformDocument helper in ObservableQuery
jerelmiller May 22, 2023
e376d94
Fix other failing tests due to incorrect test setup
jerelmiller May 22, 2023
1d5e466
Fix failing test due to change in object identity
jerelmiller May 22, 2023
ae1024f
Run custom transforms on the original document when calling `fetchMor…
jerelmiller May 22, 2023
11b1893
Update test name to reflect behavior
jerelmiller May 22, 2023
09b8bc1
Use ternary for setting lastQuery
jerelmiller May 22, 2023
c21fe45
Minor tweak to comment
jerelmiller May 22, 2023
c8a8643
Make test more obvious what the change in query is
jerelmiller May 22, 2023
04c1229
Add test to ensure setOptions with new query works as expected
jerelmiller May 22, 2023
2e40235
Switch over to getDocumentInfo for most usages of queryManager.transform
jerelmiller May 22, 2023
e4030ff
No need to wrap query in this.transform when creating a subscription
jerelmiller May 22, 2023
c678c47
Switch another usage to getDocumentInfo
jerelmiller May 22, 2023
f11f429
Run checkDocument in DocumentTransform before running the transform f…
jerelmiller May 22, 2023
104e289
Run checkDocument immediately when calling `watchQuery`
jerelmiller May 22, 2023
821dbcf
Fix prettier applying changes to some files that shouldn't
jerelmiller May 22, 2023
36e0f52
Add note about the difference between computed query property and opt…
jerelmiller May 23, 2023
b82ff2a
Update fragment registry test to include missing __typename in result
jerelmiller May 23, 2023
e706352
Add tests to ensure document transforms are run before the cache is read
jerelmiller May 23, 2023
86a4d8d
Ensure refetchQueries works with document transforms
jerelmiller May 23, 2023
8c32d7e
Add test to ensure refetchQueries with mutate works with legacy option
jerelmiller May 23, 2023
8893fb0
QueryManager.transform is now implemented with document transforms
jerelmiller May 23, 2023
0c4948e
Ensure useSuspenseQuery default queries return __typename
jerelmiller May 23, 2023
10e0387
Pass this.query when checking hasForcedResolvers
jerelmiller May 23, 2023
3747f83
Set the transformed query on queryInfo immediately after calling watc…
jerelmiller May 23, 2023
d39058d
Remove document from getDocumentInfo cache entry
jerelmiller May 23, 2023
abe8793
Adjust size limit
jerelmiller May 23, 2023
a857181
Merge branch 'release-3.8' into custom-document-transforms
jerelmiller May 23, 2023
9b73288
Rename cacheOutput to cacheResult
jerelmiller May 23, 2023
4180679
Adjust size-limit again
jerelmiller May 23, 2023
48d79f5
Update changeset with updated API
jerelmiller May 23, 2023
bee883b
Run getDocumentInfo on transformed query when running mutation
jerelmiller May 23, 2023
5caf545
Run transform first when calling watchQuery to avoid checkDocument ca…
jerelmiller May 23, 2023
b5e75af
Run `checkDocument` after checking cache in `transformDocument` to av…
jerelmiller May 23, 2023
019c6f2
Export isQuery, isMutation, and isSubscription helpers from utilities
jerelmiller May 23, 2023
b6ccc80
Add a static `split` method to DocumentTransform to conditionally exe…
jerelmiller May 23, 2023
a05ce44
Disable cache for concatenated transforms to let each transform deter…
jerelmiller May 23, 2023
eddaa30
Only create the transform cache when necessary
jerelmiller May 23, 2023
4022e0d
Return whether the document was invalidated
jerelmiller May 23, 2023
b6ab3ee
Don't cache identity transform
jerelmiller May 23, 2023
7a62ac0
Minor refactor of `split` to cut down on repetition
jerelmiller May 23, 2023
9a3e82a
Ensure `invalidateDocument` invalidates the chain of transform caches…
jerelmiller May 23, 2023
0339f45
Determine nonreactive based on transformed query, not the original query
jerelmiller May 23, 2023
43619d5
Rename isQuery/isSubscription/isMutation to include Operation suffix
jerelmiller May 23, 2023
0440c7a
Increase size-limit
jerelmiller May 23, 2023
e6b1711
Clarify how invalidateDocument works with `.split` transform.
jerelmiller May 23, 2023
208aff9
Update document transform tests to assert on cache size when calling …
jerelmiller May 23, 2023
54873b6
Allow for custom invalidation for each transform. Fix transforms with…
jerelmiller May 24, 2023
2c60856
Use a noop in several places
jerelmiller May 24, 2023
37677d1
Add test to validate .split with .concat properly invalidates
jerelmiller May 24, 2023
473de15
Update test name
jerelmiller May 24, 2023
9176b84
Rename private method
jerelmiller May 24, 2023
cab8a49
Add tests to demonstrate behavior of custom `invalidate`
jerelmiller May 24, 2023
dc1aa01
Return transformed doc from invalidateDocument. Add test to validate …
jerelmiller May 24, 2023
8ea5c3b
Add test to ensure invalidating a document with no cache is ok
jerelmiller May 24, 2023
bfe651d
Add a documentTransform computed property on ApolloClient to get a re…
jerelmiller May 24, 2023
a17765f
Minor tweak to test name
jerelmiller May 24, 2023
2372ce2
Run the transform during invalidation if custom invalidate is not pas…
jerelmiller May 24, 2023
635aca6
Fix incorrect ordering of toMatchDocument diff
jerelmiller May 24, 2023
ddb1f5a
Better validation message in toMatchDocument when argument is not a D…
jerelmiller May 24, 2023
b5148c1
Allow `next` function to return the transformed document
jerelmiller May 24, 2023
018d14e
Update tests to assert `invalidateDocument` now returns cached transf…
jerelmiller May 24, 2023
bf9f535
Add more document transform tests to ensure validation works as expected
jerelmiller May 24, 2023
8c3dd25
Remove ability to invalidate the caches for a document. Will rely on …
jerelmiller May 24, 2023
2e9f140
Add ability to create custom cache keys
jerelmiller May 24, 2023
9e90b13
Immediately return a transformed document back from the document tran…
jerelmiller May 24, 2023
1fe533b
Update test to show uncached transforms return transformed document
jerelmiller May 24, 2023
c4b7092
Update size-limit
jerelmiller May 24, 2023
782d162
Add tests to describe query property
jerelmiller May 24, 2023
ee0b4ae
Simplify DocumentTransform caching using Trie entries.
benjamn May 25, 2023
3187c3a
Minor tweak to comment about why cache is disabled for `concat` and `…
jerelmiller May 25, 2023
4ec1b52
Ensure `getCacheKey` option allows `undefined` return type
jerelmiller May 25, 2023
fbc436e
No need for intermediate variable in `.split`
jerelmiller May 25, 2023
f7890ae
Format code updated in ee0b4ae
jerelmiller May 25, 2023
46748ba
Add formatting changes to git-blame-ignore-revs
jerelmiller May 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .changeset/warm-pandas-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@apollo/client': minor
---

Add the ability to specify custom GraphQL document transforms. These transforms are run before reading data from the cache, before local state is resolved, and before the query document is sent through the link chain.

To register a custom document transform, create a transform using the `DocumentTransform` class and pass it to the `documentTransform` option on `ApolloClient`.

```ts
import { DocumentTransform } from '@apollo/client';

const documentTransform = new DocumentTransform((document) => {
// do something with `document`
return transformedDocument;
});

const client = new ApolloClient({ documentTransform: documentTransform });
```

For additional documentation on the behavior and API of `DocumentTransform`, see the [pull request](https://github.com/apollographql/apollo-client/pull/10509).
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# format "ObservableQuery" test (in #10597)
104bf11765b1db50292f9656aa8fe48e2d749a83

# format changes from ee0b4ae
f7890ae96a3ba900d3de9bf8b23254bcfba18a25
18 changes: 16 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*.md

# Do not format anything automatically except files listed below
/*
*

##### PATHS TO BE FORMATTED #####
!src/
Expand All @@ -38,9 +38,15 @@ src/react/context/__tests__/*
# Allow src/react/cache
!src/react/cache/

# Allow certain core tests
# Allow certain core files
!src/core
src/core/*
!src/core/__tests__/
src/core/tests/*
!src/core/__tests__/equalByQuery.ts
!src/core/__tests__/DocumentTransform.ts
!src/core/equalByQuery.ts
!src/core/DocumentTransform.ts

# Allowed utilities
!src/utilities/
Expand All @@ -59,6 +65,9 @@ src/utilities/common/*
src/utilities/common/__tests__/*
!src/utilities/common/__tests__/omitDeep.ts
!src/utilities/common/__tests__/stripTypename.ts
!src/utilities/graphql
src/utilities/graphql/*
!src/utilities/graphql/operations.ts

# Allowed links
!src/link
Expand All @@ -78,3 +87,8 @@ src/react/hooks/*
src/react/hooks/__tests__/*
!src/react/hooks/__tests__/useSuspenseQuery.test.tsx
!src/react/hooks/__tests__/useBackgroundQuery.test.tsx

## Allowed testing utils
!src/testing
src/testing/*
!src/testing/matchers
4 changes: 2 additions & 2 deletions .size-limit.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const checks = [
{
path: "dist/apollo-client.min.cjs",
limit: "36.87kb"
limit: "37.06kb"
},
{
path: "dist/main.cjs",
Expand All @@ -10,7 +10,7 @@ const checks = [
{
path: "dist/index.js",
import: "{ ApolloClient, InMemoryCache, HttpLink }",
limit: "35.02kb"
limit: "35.31kb"
},
...[
"ApolloProvider",
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/__snapshots__/exports.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Array [
"ApolloLink",
"ApolloProvider",
"Cache",
"DocumentTransform",
"DocumentType",
"HttpLink",
"InMemoryCache",
Expand Down Expand Up @@ -93,6 +94,7 @@ Array [
"ApolloError",
"ApolloLink",
"Cache",
"DocumentTransform",
"HttpLink",
"InMemoryCache",
"MissingFieldError",
Expand Down Expand Up @@ -421,15 +423,18 @@ Array [
"isExecutionPatchResult",
"isField",
"isInlineFragment",
"isMutationOperation",
"isNodeReadableStream",
"isNodeResponse",
"isNonEmptyArray",
"isNonNullObject",
"isPlainObject",
"isQueryOperation",
"isReadableStream",
"isReference",
"isStatefulPromise",
"isStreamableBlob",
"isSubscriptionOperation",
"iterateObserversSafely",
"makeReference",
"makeUniqueId",
Expand Down
Loading