Skip to content

Commit 9539685

Browse files
author
Christian Ivicevic
authored
[react-relay] Add experimental useSubscription hook typing (DefinitelyTyped#44915)
* [react-relay] Add experimental useSubscription hook typing * [react-relay] Mute linter about no-unnecessary-generics * [react-relay] Fix wrong disable linter command * [react-relay] Fix wrong location of the linter comment
1 parent 2a2c1ff commit 9539685

File tree

4 files changed

+76
-10
lines changed

4 files changed

+76
-10
lines changed

types/react-relay/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// Jared Kass <https://github.com/jdk243>
1111
// Renan Machado <https://github.com/renanmav>
1212
// Janic Duplessis <https://github.com/janicduplessis>
13+
// Christian Ivicevic <https://github.com/ChristianIvicevic>
1314
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
1415
// TypeScript Version: 3.3
1516

types/react-relay/lib/hooks.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export { useMutation } from './relay-experimental/useMutation';
1616
export { usePreloadedQuery } from './relay-experimental/usePreloadedQuery';
1717
export { useRefetchableFragment } from './relay-experimental/useRefetchableFragment';
1818
export { useRelayEnvironment } from './relay-experimental/useRelayEnvironment';
19+
export { useSubscription } from './relay-experimental/useSubscription';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { GraphQLSubscriptionConfig, OperationType, requestSubscription } from 'relay-runtime';
2+
3+
export function useSubscription<TSubscriptionPayload extends OperationType>(
4+
// The actual subtype of OperationType is required to allow for type inferrence inside GraphQLSubscriptionConfig.
5+
// tslint:disable-next-line:no-unnecessary-generics
6+
config: GraphQLSubscriptionConfig<TSubscriptionPayload>,
7+
requestSubscriptionFn?: typeof requestSubscription,
8+
): void;

types/react-relay/test/relay-experimental-tests.tsx

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import * as React from 'react';
22

3-
import { Environment, RecordSource, Store, Network, commitMutation, FragmentRefs } from 'relay-runtime';
3+
import {
4+
Environment,
5+
RecordSource,
6+
Store,
7+
Network,
8+
commitMutation,
9+
FragmentRefs,
10+
GraphQLSubscriptionConfig,
11+
} from 'relay-runtime';
412
import {
513
fetchQuery,
614
graphql,
@@ -13,7 +21,8 @@ import {
1321
useRefetchableFragment,
1422
usePaginationFragment,
1523
useBlockingPaginationFragment,
16-
useMutation
24+
useMutation,
25+
useSubscription,
1726
} from 'react-relay/hooks';
1827

1928
const source = new RecordSource();
@@ -748,7 +757,7 @@ function Mutation() {
748757
readonly id: string;
749758
readonly viewer_does_like?: boolean | null;
750759
readonly like_count?: number | null;
751-
}
760+
};
752761
} | null;
753762
}
754763

@@ -758,13 +767,13 @@ function Mutation() {
758767
readonly id: string;
759768
readonly viewer_does_like?: boolean | null;
760769
readonly like_count?: number | null;
761-
}
770+
};
762771
} | null;
763772
}
764773

765774
interface FeedbackLikeMutationVariables {
766775
input: {
767-
id: string,
776+
id: string;
768777
text: string;
769778
};
770779
}
@@ -788,7 +797,7 @@ function Mutation() {
788797
}
789798
`);
790799
if (isInFlight) {
791-
return <div>loading</div>;
800+
return <div>loading</div>;
792801
}
793802
return (
794803
<button
@@ -814,13 +823,60 @@ function Mutation() {
814823
optimisticResponse: {
815824
feedback_like: {
816825
feedback: {
817-
id: "1"
818-
}
819-
}
820-
}
826+
id: '1',
827+
},
828+
},
829+
},
821830
});
822831
}}
823832
/>
824833
);
825834
};
826835
}
836+
837+
/**
838+
* Tests for useSubscription
839+
* see https://relay.dev/docs/en/experimental/api-reference#usesubscription
840+
*/
841+
function Subscription() {
842+
interface SubscriptionVariables {
843+
id: string;
844+
}
845+
interface SubscriptionResponse {
846+
readonly store: {
847+
readonly id: string;
848+
readonly value1: number;
849+
readonly value2: number;
850+
};
851+
}
852+
interface Subscription {
853+
readonly response: SubscriptionResponse;
854+
readonly variables: SubscriptionVariables;
855+
}
856+
857+
interface Props {
858+
id: string;
859+
}
860+
861+
return function SubscriptionComponent({ id }: Props) {
862+
const subscriptionConfig: GraphQLSubscriptionConfig<Subscription> = React.useMemo(
863+
() => ({
864+
subscription: graphql`
865+
subscription subscriptionComponentSubscription($id: ID!) {
866+
store(id: $id) {
867+
id
868+
value1
869+
value2
870+
}
871+
}
872+
`,
873+
variables: { id },
874+
}),
875+
[id],
876+
);
877+
878+
useSubscription(subscriptionConfig);
879+
880+
return <p>The subscription has been started in the background.</p>;
881+
};
882+
}

0 commit comments

Comments
 (0)