Skip to content

Commit

Permalink
refactor: upgrade and run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed May 28, 2020
1 parent a6e8e0a commit 4ed1401
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 111 deletions.
1 change: 1 addition & 0 deletions .prettierignore
@@ -0,0 +1 @@
dist
6 changes: 0 additions & 6 deletions .prettierrc

This file was deleted.

31 changes: 12 additions & 19 deletions README.md
Expand Up @@ -32,9 +32,7 @@ const query = `{
}
}`

request('https://api.graph.cool/simple/v1/movies', query).then(data =>
console.log(data)
)
request('https://api.graph.cool/simple/v1/movies', query).then((data) => console.log(data))
```

## Usage
Expand All @@ -43,11 +41,11 @@ request('https://api.graph.cool/simple/v1/movies', query).then(data =>
import { request, GraphQLClient } from 'graphql-request'

// Run GraphQL queries/mutations using a static function
request(endpoint, query, variables).then(data => console.log(data))
request(endpoint, query, variables).then((data) => console.log(data))

// ... or create a GraphQL client instance to send requests
const client = new GraphQLClient(endpoint, { headers: {} })
client.request(query, variables).then(data => console.log(data))
client.request(query, variables).then((data) => console.log(data))
```

## Examples
Expand Down Expand Up @@ -81,7 +79,7 @@ async function main() {
console.log(JSON.stringify(data, undefined, 2))
}

main().catch(error => console.error(error))
main().catch((error) => console.error(error))
```

[TypeScript Source](examples/authentication-via-http-header.ts)
Expand Down Expand Up @@ -114,7 +112,7 @@ async function main() {
console.log(JSON.stringify(data, undefined, 2))
}

main().catch(error => console.error(error))
main().catch((error) => console.error(error))
```

[TypeScript Source](examples/passing-more-options-to-fetch.ts)
Expand Down Expand Up @@ -146,7 +144,7 @@ async function main() {
console.log(JSON.stringify(data, undefined, 2))
}

main().catch(error => console.error(error))
main().catch((error) => console.error(error))
```

[TypeScript Source](examples/using-variables.ts)
Expand Down Expand Up @@ -179,7 +177,7 @@ async function main() {
}
}

main().catch(error => console.error(error))
main().catch((error) => console.error(error))
```

[TypeScript Source](examples/error-handling)
Expand Down Expand Up @@ -207,7 +205,7 @@ async function main() {
console.log(JSON.stringify(data, undefined, 2))
}

main().catch(error => console.error(error))
main().catch((error) => console.error(error))
```

### Cookie support for `node`
Expand Down Expand Up @@ -245,7 +243,7 @@ async function main() {
console.log(JSON.stringify(data, undefined, 2))
}

main().catch(error => console.error(error))
main().catch((error) => console.error(error))
```

[TypeScript Source](examples/cookie-support-for-node)
Expand All @@ -272,16 +270,11 @@ async function main() {
}
`

const { data, errors, extensions, headers, status } = await rawRequest(
endpoint,
query
)
console.log(
JSON.stringify({ data, errors, extensions, headers, status }, undefined, 2)
)
const { data, errors, extensions, headers, status } = await rawRequest(endpoint, query)
console.log(JSON.stringify({ data, errors, extensions, headers, status }, undefined, 2))
}

main().catch(error => console.error(error))
main().catch((error) => console.error(error))
```

[TypeScript Source](examples/receiving-a-raw-response)
Expand Down
5 changes: 2 additions & 3 deletions examples/authentication-via-http-header.ts
@@ -1,6 +1,5 @@
import { GraphQLClient } from '../src'

;(async function() {
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

const graphQLClient = new GraphQLClient(endpoint, {
Expand All @@ -26,4 +25,4 @@ import { GraphQLClient } from '../src'

const data = await graphQLClient.request<TData>(query)
console.log(JSON.stringify(data, undefined, 2))
})().catch(error => console.error(error))
})().catch((error) => console.error(error))
21 changes: 10 additions & 11 deletions examples/cookie-support-for-node.ts
@@ -1,8 +1,7 @@
(global as any).fetch = require('fetch-cookie/node-fetch')(require('node-fetch'))
;(global as any).fetch = require('fetch-cookie/node-fetch')(require('node-fetch'))

import { GraphQLClient } from '../src'

;(async function() {
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

const graphQLClient = new GraphQLClient(endpoint, {
Expand All @@ -12,20 +11,20 @@ import { GraphQLClient } from '../src'
})

const query = /* GraphQL */ `
{
Movie(title: "Inception") {
releaseDate
actors {
name
}
{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
`
}
`

interface TData {
Movie: { releaseDate: string; actors: Array<{ name: string }> }
}

const data = await graphQLClient.rawRequest<TData>(query)
console.log(JSON.stringify(data, undefined, 2))
})().catch(error => console.error(error))
})().catch((error) => console.error(error))
5 changes: 2 additions & 3 deletions examples/error-handling.ts
@@ -1,6 +1,5 @@
import { request } from '../src'

;(async function() {
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

const query = /* GraphQL */ `
Expand All @@ -25,4 +24,4 @@ import { request } from '../src'
console.error(JSON.stringify(error, undefined, 2))
process.exit(1)
}
})().catch(error => console.error(error))
})().catch((error) => console.error(error))
5 changes: 2 additions & 3 deletions examples/passing-more-options-to-fetch.ts
@@ -1,6 +1,5 @@
import { GraphQLClient } from '../src'

