Ocelot-Social/src/jest/helpers.js
2019-01-14 16:07:52 +01:00

28 lines
719 B
JavaScript

import { request } from 'graphql-request'
import { GraphQLClient } from 'graphql-request'
export const host = 'http://127.0.0.1:3123'
export async function getJWT({ email, password }) {
const mutation = `
mutation {
login(email:"${email}", password:"${password}"){
token
}
}`
const response = await request(host, mutation)
const { token } = response.login
if(!token) throw `Could not get a JWT token from the backend:\n${response}`
return token
}
export async function authenticatedGraphQLClient(params){
const jwt = await getJWT(params)
const options = {
headers: {
'Authorization': `Bearer ${jwt}`
}
}
return new GraphQLClient(host, options)
}