All files / src/query/schema utils.js

86.96% Statements 20/23
66.67% Branches 6/9
83.33% Functions 5/6
90.91% Lines 20/22

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                      9x 629x 629x 629x     9x 530x 530x   99x     14x     64x               26x           26x                                 327x       9x 37x 52x       9x   9x 99x 99x    
// @flow
import mapValues from 'lodash/mapValues';
import {types} from './types';
import ScalarField from './scalarField';
import ArrayField from './arrayField';
import ObjectField from './objectField';
import RelationField from './relationField';
import CompositeField from './compositeField';
 
import type {Types, Field} from './types';
 
export const getType = (type: string): Types | null => {
  const upperType = type.toUpperCase();
  const enumType: Types = types[upperType];
  return enumType || null;
};
 
export const createField = (key: string, rootSchema: any, schema: any, isEntity?: boolean): Field => {
  const type = getType(schema.type);
  switch (type) {
    case types.ARRAY:
      return new ArrayField({key, rootSchema, schema, isEntity});
 
    case types.OBJECT:
      return new ObjectField({key, rootSchema, schema, isEntity});
 
    case types.RELATION:
      return new RelationField({key, rootSchema, schema});
 
    /**
     * File {contentType: string, name: string, size: string, url: string}
     * Image {contentType: string, name: string, size: string, url: string}
     */
    case types.FILE:
    case types.IMAGE: {
      const childFields = {
        contentType: {type: 'string'},
        name: {type: 'string'},
        size: {type: 'string'},
        url: {type: 'string'}
      };
      return new CompositeField({key, type, rootSchema, childFields});
    }
 
    /**
     * GeoPoint {lat: , lng: string, placeId: string, address: string}
     */
    case types.GEOPOINT: {
      const childFields = {
        lat: {type: 'string'},
        lng: {type: 'string'},
        placeId: {type: 'string'},
        address: {type: 'string'}
      };
      return new CompositeField({key, type, rootSchema, childFields});
    }
 
    default:
      return new ScalarField({key, schema, type});
  }
};
 
export const createSchema = (rootSchema: any) => {
  return mapValues(rootSchema, (fieldSchema, key) => {
    return createField(key, rootSchema, rootSchema[key], true);
  });
};
 
export const capitalizeFirstLetter = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);
 
export const isCompositeType = (type: string) => {
  const enumType = getType(type);
  return [types.FILE, types.GEOPOINT, types.IMAGE].indexOf(enumType) >= 0;
};