;(async function() {
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

const graphQLClient = new GraphQLClient(endpoint, {
Expand All @@ -25,4 +24,4 @@ import { GraphQLClient } from '../src'

const data = await graphQLClient.request<TData>(query)
console.log(JSON.stringify(data, undefined, 2))
})().catch(error => console.error(error))
})().catch((error) => console.error(error))
14 changes: 4 additions & 10 deletions examples/receiving-a-raw-response.ts
@@ -1,6 +1,5 @@
import { rawRequest } from '../src'

;(async function() {
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

const query = /* GraphQL */ `
Expand All @@ -18,11 +17,6 @@ import { rawRequest } from '../src'
Movie: { releaseDate: string; actors: Array<{ name: string }> }
}

const { data, errors, extensions, headers, status } = await rawRequest<TData>(
endpoint,
query
)
console.log(
JSON.stringify({ data, errors, extensions, headers, status }, undefined, 2)
)
})().catch(error => console.error(error))
const { data, errors, extensions, headers, status } = await rawRequest<TData>(endpoint, query)
console.log(JSON.stringify({ data, errors, extensions, headers, status }, undefined, 2))
})().catch((error) => console.error(error))
5 changes: 2 additions & 3 deletions examples/using-variables.ts
@@ -1,6 +1,5 @@
import { request } from '../src'

;(async function() {
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'

const query = /* GraphQL */ `
Expand All @@ -24,4 +23,4 @@ import { request } from '../src'

const data = await request<TData>(endpoint, query, variables)
console.log(JSON.stringify(data, undefined, 2))
})().catch(error => console.error(error))
})().catch((error) => console.error(error))
7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -30,6 +30,7 @@
},
"homepage": "https://github.com/prisma/graphql-request",
"scripts": {
"format": "prettier --write .",
"prepublish": "npm run build",
"build": "rm -rf dist && tsc -d",
"lint": "tslint --project . {examples,src,test}/**/*.ts",
Expand All @@ -38,15 +39,17 @@
},
"dependencies": {},
"devDependencies": {
"@prisma-labs/prettier-config": "^0.1.0",
"@types/fetch-mock": "5.12.2",
"@types/node": "8.5.5",
"ava": "^3.8.2",
"bundlesize": "^0.18.0",
"fetch-cookie": "0.7.2",
"fetch-mock": "5.13.1",
"prettier": "1.14.2",
"prettier": "^2.0.5",
"tslint": "5.11.0",
"tslint-config-standard": "8.0.1",
"typescript": "3.0.3"
}
},
"prettier": "@prisma-labs/prettier-config"
}
5 changes: 1 addition & 4 deletions renovate.json
@@ -1,6 +1,3 @@
{
"extends": [
"config:base",
"docker:disable"
]
"extends": ["config:base", "docker:disable"]
}
23 changes: 7 additions & 16 deletions src/index.ts
Expand Up @@ -15,7 +15,7 @@ export class GraphQLClient {

async rawRequest<T = any>(
query: string,
variables?: Variables,
variables?: Variables
): Promise<{
data?: T
extensions?: any
Expand Down Expand Up @@ -43,11 +43,10 @@ export class GraphQLClient {
const { headers, status } = response
return { ...result, headers, status }
} else {
const errorResult =
typeof result === 'string' ? { error: result } : result
const errorResult = typeof result === 'string' ? { error: result } : result
throw new ClientError(
{ ...errorResult, status: response.status, headers: response.headers },
{ query, variables },
{ query, variables }
)
}
}
Expand All @@ -72,12 +71,8 @@ export class GraphQLClient {
if (response.ok && !result.errors && result.data) {
return result.data
} else {
const errorResult =
typeof result === 'string' ? { error: result } : result
throw new ClientError(
{ ...errorResult, status: response.status },
{ query, variables },
)
const errorResult = typeof result === 'string' ? { error: result } : result
throw new ClientError({ ...errorResult, status: response.status }, { query, variables })
}
}

Expand All @@ -102,7 +97,7 @@ export class GraphQLClient {
export function rawRequest<T = any>(
url: string,
query: string,
variables?: Variables,
variables?: Variables
): Promise<{
data?: T
extensions?: any
Expand All @@ -115,11 +110,7 @@ export function rawRequest<T = any>(
return client.rawRequest<T>(query, variables)
}

export function request<T = any>(
url: string,
query: string,
variables?: Variables,
): Promise<T> {
export function request<T = any>(url: string, query: string, variables?: Variables): Promise<T> {
const client = new GraphQLClient(url)

return client.request<T>(query, variables)
Expand Down

0 comments on commit 4ed1401

Please sign in to comment.