All files / src/api/connector/internal graphql.js

44.78% Statements 30/67
25% Branches 5/20
37.5% Functions 6/16
45.45% Lines 30/66

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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205                      4x                                                           4x         4x                             4x 1x                                                                               1x     4x 1x   1x     4x 1x 1x 1x 1x                 1x   1x 1x 1x           1x 1x   1x 1x   1x 1x         1x 1x 1x               1x 1x       1x                                                                                      
import http from 'axios';
 
import {
  each,
  find,
  isEmpty,
  map,
} from 'lodash';
 
import { getCommonMeta, getSavedSources } from '../common';
 
const getQueryParams = (source) => {
  const args = [];
  const bindings = [];
  each(source.params, (arg) => {
    args.push(`$${arg.name}: ${arg.type}`);
    bindings.push(`${arg.name}: $${arg.name}`);
  });
 
  if (!isEmpty(source.filters)) {
    args.push('$filters: JSON');
    bindings.push('filters: $filters');
  }
 
  const params = {
    args: '',
    bindings: '',
  };
 
  if (!isEmpty(args)) {
    params.args = `(${args.join(',')})`;
    params.bindings = `(${bindings.join(',')})`;
  }
 
  if (source.params && source.params.pagination) {
    params.pagination = 'pagination { totalResults }';
  }
 
  return params;
};
 
const getQueryFields = (source) => {
  const list = map(source.schema, 'name');
  return list.join(' ');
};
 
const getQuery = (source) => {
  const { name } = source;
  const params = getQueryParams(source);
  const pagination = params.pagination || '';
 
  return `query ${name}${params.args} {
    ${name}${params.bindings} {
      items {
        ${getQueryFields(source)}
      }
      ${pagination}
    }
  }`;
};
 
const getSchemaTypeQuery = () => {
  const query = `query schemaType($name: String!) {
    __type(name: $name) {
      name
      kind
      fields {
        name
        args {
          name
          type {
            name
          }
        }
        type {
          name
          kind
          fields {
            name
            type {
              name
              ofType {
                name
                kind
              }
            }
          }
          ofType {
            name
            kind
            fields {
              name
              type {
                name
              }
            }
          }
        }
      }
    }
  }`;
 
  return query;
};
 
const getRootType = (response) => {
  const { data } = response;
  // eslint-disable-next-line
  return data.data.__type;
};
 
const getSourceModel = (source) => {
  Eif (source.type) {
    const items = find(source.type.fields, { name: 'items' });
    Eif (items) {
      return items.type.ofType.name;
    }
  }
 
  return null;
};
 
export default {
  getSources(connector, { savedOnly }) {
    Iif (savedOnly) return getSavedSources(connector);
 
    const connectorType = connector.type;
    const url = `${connectorType.options.endpoint}/${connectorType.name}`;
    return http.post(url, {
      query: getSchemaTypeQuery(),
      variables: {
        name: 'Query',
      },
    }, getCommonMeta(connector)).then((response) => {
      const data = getRootType(response);
      const sources = {};
 
      each(data.fields, (item) => {
        const model = getSourceModel(item);
 
        Eif (model) {
          const source = {
            name: item.name,
            model,
          };
 
          const params = {};
          Eif (item.args) {
            each(item.args, (n) => {
              params[n.name] = {
                name: n.name,
                type: n.type.name,
              };
            });
          }
 
          source.params = params;
          sources[item.name] = source;
        }
      });
 
      return sources;
    });
  },
  getSourceData(connector, source, options) {
    const connectorType = connector.type;
    const url = `${connectorType.options.endpoint}/${connectorType.name}`;
    return http.post(url, {
      query: getQuery(source),
      variables: {
        ...options.params,
        filters: source.filters,
      },
    }, getCommonMeta(connector)).then((response) => {
      const result = response.data.data;
      return result;
    });
  },
  getSourceSchema(connector, source) {
    const connectorType = connector.type;
    const url = `${connectorType.options.endpoint}/${connectorType.name}`;
    return http.post(url, {
      query: getSchemaTypeQuery(),
      variables: {
        name: source.model,
      },
    }, getCommonMeta(connector)).then((response) => {
      const data = getRootType(response);
      const schema = [];
 
      each(data.fields, (item) => {
        schema.push({
          name: item.name,
          type: item.type.name,
        });
      });
 
      return {
        name: data.name,
        schema,
      };
    });
  },
};