Skip to content

Commit

Permalink
Add support for GET requests
Browse files Browse the repository at this point in the history
  • Loading branch information
forabi committed Mar 30, 2018
1 parent 4f02144 commit 37fb5bb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 27 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -39,6 +39,7 @@
"typescript": "2.7.2"
},
"dependencies": {
"cross-fetch": "2.0.0"
"cross-fetch": "2.0.0",
"universal-url": "^1.0.0"
}
}
63 changes: 37 additions & 26 deletions src/index.ts
@@ -1,6 +1,9 @@
import { ClientError, GraphQLError, Headers, Options, Variables } from './types'
export { ClientError } from './types'
import 'cross-fetch/polyfill'
import { shim } from 'universal-url'

shim()

export class GraphQLClient {
private url: string
Expand All @@ -15,19 +18,7 @@ export class GraphQLClient {
query: string,
variables?: Variables,
): Promise<{ data?: T, extensions?: any, errors?: GraphQLError[] }> {
const { headers, ...others } = this.options

const body = JSON.stringify({
query,
variables: variables ? variables : undefined,
})

const response = await fetch(this.url, {
method: 'POST',
headers: Object.assign({ 'Content-Type': 'application/json' }, headers),
body,
...others,
})
const response = await this.getResponse(query, variables)

const result = await getResult(response)

Expand All @@ -47,19 +38,7 @@ export class GraphQLClient {
query: string,
variables?: Variables,
): Promise<T> {
const { headers, ...others } = this.options

const body = JSON.stringify({
query,
variables: variables ? variables : undefined,
})

const response = await fetch(this.url, {
method: 'POST',
headers: Object.assign({ 'Content-Type': 'application/json' }, headers),
body,
...others,
})
const response = await this.getResponse(query, variables)

const result = await getResult(response)

Expand Down Expand Up @@ -91,6 +70,38 @@ export class GraphQLClient {
}
return this
}

async getResponse(
query: string,
variables?: Variables,
) {
const { headers, method = 'POST', ...others } = this.options
let response: Response

if (method.toLowerCase() === 'get') {
const url = new URL(this.url)
url.searchParams.append('query', JSON.stringify(query))
if (variables) {
url.searchParams.append('variables', JSON.stringify(variables))
}

response = await fetch(url.toString(), { headers })
} else {
const body = JSON.stringify({
query,
variables,
})

response = await fetch(this.url, {
headers: Object.assign({ 'Content-Type': 'application/json' }, headers),
body,
...others,
method: 'POST',
})
}

return response
}
}

export async function rawRequest<T extends any>(
Expand Down
37 changes: 37 additions & 0 deletions yarn.lock
Expand Up @@ -1421,6 +1421,10 @@ has-yarn@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/has-yarn/-/has-yarn-1.0.0.tgz#89e25db604b725c8f5976fff0addc921b828a5a7"

hasurl@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/hasurl/-/hasurl-1.0.0.tgz#e4c619097ae1e8fc906bee904ce47e94f5e1ea37"

hawk@~3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/hawk/-/hawk-3.1.3.tgz#078444bd7c1640b0fe540d2c9b73d59678e8e1c4"
Expand Down Expand Up @@ -1853,6 +1857,10 @@ lodash.merge@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.0.tgz#69884ba144ac33fe699737a6086deffadd0f89c5"

lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"

lodash@^4.17.4, lodash@^4.2.0:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
Expand Down Expand Up @@ -2307,6 +2315,10 @@ punycode@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e"

punycode@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.0.tgz#5f863edc89b96db09074bad7947bf09056ca4e7d"

qs@~6.4.0:
version "6.4.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.4.0.tgz#13e26d28ad6b0ffaa91312cd3bf708ed351e7233"
Expand Down Expand Up @@ -2826,6 +2838,12 @@ tough-cookie@~2.3.0:
dependencies:
punycode "^1.4.1"

tr46@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/tr46/-/tr46-1.0.1.tgz#a8b13fd6bfd2489519674ccde55ba3693b706d09"
dependencies:
punycode "^2.1.0"

trim-newlines@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-1.0.0.tgz#5887966bb582a4503a41eb524f7d35011815a613"
Expand Down Expand Up @@ -2923,6 +2941,13 @@ unique-temp-dir@^1.0.0:
os-tmpdir "^1.0.1"
uid2 "0.0.3"

universal-url@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/universal-url/-/universal-url-1.0.0.tgz#3375a3f9166f38984e089d12ede58006f088a3d1"
dependencies:
hasurl "^1.0.0"
whatwg-url "^6.0.1"

unzip-response@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/unzip-response/-/unzip-response-2.0.1.tgz#d2f0f737d16b0615e72a6935ed04214572d56f97"
Expand Down Expand Up @@ -2968,6 +2993,10 @@ verror@1.3.6:
dependencies:
extsprintf "1.0.2"

webidl-conversions@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"

well-known-symbols@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/well-known-symbols/-/well-known-symbols-1.0.0.tgz#73c78ae81a7726a8fa598e2880801c8b16225518"
Expand All @@ -2976,6 +3005,14 @@ whatwg-fetch@2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz#9c84ec2dcf68187ff00bc64e1274b442176e1c84"

whatwg-url@^6.0.1:
version "6.4.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-6.4.0.tgz#08fdf2b9e872783a7a1f6216260a1d66cc722e08"
dependencies:
lodash.sortby "^4.7.0"
tr46 "^1.0.0"
webidl-conversions "^4.0.1"

which@^1.2.9:
version "1.2.14"
resolved "https://registry.yarnpkg.com/which/-/which-1.2.14.tgz#9a87c4378f03e827cecaf1acdf56c736c01c14e5"
Expand Down

0 comments on commit 37fb5bb

Please sign in to comment.