Skip to content

Commit

Permalink
✨ Add option to run on HTTP2
Browse files Browse the repository at this point in the history
  • Loading branch information
khaosdoctor committed Jul 31, 2020
1 parent d72210b commit 98f1a87
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ const options = {
headers: {
"Authorization": "Bearer <token>"
},
debug: false
debug: false,
useHttp2: false
}

gotQL.query('mygraphqlendpoint.com.br/api', query, options)
Expand Down Expand Up @@ -199,6 +200,8 @@ Both `gotql.query` and `gotql.mutation` accept an optional user option object wi
- Type: `object`, in the form of `[headerName: string]: headerValue: string`
- _gotInstance_: Customized Got instance to be used when calling the endpoint
- Type: `got`. Internally this will be called as `got.post(prependHttp(endPoint), gotPayload)`
- _useHttp2_: Boolean defining if the call should be made using HTTP2, defaults to `false` (see [release 11 of got](https://github.com/sindresorhus/got/releases/tag/v11.0.0))
- Type: `boolean`

>**Note:** GotQL uses [`debug`](https://npmjs.com/package/debug) internally as default debugger, so you can set debug levels by setting the `DEBUG` environment variable. These are the current levels:
>
Expand Down
8 changes: 4 additions & 4 deletions src/modules/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function getQueryVariables (variables?: QueryType['variables']) {
* @param {queryType} query JSON-like query type
* @param {string} parsedQuery String-parsed query
*/
function getPayload (headers: UserOptions['headers'], query: QueryType, parsedQuery: string) {
function getPayload (headers: UserOptions['headers'], options: UserOptions, query: QueryType, parsedQuery: string) {
info('Generating final payload')
const returnObject: Pick<Options, 'json' | 'headers' | 'http2'> = {
headers: getHeaders(headers),
Expand All @@ -66,7 +66,7 @@ function getPayload (headers: UserOptions['headers'], query: QueryType, parsedQu
operationName: query.name || null,
variables: getQueryVariables(query.variables) || null
},
http2: false
http2: options.useHttp2 || false
}

info('Payload to be sent: %O', returnObject)
Expand Down Expand Up @@ -107,7 +107,7 @@ function handleResponse (response: Response<any>, options?: UserOptions): GotQL.
* @param {any} got The Got object as an injected dependency (for test modularity)
* @return {{data: object, statusCode: number, message: string}} Got handled response
*/
export async function run (endPoint: string, query: QueryType, type: GotQL.ExecutionType, got: GotInstance, options?: UserOptions): Promise<GotQL.Response> {
export async function run (endPoint: string, query: QueryType, type: GotQL.ExecutionType, got: GotInstance, options: UserOptions = { useHttp2: false }): Promise<GotQL.Response> {
try {
info('Invoking runner with query type %s', type)
if (!['query', 'mutation'].includes(type)) throw new Error('Query type must be either `query` or `mutation`')
Expand All @@ -118,7 +118,7 @@ export async function run (endPoint: string, query: QueryType, type: GotQL.Execu

info('Building payload object')
const headers = options ? options.headers : {}
const gotPayload = getPayload(headers, query, graphQuery)
const gotPayload = getPayload(headers, options, query, graphQuery)
info('Payload object: %O', gotPayload.json)
info('Sending request...')
let response = await got.post<Request>(prependHttp(endPoint), gotPayload)
Expand Down
3 changes: 2 additions & 1 deletion src/types/UserOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ import { Got as GotInstance } from 'got'
export type UserOptions = {
errorStatusCode?: number,
headers?: Record<string, string>,
gotInstance?: GotInstance
gotInstance?: GotInstance,
useHttp2?: boolean
}

0 comments on commit 98f1a87

Please sign in to comment.