All files / src/query index.js

95.45% Statements 21/22
87.5% Branches 7/8
100% Functions 7/7
95.24% Lines 20/21

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                      20x 20x 20x 20x       1x 1x 1x 1x 6x   6x               31x 1x   30x 30x       24x 24x 23x 23x   1x         64x    
// @flow
 
import {schemaToQueriesObject, objectToQueries} from './utils';
import {get, set, mapKeys} from 'lodash';
 
export class Query {
  schema: Object
  queries: Object
  variables: Object
 
  constructor({schema}: {schema: Object}) {
    this.schema = schema;
    const {queriesObj, variables} = schemaToQueriesObject(schema);
    this.variables = variables;
    this.queries = queriesObj;
  }
 
  updateQueries = (pathArr: Array<string>, field: string, value: any) => {
    const path = `${pathArr.join('.fields.')}.${field}`;
    Eif (field === 'args') {
      const args = get(this.queries, path);
      this.variables = {...this.variables, ...Object.keys(args).reduce((result: Object, key: string) => {
        result[args[key]] = value[key];
 
        return result;
      }, {})};
    } else {
      set(this.queries, path, value);
    }
  }
 
  getQueries = (pathArr: Array<string>): Object => {
    if (!pathArr || pathArr.length === 0) {
      return this.queries;
    }
    const path = pathArr.join('.fields.');
    return get(this.queries, path);
  }
 
  toGQL = (key?: string): string => {
    const variables = this.getVairables();
    if (key) {
      const obj = this.getQueries([key]);
      return objectToQueries({[key]: obj}, !obj.declareArgs, variables);  
    } else {
      return objectToQueries(this.queries, false, variables);
    }
  }
 
  getVairables = () => {
    return mapKeys(this.variables, (value, key) => key.substr(1));
  }
}