All files svqlConfig.ts

100% Statements 55/55
100% Branches 24/24
100% Functions 13/13
100% Lines 39/39

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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136  1x 1x 1x   1x   1x                       1x       1x 2x     1x 18x                                       5x 4x   4x 15x             14x   8x   8x 8x 8x 8x 1x     8x 1x     7x             7x         4x 4x 3x 3x             1x       3x           3x 2x       3x                 4x 3x                 5x 5x   4x    
import { GraphQLFetchOptions, WSOptions } from '..'
import { GraphQL } from './GraphQL'
import { graphqlFetchOptions } from './graphqlFetchOptions'
import { hashObject } from './hashObject'
import { queryType } from './interfaces'
import { SubscribeQL } from './SubscribeQL'
//@ts-ignore
export let client: {
  graphql: GraphQL;
  subscription({ query, variables }: { query: string, variables?: object }): {
    [x: number]: () => any;
    subscribe(observerOrNext?: any, onError?: any, onComplete?: any): {
      unsubscribe: () => void;
    };
  };
  sub: any;
  mutate({ query, variables, cache, key }: queryType): Promise<any>;
  query({ query, variables, cache, key }: queryType): Promise<any>;
} = {}
let _headers = {
  'content-type': 'application/json',
}
 
export function setHeaders(headers: any) {
  _headers = headers
}
 
export function headers() {
  return _headers
}
 
 
 
/**
 * * Gets default client {@link svqlConfig} for a
 * [GraphQL Client]{@link svqlConfig}.
 * @param {*} url
 * @param {*} wsUrl
 * @param {{
 *   connectionCallback:string
 *   connectionParams:Object,
 *   timeout:number,
 *   reconnect:boolean,
 *   reconnectionAttempts:number,
 *   lazy:boolean,
 *   inactivityTimeout:number
 *   }} options
 */
export function getClient(url: string, wsUrl?: string, wsOptions: WSOptions = {}) {
  const graphql = new GraphQL()
 
  const fetchOptionsOverride = (_options: GraphQLFetchOptions) => {
    ; (_options.url = url), (_options.headers = headers())
  }
 
  function getOrSet(
    fetchOptionsOverride: Function,
    data: any,
    withCache: boolean,
    getKey = (key: any) => key
  ) {
    const fetchOptions = graphqlFetchOptions({ ...data })
 
    fetchOptionsOverride(fetchOptions)
    const has = hashObject(fetchOptions)
    getKey(has)
    if (graphql.cache[has] && graphql.cache[has].graphQLErrors) {
      delete graphql.cache[has]
    }
 
    if (graphql.cache[has] && withCache) {
      return new Promise((res) => res(graphql.cache[has]))
    }
 
    const pending = graphql.operate({
      fetchOptionsOverride,
      operation: {
        ...data,
      },
    })
 
    return pending.cacheValuePromise.then((r) => graphql.cache[has])
  }
  /**
   * @param {event:string,callback:Function } client.on
   */
  client.graphql = graphql
  if (wsUrl) {
    const initSub = (ws: any) =>
      SubscribeQL(wsUrl, {
        reconnect: ws.reconnect || true,
        lazy: ws.lazy || true,
        ...(ws.connectionParams
          ? { connectionParams: ws.connectionParams }
          : {
            connectionParams: () => {
              return headers()
            },
          }),
      })
    let sub = initSub(wsOptions)
 
    /**
     *
     * @param { query:string, variables:Object } client.subscription
     */
    client.subscription = ({ query, variables }) =>
      sub.request({ query, variables })
    /**
     * @param {SubscribeQL} client.sub
     */
    client.sub = sub
  }
  /**
   *
   * @param {string} query
   * @param {Object} variables
   * @param {boolean} cache
   * @returns {Promise}
   */
  client.mutate = ({ query, variables, cache = false, key }: queryType): Promise<any> =>
    getOrSet(fetchOptionsOverride, { query, variables }, cache, key)
  /**
   *
   * @param {string} query
   * @param {Object} variables
   * @param {boolean} cache
   * @returns {Promise}
   */
 
  client.query = ({ query, variables, cache = true, key }: queryType): Promise<any> =>
    getOrSet(fetchOptionsOverride, { query, variables }, cache, key)
 
  return client
}