Skip to content

Commit

Permalink
feat: support parsed query (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
Divyendu Singh authored and timsuchanek committed Jul 24, 2018
1 parent 3e917c0 commit ddfd0a8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
4 changes: 4 additions & 0 deletions package.json
Expand Up @@ -34,11 +34,15 @@
"@types/node": "8.5.5",
"ava": "0.25.0",
"fetch-mock": "5.13.1",
"graphql-tag": "2.9.2",
"tslint": "5.9.1",
"tslint-config-standard": "7.0.0",
"typescript": "2.7.2"
},
"dependencies": {
"cross-fetch": "2.0.0"
},
"peerDependencies": {
"graphql": "0.13.2"
}
}
10 changes: 5 additions & 5 deletions src/index.ts
@@ -1,7 +1,7 @@
import { ClientError, GraphQLError, Headers as HttpHeaders, Options, Variables } from './types'
export { ClientError } from './types'
import 'cross-fetch/polyfill'

import { print } from 'graphql'
export class GraphQLClient {
private url: string
private options: Options
Expand Down Expand Up @@ -45,13 +45,13 @@ export class GraphQLClient {
}

async request<T extends any>(
query: string,
query: string | object,
variables?: Variables,
): Promise<T> {
const { headers, ...others } = this.options

const printedQuery = typeof query === 'object' ? print(query) : query
const body = JSON.stringify({
query,
query: printedQuery,
variables: variables ? variables : undefined,
})

Expand All @@ -71,7 +71,7 @@ export class GraphQLClient {
typeof result === 'string' ? { error: result } : result
throw new ClientError(
{ ...errorResult, status: response.status },
{ query, variables },
{ query: printedQuery, variables },
)
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/index.test.ts
Expand Up @@ -2,6 +2,7 @@ import test from 'ava'
import * as fetchMock from 'fetch-mock'
import { ClientError, rawRequest, request, GraphQLClient } from '../src/index'
import { Options } from '../src/types'
import gql from 'graphql-tag'

test('minimal query', async (t) => {
const data = {
Expand Down Expand Up @@ -124,6 +125,21 @@ test('extra fetch options', async (t) => {
})
})

test('minimal parsed query', async (t) => {
const data = {
Actor: {
name: "Tom Hardy"
}
}

/*
This test is flaky because it relies on the internet
but the mock passed for this feature (parsed query) without
the implementation.
*/
t.deepEqual(await request('https://api.graph.cool/simple/v1/movies', gql`query { Actor(name: "Tom Hardy") { name } }`), data)
})

async function mock(response: any, testFn: () => Promise<void>) {
fetchMock.mock({
matcher: '*',
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Expand Up @@ -8,7 +8,7 @@
"strictNullChecks": true,
"noUnusedLocals": true,
"outDir": "dist",
"lib": ["es2015", "es2016", "dom"]
"lib": ["es2015", "es2016", "dom", "esnext"]
},
"exclude": [
"node_modules"
Expand Down

0 comments on commit ddfd0a8

Please sign in to comment.