Skip to content

Instantly share code, notes, and snippets.

@timsuchanek
Created April 13, 2018 16:39
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timsuchanek/a9caf49d1221b86ebbe066a55b24dbdb to your computer and use it in GitHub Desktop.
Save timsuchanek/a9caf49d1221b86ebbe066a55b24dbdb to your computer and use it in GitHub Desktop.
Yoga Testing
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