Skip to content

Commit

Permalink
fix(link-http): allow to use HttpParams on GET requests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela committed Oct 31, 2017
1 parent 9369abe commit 3ec24f3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
24 changes: 22 additions & 2 deletions packages/apollo-angular-link-http/src/HttpLink.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Injectable} from '@angular/core';
import {HttpClient, HttpResponse} from '@angular/common/http';
import {HttpClient, HttpResponse, HttpParams} from '@angular/common/http';
import {
ApolloLink,
Observable as LinkObservable,
Expand Down Expand Up @@ -68,14 +68,34 @@ export class HttpLinkHandler extends ApolloLink {
req.method = method;
}

// `body` for some, `params` for others
const useBody = ['POST', 'PUT', 'PATCH'].includes(
req.method.toUpperCase(),
);
let bodyOrParams = {};

if (useBody) {
bodyOrParams = {
body: req.body,
};
} else {
const params = Object.keys(req.body).reduce(
(httpParams, param) =>
httpParams.set(param, (req.body as any)[param]),
new HttpParams(),
);

bodyOrParams = {params};
}

// create a request
const obs: Observable<HttpResponse<Object>> = httpClient.request<
Object
>(req.method, req.url, {
body: req.body,
observe: 'response',
responseType: 'json',
reportProgress: false,
...bodyOrParams,
...req.options,
});

Expand Down
1 change: 0 additions & 1 deletion packages/apollo-angular-link-http/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export type Body = {
query: string;
variables?: Record<string, any>;
operationName?: string;
context?: Record<string, any> | Context;
extensions?: Record<string, any>;
};

Expand Down
3 changes: 1 addition & 2 deletions packages/apollo-angular-link-http/tests/HttpLink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ describe('HttpLink', () => {

httpBackend.match(req => {
expect(req.method).toBe('GET');
expect(req.body.operationName).toBe(op.operationName);
expect(req.detectContentTypeHeader()).toBe('application/json');
expect(req.params.get('operationName')).toBe(op.operationName);
return true;
});
},
Expand Down
10 changes: 8 additions & 2 deletions packages/apollo-angular-link-http/tests/SSR.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,19 @@ describe('integration', () => {
) {}

public ngOnInit() {
execute(this.httpLink.create({uri: 'graphql'}), {
execute(this.httpLink.create({uri: 'graphql', method: 'GET'}), {
query,
}).subscribe(result => {
this.text = result.data.website.status;
});

this.httpBackend.expectOne('graphql').flush({data});
this.httpBackend
.match(
req =>
req.url === 'graphql' &&
req.params.get('operationName') === 'websiteInfo',
)[0]
.flush({data});
}
}

Expand Down

0 comments on commit 3ec24f3

Please sign in to comment.