Skip to content

Commit

Permalink
Introduce useGETForQueries in HttpLink (#1641)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela committed Mar 11, 2021
1 parent 3e3d15c commit 5996109
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/apollo-angular/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### vNext

- `useGETForQueries` in Http Link

### v2.4.0

- Fix: Use let in Apollo constructor due to firefox bug [#1632](https://github.com/kamilkisiela/apollo-angular/pull/1632)
Expand Down
12 changes: 11 additions & 1 deletion packages/apollo-angular/http/src/http-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,22 @@ export class HttpLinkHandler extends ApolloLink {
return prioritize(context[key], this.options[key], init);
};

let method = pick('method', 'POST');
const includeQuery = pick('includeQuery', true);
const includeExtensions = pick('includeExtensions', false);
const method = pick('method', 'POST');
const url = pick('uri', 'graphql');
const withCredentials = pick('withCredentials');
const useMultipart = pick('useMultipart');
const useGETForQueries = this.options.useGETForQueries === true;

const isQuery = operation.query.definitions.some(
(def) =>
def.kind === 'OperationDefinition' && def.operation === 'query',
);

if (useGETForQueries && isQuery) {
method = 'GET';
}

const req: Request = {
method,
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-angular/http/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type OperationPrinter = (operation: DocumentNode) => string;

export interface Options extends FetchOptions, HttpRequestOptions {
operationPrinter?: OperationPrinter;
useGETForQueries?: boolean;
}

export type Body = {
Expand Down
55 changes: 55 additions & 0 deletions packages/apollo-angular/http/tests/http-link.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,59 @@ describe('HttpLink', () => {
data: data1,
});
});

test('should be able to useGETForQueries', () => {
const link = httpLink.create({
uri: 'graphql',
useGETForQueries: true,
includeExtensions: true,
});
const op = {
query: gql`
query heroes {
heroes {
name
}
}
`,
operationName: 'heroes',
variables: {up: 'dog'},
extensions: {what: 'what'},
};

execute(link, op).subscribe(noop);

httpBackend.match((req) => {
expect(req.method).toBe('GET');
expect(req.params.get('variables')).toBe(JSON.stringify(op.variables));
expect(req.params.get('extensions')).toBe(JSON.stringify(op.extensions));
expect(req.params.get('operationName')).toBe(op.operationName);
return true;
});
});

test('useGETForQueries should use GET only for queries :)', () => {
const link = httpLink.create({
uri: 'graphql',
useGETForQueries: true,
});
const op = {
query: gql`
mutation addRandomHero {
addRandomHero {
name
}
}
`,
operationName: 'addRandomHero',
};

execute(link, op).subscribe(noop);

httpBackend.match((req) => {
expect(req.method).toBe('POST');
expect(req.body.operationName).toBe(op.operationName);
return true;
});
});
});
1 change: 1 addition & 0 deletions website/docs/data/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ not to send a query, disable `includeQuery`.
| headers | HttpHeaders | `none` | false |
| withCredentials | boolean | `none` | false |
| method | string | `POST` | false |
| useGETForQueries | boolean | `false` | false |

## Context

Expand Down

0 comments on commit 5996109

Please sign in to comment.