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

fix: add gql request name support #2419

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions projects/graphql-client/src/graphql-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface GraphQlMutationHandler<TRequest, TResponse> extends GraphQlHand
export interface GraphQlRequestOptions {
cacheability?: GraphQlRequestCacheability;
isolated?: boolean;
name?: string;
}

export const enum GraphQlRequestCacheability {
Expand Down
11 changes: 7 additions & 4 deletions projects/graphql-client/src/graphql-request.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class GraphQlRequestService {
options: GraphQlRequestOptions
): Observable<TResponse> {
return type === GraphQlHandlerType.Mutation
? this.executeMutation(requestString)
? this.executeMutation(requestString, options)
: this.executeQuery(requestString, options);
}

Expand All @@ -155,7 +155,7 @@ export class GraphQlRequestService {
): Observable<TResponse> {
return this.apollo
.query<TResponse>({
query: gql(requestString),
query: gql(`query ${options?.name ?? ''} ${requestString}`),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work with current batching behavior? I would think it would either break or prevent batching - neither good. Work to push the name further down so multiple named queries can be grouped (you might want to use alias rather than a query name, for example).

errorPolicy: 'all',
fetchPolicy: options.cacheability
})
Expand All @@ -169,10 +169,13 @@ export class GraphQlRequestService {
);
}

private executeMutation<TResponse extends Dictionary<unknown>>(requestString: string): Observable<TResponse> {
private executeMutation<TResponse extends Dictionary<unknown>>(
requestString: string,
options: GraphQlRequestOptions
): Observable<TResponse> {
return this.apollo
.mutate<TResponse>({
mutation: gql(`mutation ${requestString}`)
mutation: gql(`mutation ${options?.name ?? ''} ${requestString}`)
})
.pipe(
tap(response => {
Expand Down