Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typescript-react-apollo] Generate HookResult type #2133

Merged
merged 1 commit into from Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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, {});
});
});
});