Skip to content

Commit

Permalink
feat: adds watchFragment support
Browse files Browse the repository at this point in the history
  • Loading branch information
alessbell authored and PowerKiKi committed Aug 13, 2024
1 parent 3812bbe commit 4ca9c4a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-plums-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"apollo-angular": minor
---

Exposes `watchFragment` method on the `ApolloBase` class.
16 changes: 16 additions & 0 deletions packages/apollo-angular/src/apollo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { from, Observable } from 'rxjs';
import { Inject, Injectable, NgZone, Optional } from '@angular/core';
import type { WatchFragmentResult } from '@apollo/client';
import type {
ApolloClientOptions,
ApolloQueryResult,
Expand All @@ -19,6 +20,7 @@ import type {
MutationOptions,
MutationResult,
NamedOptions,
WatchFragmentOptions,
WatchQueryOptions,
} from './types';
import { fixObservable, fromPromise, useMutationLoading, wrapWithZone } from './utils';
Expand Down Expand Up @@ -66,6 +68,20 @@ export class ApolloBase<TCacheShape = any> {
);
}

public watchFragment<
TFragmentData = unknown,
TVariables extends OperationVariables = EmptyObject,
>(
options: WatchFragmentOptions<TFragmentData, TVariables>,
extra?: ExtraSubscriptionOptions,
): Observable<WatchFragmentResult<TFragmentData>> {
const obs = from(
fixObservable(this.ensureClient().watchFragment<TFragmentData, TVariables>({ ...options })),
);

return extra && extra.useZone !== true ? obs : wrapWithZone(obs, this.ngZone);
}

public subscribe<T, V extends OperationVariables = EmptyObject>(
options: SubscriptionOptions<V, T>,
extra?: ExtraSubscriptionOptions,
Expand Down
4 changes: 4 additions & 0 deletions packages/apollo-angular/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
MutationOptions as CoreMutationOptions,
QueryOptions as CoreQueryOptions,
SubscriptionOptions as CoreSubscriptionOptions,
WatchFragmentOptions as CoreWatchFragmentOptions,
WatchQueryOptions as CoreWatchQueryOptions,
FetchResult,
OperationVariables,
Expand Down Expand Up @@ -65,6 +66,9 @@ export interface MutationOptions<TData = any, TVariables = EmptyObject>
useMutationLoading?: boolean;
}

export interface WatchFragmentOptions<TData = any, TVariables = EmptyObject>
extends CoreWatchFragmentOptions<TData, TVariables> {}

export interface SubscriptionResult<TData> extends ExecutionResult<TData> {}

export type NamedOptions = Record<string, ApolloClientOptions<any>>;
Expand Down
32 changes: 32 additions & 0 deletions packages/apollo-angular/tests/Apollo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,38 @@ describe('Apollo', () => {
});
});

describe('watchFragment', () => {
test('should be called with the same options', () => {
const apollo = new Apollo(ngZone);

apollo.create({
link: mockSingleLink(),
cache: new InMemoryCache(),
});

const client = apollo.client;

const fragment = gql`
fragment ItemFragment on Item {
id
text
}
`;

const from = 'Item:1';

const options = {
fragment,
from,
};

client.watchFragment = jest.fn().mockReturnValue(new Observable());
apollo.watchFragment(options);

expect(client.watchFragment).toBeCalledWith(options);
});
});

describe('query updates', () => {
test('should update a query after mutation', (done: jest.DoneCallback) => {
expect.assertions(3);
Expand Down
22 changes: 3 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11351,16 +11351,7 @@ string-length@^4.0.1:
char-regex "^1.0.2"
strip-ansi "^6.0.0"

"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -11402,14 +11393,7 @@ stringify-entities@^4.0.0:
character-entities-html4 "^2.0.0"
character-entities-legacy "^3.0.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -12470,7 +12454,7 @@ wildcard@^2.0.0:
resolved "https://registry.yarnpkg.com/wildcard/-/wildcard-2.0.1.tgz#5ab10d02487198954836b6349f74fff961e10f67"
integrity sha512-CC1bOL87PIWSBhDcTrdeLo6eGT7mCFtrg0uIJtqJUFyK+eJnzl8A1niH56uu7KMa5XFrtiV+AQuHO3n7DsHnLQ==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand Down

0 comments on commit 4ca9c4a

Please sign in to comment.