Skip to content

Commit

Permalink
Generate HookResult type (#2133)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan authored and dotansimha committed Jul 9, 2019
1 parent 1cd87c5 commit 5202e64
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 25 deletions.
49 changes: 28 additions & 21 deletions dev-test/githunt/types.reactApollo.hooks.tsx
Expand Up @@ -313,9 +313,10 @@ export function withOnCommentAdded<TProps, TChildProps = {}>(operationOptions?:
});
};

export function useOnCommentAddedSubscription(baseOptions?: ReactApolloHooks.SubscriptionHookOptions<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>) {
return ReactApolloHooks.useSubscription<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>(OnCommentAddedDocument, baseOptions);
};
export function useOnCommentAddedSubscription(baseOptions?: ReactApolloHooks.SubscriptionHookOptions<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>) {
return ReactApolloHooks.useSubscription<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>(OnCommentAddedDocument, baseOptions);
};
export type OnCommentAddedSubscriptionHookResult = ReturnType<typeof useOnCommentAddedSubscription>;
export const CommentDocument = gql`
query Comment($repoFullName: String!, $limit: Int, $offset: Int) {
currentUser {
Expand Down Expand Up @@ -363,9 +364,10 @@ export function withComment<TProps, TChildProps = {}>(operationOptions?: ReactAp
});
};

export function useCommentQuery(baseOptions?: ReactApolloHooks.QueryHookOptions<CommentQueryVariables>) {
return ReactApolloHooks.useQuery<CommentQuery, CommentQueryVariables>(CommentDocument, baseOptions);
};
export function useCommentQuery(baseOptions?: ReactApolloHooks.QueryHookOptions<CommentQueryVariables>) {
return ReactApolloHooks.useQuery<CommentQuery, CommentQueryVariables>(CommentDocument, baseOptions);
};
export type CommentQueryHookResult = ReturnType<typeof useCommentQuery>;
export const CurrentUserForProfileDocument = gql`
query CurrentUserForProfile {
currentUser {
Expand All @@ -392,9 +394,10 @@ export function withCurrentUserForProfile<TProps, TChildProps = {}>(operationOpt
});
};

export function useCurrentUserForProfileQuery(baseOptions?: ReactApolloHooks.QueryHookOptions<CurrentUserForProfileQueryVariables>) {
return ReactApolloHooks.useQuery<CurrentUserForProfileQuery, CurrentUserForProfileQueryVariables>(CurrentUserForProfileDocument, baseOptions);
};
export function useCurrentUserForProfileQuery(baseOptions?: ReactApolloHooks.QueryHookOptions<CurrentUserForProfileQueryVariables>) {
return ReactApolloHooks.useQuery<CurrentUserForProfileQuery, CurrentUserForProfileQueryVariables>(CurrentUserForProfileDocument, baseOptions);
};
export type CurrentUserForProfileQueryHookResult = ReturnType<typeof useCurrentUserForProfileQuery>;
export const FeedDocument = gql`
query Feed($type: FeedType!, $offset: Int, $limit: Int) {
currentUser {
Expand Down Expand Up @@ -423,9 +426,10 @@ export function withFeed<TProps, TChildProps = {}>(operationOptions?: ReactApoll
});
};

export function useFeedQuery(baseOptions?: ReactApolloHooks.QueryHookOptions<FeedQueryVariables>) {
return ReactApolloHooks.useQuery<FeedQuery, FeedQueryVariables>(FeedDocument, baseOptions);
};
export function useFeedQuery(baseOptions?: ReactApolloHooks.QueryHookOptions<FeedQueryVariables>) {
return ReactApolloHooks.useQuery<FeedQuery, FeedQueryVariables>(FeedDocument, baseOptions);
};
export type FeedQueryHookResult = ReturnType<typeof useFeedQuery>;
export const SubmitRepositoryDocument = gql`
mutation submitRepository($repoFullName: String!) {
submitRepository(repoFullName: $repoFullName) {
Expand All @@ -452,9 +456,10 @@ export function withSubmitRepository<TProps, TChildProps = {}>(operationOptions?
});
};

export function useSubmitRepositoryMutation(baseOptions?: ReactApolloHooks.MutationHookOptions<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>) {
return ReactApolloHooks.useMutation<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>(SubmitRepositoryDocument, baseOptions);
};
export function useSubmitRepositoryMutation(baseOptions?: ReactApolloHooks.MutationHookOptions<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>) {
return ReactApolloHooks.useMutation<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>(SubmitRepositoryDocument, baseOptions);
};
export type SubmitRepositoryMutationHookResult = ReturnType<typeof useSubmitRepositoryMutation>;
export const SubmitCommentDocument = gql`
mutation submitComment($repoFullName: String!, $commentContent: String!) {
submitComment(repoFullName: $repoFullName, commentContent: $commentContent) {
Expand All @@ -481,9 +486,10 @@ export function withSubmitComment<TProps, TChildProps = {}>(operationOptions?: R
});
};

export function useSubmitCommentMutation(baseOptions?: ReactApolloHooks.MutationHookOptions<SubmitCommentMutation, SubmitCommentMutationVariables>) {
return ReactApolloHooks.useMutation<SubmitCommentMutation, SubmitCommentMutationVariables>(SubmitCommentDocument, baseOptions);
};
export function useSubmitCommentMutation(baseOptions?: ReactApolloHooks.MutationHookOptions<SubmitCommentMutation, SubmitCommentMutationVariables>) {
return ReactApolloHooks.useMutation<SubmitCommentMutation, SubmitCommentMutationVariables>(SubmitCommentDocument, baseOptions);
};
export type SubmitCommentMutationHookResult = ReturnType<typeof useSubmitCommentMutation>;
export const VoteDocument = gql`
mutation vote($repoFullName: String!, $type: VoteType!) {
vote(repoFullName: $repoFullName, type: $type) {
Expand Down Expand Up @@ -514,6 +520,7 @@ export function withVote<TProps, TChildProps = {}>(operationOptions?: ReactApoll
});
};

export function useVoteMutation(baseOptions?: ReactApolloHooks.MutationHookOptions<VoteMutation, VoteMutationVariables>) {
return ReactApolloHooks.useMutation<VoteMutation, VoteMutationVariables>(VoteDocument, baseOptions);
};
export function useVoteMutation(baseOptions?: ReactApolloHooks.MutationHookOptions<VoteMutation, VoteMutationVariables>) {
return ReactApolloHooks.useMutation<VoteMutation, VoteMutationVariables>(VoteDocument, baseOptions);
};
export type VoteMutationHookResult = ReturnType<typeof useVoteMutation>;
13 changes: 9 additions & 4 deletions packages/plugins/typescript/react-apollo/src/visitor.ts
Expand Up @@ -129,10 +129,15 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor<ReactApolloRawPlug
const operationName: string = this.convertName(node.name.value, { suffix: titleCase(operationType), useTypesPrefix: false });

this.imports.add(this.getReactApolloHooksImport());
return `
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 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<typeof use${operationName}>;`;

return [hookFn, hookResult].join('\n');
}

protected buildOperation(node: OperationDefinitionNode, documentVariableName: string, operationType: string, operationResultType: string, operationVariablesTypes: string): string {
Expand Down
Expand Up @@ -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<typeof useFeedQuery>;
`);

expect(content.content).toBeSimilarStringTo(`
export type SubmitRepositoryMutationHookResult = ReturnType<typeof useSubmitRepositoryMutation>;
`);
await validateTypeScript(content, schema, docs, {});
});
});
});

0 comments on commit 5202e64

Please sign in to comment.