Created
April 13, 2018 16:39
-
-
Save timsuchanek/a9caf49d1221b86ebbe066a55b24dbdb to your computer and use it in GitHub Desktop.
Yoga Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as http from 'http' | |
import * as https from 'https' | |
import { GraphQLClient } from 'graphql-request' | |
const debug = require('debug')('TestClient') | |
import { GraphQLServer } from 'graphql-yoga' | |
import { server } from '../server' | |
export default class TestClient { | |
app: GraphQLServer | |
server: http.Server | |
address: string | |
client: GraphQLClient | |
initPromise: Promise<void> | |
constructor() { | |
this.app = server | |
this.initPromise = this.init() | |
debug({ address: this.address }) | |
} | |
async init(): Promise<void> { | |
if (!this.server) { | |
this.server = (await this.app.start({ | |
port: 0, | |
debug: false, | |
})) as any | |
} | |
const { port } = this.server.address() | |
const protocol = this.server instanceof https.Server ? 'https' : 'http' | |
this.address = `${protocol}://127.0.0.1:${port}/` | |
this.client = new GraphQLClient(this.address) | |
} | |
async request<T = any>( | |
query: string, | |
variables?: any, | |
headers?: any, | |
): Promise<any> { | |
debug(this.address, query, variables) | |
await this.initPromise | |
if (headers) { | |
return new GraphQLClient(this.address, { headers }).request( | |
query, | |
variables, | |
) | |
} | |
return this.client.request<T>(query, variables) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment