From 3a1ba5cf58166d6c23349ee639669caaec7680e1 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Tue, 9 Jul 2019 02:21:58 -0400 Subject: [PATCH] Generate HookResult type --- dev-test/githunt/types.reactApollo.hooks.tsx | 49 +++++++++++-------- .../typescript/react-apollo/src/visitor.ts | 13 +++-- .../react-apollo/tests/react-apollo.spec.ts | 43 ++++++++++++++++ 3 files changed, 80 insertions(+), 25 deletions(-) diff --git a/dev-test/githunt/types.reactApollo.hooks.tsx b/dev-test/githunt/types.reactApollo.hooks.tsx index 5653df0ceac..003fc3ef3b0 100644 --- a/dev-test/githunt/types.reactApollo.hooks.tsx +++ b/dev-test/githunt/types.reactApollo.hooks.tsx @@ -313,9 +313,10 @@ export function withOnCommentAdded(operationOptions?: }); }; -export function useOnCommentAddedSubscription(baseOptions?: ReactApolloHooks.SubscriptionHookOptions) { - return ReactApolloHooks.useSubscription(OnCommentAddedDocument, baseOptions); -}; + export function useOnCommentAddedSubscription(baseOptions?: ReactApolloHooks.SubscriptionHookOptions) { + return ReactApolloHooks.useSubscription(OnCommentAddedDocument, baseOptions); + }; +export type OnCommentAddedSubscriptionHookResult = ReturnType; export const CommentDocument = gql` query Comment($repoFullName: String!, $limit: Int, $offset: Int) { currentUser { @@ -363,9 +364,10 @@ export function withComment(operationOptions?: ReactAp }); }; -export function useCommentQuery(baseOptions?: ReactApolloHooks.QueryHookOptions) { - return ReactApolloHooks.useQuery(CommentDocument, baseOptions); -}; + export function useCommentQuery(baseOptions?: ReactApolloHooks.QueryHookOptions) { + return ReactApolloHooks.useQuery(CommentDocument, baseOptions); + }; +export type CommentQueryHookResult = ReturnType; export const CurrentUserForProfileDocument = gql` query CurrentUserForProfile { currentUser { @@ -392,9 +394,10 @@ export function withCurrentUserForProfile(operationOpt }); }; -export function useCurrentUserForProfileQuery(baseOptions?: ReactApolloHooks.QueryHookOptions) { - return ReactApolloHooks.useQuery(CurrentUserForProfileDocument, baseOptions); -}; + export function useCurrentUserForProfileQuery(baseOptions?: ReactApolloHooks.QueryHookOptions) { + return ReactApolloHooks.useQuery(CurrentUserForProfileDocument, baseOptions); + }; +export type CurrentUserForProfileQueryHookResult = ReturnType; export const FeedDocument = gql` query Feed($type: FeedType!, $offset: Int, $limit: Int) { currentUser { @@ -423,9 +426,10 @@ export function withFeed(operationOptions?: ReactApoll }); }; -export function useFeedQuery(baseOptions?: ReactApolloHooks.QueryHookOptions) { - return ReactApolloHooks.useQuery(FeedDocument, baseOptions); -}; + export function useFeedQuery(baseOptions?: ReactApolloHooks.QueryHookOptions) { + return ReactApolloHooks.useQuery(FeedDocument, baseOptions); + }; +export type FeedQueryHookResult = ReturnType; export const SubmitRepositoryDocument = gql` mutation submitRepository($repoFullName: String!) { submitRepository(repoFullName: $repoFullName) { @@ -452,9 +456,10 @@ export function withSubmitRepository(operationOptions? }); }; -export function useSubmitRepositoryMutation(baseOptions?: ReactApolloHooks.MutationHookOptions) { - return ReactApolloHooks.useMutation(SubmitRepositoryDocument, baseOptions); -}; + export function useSubmitRepositoryMutation(baseOptions?: ReactApolloHooks.MutationHookOptions) { + return ReactApolloHooks.useMutation(SubmitRepositoryDocument, baseOptions); + }; +export type SubmitRepositoryMutationHookResult = ReturnType; export const SubmitCommentDocument = gql` mutation submitComment($repoFullName: String!, $commentContent: String!) { submitComment(repoFullName: $repoFullName, commentContent: $commentContent) { @@ -481,9 +486,10 @@ export function withSubmitComment(operationOptions?: R }); }; -export function useSubmitCommentMutation(baseOptions?: ReactApolloHooks.MutationHookOptions) { - return ReactApolloHooks.useMutation(SubmitCommentDocument, baseOptions); -}; + export function useSubmitCommentMutation(baseOptions?: ReactApolloHooks.MutationHookOptions) { + return ReactApolloHooks.useMutation(SubmitCommentDocument, baseOptions); + }; +export type SubmitCommentMutationHookResult = ReturnType; export const VoteDocument = gql` mutation vote($repoFullName: String!, $type: VoteType!) { vote(repoFullName: $repoFullName, type: $type) { @@ -514,6 +520,7 @@ export function withVote(operationOptions?: ReactApoll }); }; -export function useVoteMutation(baseOptions?: ReactApolloHooks.MutationHookOptions) { - return ReactApolloHooks.useMutation(VoteDocument, baseOptions); -}; \ No newline at end of file + export function useVoteMutation(baseOptions?: ReactApolloHooks.MutationHookOptions) { + return ReactApolloHooks.useMutation(VoteDocument, baseOptions); + }; +export type VoteMutationHookResult = ReturnType; \ No newline at end of file diff --git a/packages/plugins/typescript/react-apollo/src/visitor.ts b/packages/plugins/typescript/react-apollo/src/visitor.ts index e51c6c4ffe8..cc334fe76a9 100644 --- a/packages/plugins/typescript/react-apollo/src/visitor.ts +++ b/packages/plugins/typescript/react-apollo/src/visitor.ts @@ -129,10 +129,15 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor) { - return ReactApolloHooks.use${operationType}<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, baseOptions); -};`; + + const hookFn = ` + export function use${operationName}(baseOptions?: ReactApolloHooks.${operationType}HookOptions<${this.config.hooksImportFrom === '@apollo/react-hooks' || node.operation !== 'query' ? `${operationResultType}, ` : ''}${operationVariablesTypes}>) { + return ReactApolloHooks.use${operationType}<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, baseOptions); + };`; + + const hookResult = `export type ${operationName}HookResult = ReturnType;`; + + return [hookFn, hookResult].join('\n'); } protected buildOperation(node: OperationDefinitionNode, documentVariableName: string, operationType: string, operationResultType: string, operationVariablesTypes: string): string { diff --git a/packages/plugins/typescript/react-apollo/tests/react-apollo.spec.ts b/packages/plugins/typescript/react-apollo/tests/react-apollo.spec.ts index 546480fc73e..c7dcc94a75c 100644 --- a/packages/plugins/typescript/react-apollo/tests/react-apollo.spec.ts +++ b/packages/plugins/typescript/react-apollo/tests/react-apollo.spec.ts @@ -768,5 +768,48 @@ export function useListenToCommentsSubscription(baseOptions?: ReactApolloHooks.S expect(content.content).toContain(`export function useTestQuery`); }); + + it('should generate hook result', async () => { + const documents = parse(/* GraphQL */ ` + query feed { + feed { + id + commentCount + repository { + full_name + html_url + owner { + avatar_url + } + } + } + } + + mutation submitRepository($name: String) { + submitRepository(repoFullName: $name) { + id + } + } + `); + const docs = [{ filePath: '', content: documents }]; + + const content = (await plugin( + schema, + docs, + { withHooks: true, withComponent: false, withHOC: false }, + { + outputFile: 'graphql.tsx', + } + )) as Types.ComplexPluginOutput; + + expect(content.content).toBeSimilarStringTo(` + export type FeedQueryHookResult = ReturnType; + `); + + expect(content.content).toBeSimilarStringTo(` + export type SubmitRepositoryMutationHookResult = ReturnType; + `); + await validateTypeScript(content, schema, docs, {}); + }); }); });