All files / src/client/utils formatRequestBody.ts

54.54% Statements 6/11
41.66% Branches 5/12
100% Functions 1/1
66.66% Lines 6/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  1x     1x       5x   5x   5x 5x            
import {DocumentNode} from 'graphql/language/ast'
import {print} from 'graphql/language/printer'
import {FlexibleRequestBody, GraphQLRequestPayload} from '../../types'
 
export default function formatRequestBody(
  body: FlexibleRequestBody
): GraphQLRequestPayload {
  // string
  Iif (typeof body === 'string') return {query: body}
  // DocumentNode
  else Iif ('kind' in body) return {query: print(body)}
  // FlexibleRequestBody w/ {query: string}
  else if ('query' in body && typeof body.query === 'string')
    return body as GraphQLRequestPayload
  // FlexibleRequestBody w/ {query:DocumentNode}
  else Eif ('query' in body && 'kind' in (body.query as DocumentNode))
    return {...body, query: print(body.query as DocumentNode)}
  else throw new Error('The format of the request is malformed')
}