Skip to content

Commit

Permalink
(chore) - remove useDevtoolsContext (#387)
Browse files Browse the repository at this point in the history
* (chore) - remove usage of useDevtoolsContext

* (chore) - remove tests relate'd to useDevtoolsContext

* (chore) - save size by not spreading context

* (fix) - fix test by defaulting to an empty object

* (fix) - ensure useMutation executes with an empty object as context to prevent potential crashes
  • Loading branch information
JoviDeCroock authored and kitten committed Aug 21, 2019
1 parent 6685f43 commit 666b799
Show file tree
Hide file tree
Showing 10 changed files with 4 additions and 216 deletions.
1 change: 0 additions & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './useMutation';
export * from './useQuery';
export * from './useSubscription';
export * from './useDevtoolsContext';
117 changes: 0 additions & 117 deletions src/hooks/useDevtoolsContext.test.ts

This file was deleted.

50 changes: 0 additions & 50 deletions src/hooks/useDevtoolsContext.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/hooks/useMutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,6 @@ describe('on execute', () => {
);
});

it('calls executeMutation with source component info', () => {
renderer.create(<MutationUser {...props} />);
act(() => {
execute(vars);
});
expect(client.executeMutation.mock.calls[0][1]).toHaveProperty('meta', {
source: 'MutationUser',
});
});

it('can adjust context in executeMutation', () => {
renderer.create(<MutationUser {...props} />);
act(() => {
Expand Down
6 changes: 2 additions & 4 deletions src/hooks/useMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Context } from '../context';
import { OperationResult, OperationContext } from '../types';
import { CombinedError, createRequest } from '../utils';
import { useImmediateState } from './useImmediateState';
import { useDevtoolsContext } from './useDevtoolsContext';

export interface UseMutationState<T> {
fetching: boolean;
Expand All @@ -22,7 +21,6 @@ export type UseMutationResponse<T, V> = [
export const useMutation = <T = any, V = object>(
query: DocumentNode | string
): UseMutationResponse<T, V> => {
const devtoolsContext = useDevtoolsContext();
const client = useContext(Context);
const [state, setState] = useImmediateState<UseMutationState<T>>({
fetching: false,
Expand All @@ -43,15 +41,15 @@ export const useMutation = <T = any, V = object>(
const request = createRequest(query, variables as any);

return pipe(
client.executeMutation(request, { ...devtoolsContext, ...context }),
client.executeMutation(request, context || {}),
toPromise
).then(result => {
const { data, error, extensions } = result;
setState({ fetching: false, data, error, extensions });
return result;
});
},
[client, devtoolsContext, query, setState]
[client, query, setState]
);

return [state, executeMutation];
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ describe('useQuery', () => {
variables: mockVariables,
},
{
meta: { source: 'TestHook' },
requestPolicy: undefined,
url: 'test',
}
Expand Down
13 changes: 0 additions & 13 deletions src/hooks/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,6 @@ describe('on initial useEffect', () => {
})
);
});

it('passes source component name to executeQuery', () => {
renderer.create(<QueryUser {...props} />);

expect(client.executeQuery).toBeCalledWith(
expect.any(Object),
expect.objectContaining({
meta: {
source: 'QueryUser',
},
})
);
});
});

describe('on subscription', () => {
Expand Down
4 changes: 0 additions & 4 deletions src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { pipe, subscribe } from 'wonka';
import { Context } from '../context';
import { OperationContext, RequestPolicy } from '../types';
import { CombinedError, noop } from '../utils';
import { useDevtoolsContext } from './useDevtoolsContext';
import { useRequest } from './useRequest';
import { useImmediateEffect } from './useImmediateEffect';
import { useImmediateState } from './useImmediateState';
Expand Down Expand Up @@ -33,7 +32,6 @@ export type UseQueryResponse<T> = [
export const useQuery = <T = any, V = object>(
args: UseQueryArgs<V>
): UseQueryResponse<T> => {
const devtoolsContext = useDevtoolsContext();
const unsubscribe = useRef(noop);
const client = useContext(Context);

Expand Down Expand Up @@ -62,7 +60,6 @@ export const useQuery = <T = any, V = object>(
pollInterval: args.pollInterval,
...args.context,
...opts,
...devtoolsContext,
}),
subscribe(({ data, error, extensions }) => {
setState({ fetching: false, data, error, extensions });
Expand All @@ -74,7 +71,6 @@ export const useQuery = <T = any, V = object>(
args.requestPolicy,
args.pollInterval,
client,
devtoolsContext,
request,
setState,
]
Expand Down
9 changes: 0 additions & 9 deletions src/hooks/useSubscription.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ describe('on initial useEffect', () => {
expect.any(Object)
);
});

it('passes source component info to executeSubscription', () => {
renderer.create(<SubscriptionUser q={query} />);
expect(client.executeSubscription).toBeCalledWith(
expect.any(Object),
expect.objectContaining({ meta: { source: 'SubscriptionUser' } })
);
});
});

it('should support setting context in useSubscription params', () => {
Expand All @@ -85,7 +77,6 @@ it('should support setting context in useSubscription params', () => {
},
{
url: 'test',
meta: { source: 'SubscriptionUser' },
}
);
});
Expand Down
9 changes: 2 additions & 7 deletions src/hooks/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useCallback, useContext, useEffect, useRef } from 'react';
import { pipe, subscribe } from 'wonka';
import { Context } from '../context';
import { CombinedError, noop } from '../utils';
import { useDevtoolsContext } from './useDevtoolsContext';
import { useRequest } from './useRequest';
import { useImmediateState } from './useImmediateState';
import { OperationContext } from '../types';
Expand All @@ -29,7 +28,6 @@ export const useSubscription = <T = any, R = T, V = object>(
args: UseSubscriptionArgs<V>,
handler?: SubscriptionHandler<T, R>
): UseSubscriptionResponse<R> => {
const devtoolsContext = useDevtoolsContext();
const unsubscribe = useRef(noop);
const client = useContext(Context);

Expand All @@ -48,10 +46,7 @@ export const useSubscription = <T = any, R = T, V = object>(
unsubscribe.current();

[unsubscribe.current] = pipe(
client.executeSubscription(request, {
...devtoolsContext,
...args.context,
}),
client.executeSubscription(request, args.context || {}),
subscribe(({ data, error, extensions }) => {
setState(s => ({
fetching: true,
Expand All @@ -61,7 +56,7 @@ export const useSubscription = <T = any, R = T, V = object>(
}));
})
);
}, [client, devtoolsContext, handler, request, setState, args.context]);
}, [client, handler, request, setState, args.context]);

// Trigger subscription on query change
// We don't use useImmediateEffect here as we have no way of
Expand Down

0 comments on commit 666b799

Please sign in to comment.