Skip to content

Commit

Permalink
fix: make WatchFragmentOptionsAlone required
Browse files Browse the repository at this point in the history
  • Loading branch information
alessbell committed Jul 17, 2024
1 parent 81cf17a commit 19d128e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
16 changes: 12 additions & 4 deletions packages/apollo-angular/src/fragment.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import type { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
import type { DocumentNode, TypedDocumentNode, WatchFragmentResult } from '@apollo/client';
import type {
DocumentNode,
OperationVariables,
TypedDocumentNode,
WatchFragmentResult,
} from '@apollo/client';
import { Apollo } from './apollo';
import type { EmptyObject, WatchFragmentOptionsAlone } from './types';

@Injectable()
export class Fragment<T = {}, V = EmptyObject> {
public readonly fragment: DocumentNode | TypedDocumentNode<T, V>;
export abstract class Fragment<
T extends OperationVariables = {},
V extends OperationVariables = EmptyObject,
> {
public abstract readonly fragment: DocumentNode | TypedDocumentNode<T, V>;
public client = 'default';

constructor(protected apollo: Apollo) {}

public watch(
options: WatchFragmentOptionsAlone<T, V>,
variables?: V,
options?: WatchFragmentOptionsAlone<T, V>,
): Observable<WatchFragmentResult<T>> {
return this.apollo
.use(this.client)
Expand Down
10 changes: 5 additions & 5 deletions packages/apollo-angular/tests/Fragment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const fragment = gql`

@Injectable()
export class ItemFragment extends Fragment {
public document = fragment;
public fragment = fragment;
}

describe('Fragment', () => {
Expand Down Expand Up @@ -45,20 +45,20 @@ describe('Fragment', () => {
});

test('should have fragment document defined', () => {
expect(items.document).toEqual(fragment);
expect(items.fragment).toEqual(fragment);
});

test('should use watchFragment under the hood', () => {
apolloMock.watchFragment.mockReturnValue('FragmentResult');

const result = items.watch();
const result = items.watch({ from: 'Item:1 ' });

expect(apolloMock.watchFragment).toHaveBeenCalled();
expect(result).toEqual('FragmentResult');
});

test('should pass variables to Apollo.watchFragment', () => {
items.watch({ foo: 1 });
items.watch({ from: 'Item:1' }, { foo: 1 });

expect(apolloMock.watchFragment).toHaveBeenCalled();
expect(apolloMock.watchFragment.mock.calls[0][0]).toMatchObject({
Expand All @@ -68,8 +68,8 @@ describe('Fragment', () => {

test('should pass options to Apollo.watchQuery', () => {
items.watch(
{},
{ from: 'Item:1', optimistic: true, canonizeResults: true, fragmentName: 'ItemFragment' },
{},
);

expect(apolloMock.watchFragment).toHaveBeenCalled();
Expand Down

0 comments on commit 19d128e

Please sign in to comment.