• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FAIRsharing / fairsharing.github.io / 24769584990

22 Apr 2026 08:57AM UTC coverage: 95.892% (-4.1%) from 100.0%
24769584990

push

github

web-flow
Merge pull request #2746 from FAIRsharing/dev

Dev

3653 of 3813 branches covered (95.8%)

Branch coverage included in aggregate %.

17344 of 18229 new or added lines in 282 files covered. (95.15%)

766 existing lines in 50 files now uncovered.

38617 of 40268 relevant lines covered (95.9%)

5.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.8
/src/lib/GraphClient/GraphClient.js
1
import axios from "axios";
1✔
2
import Fragments from "./queries/fragments/fragments.json";
1✔
3

4
class GraphQLClient {
1✔
5
  /** The GraphQLClient retrieves data from the FAIRSharing API and sends it to the front-end.
1✔
6
   * Be careful, this is a singleton and trying to cast new instances will return the existing instance. Be
1✔
7
   * also careful, its constructor is async !!
1✔
8
   * @returns {Promise} - to use this object you need to do "await new ClassName()" or use .then(callback)
1✔
9
   */
1✔
10
  constructor() {
1✔
11
    this.initalizeHeader();
721✔
12
    if (GraphQLClient._instance) {
721✔
13
      return GraphQLClient._instance;
601✔
14
    }
601✔
15
    GraphQLClient._instance = this;
120✔
16
    this.url = import.meta.env.VITE_API_ENDPOINT + "/graphql";
120✔
17
  }
721✔
18

19
  /**
1✔
20
   * Execute the given query (coming from a json file, see /queries/getRecords.json)
1✔
21
   * @param {Object} query - the query coming from the JSON file
1✔
22
   * sending to the API.
1✔
23
   * @returns {Promise}
1✔
24
   */
1✔
25
  async executeQuery(query) {
1✔
26
    let client = this;
27✔
27
    let queryString = {
27✔
28
      query: `{${client.buildQuery(query)}}`,
27✔
29
    };
27✔
30
    let resp = await this.getData(queryString);
27✔
31
    if (resp.data.errors) {
27✔
32
      return resp.data.errors;
1✔
33
    }
1✔
34
    return resp.data.data;
26✔
35
  }
27✔
36

37
  /**
1✔
38
   * Takes the query, post it with axios and returns the raw data
1✔
39
   * @param {Object} queryString - processed request coming out of buildQuery() or a GraphQL query string
1✔
40
   * @returns {Promise} - an axios promise representing the server response.
1✔
41
   */
1✔
42
  async getData(queryString) {
1✔
UNCOV
43
    let client = this;
×
UNCOV
44
    const fullQuery = {
×
UNCOV
45
      method: "post",
×
UNCOV
46
      baseURL: client.url,
×
UNCOV
47
      data: queryString,
×
UNCOV
48
      headers: client.headers,
×
UNCOV
49
    };
×
UNCOV
50
    return axios(fullQuery);
×
UNCOV
51
  }
×
52

53
  /**
1✔
54
   * Transform the JSON query into a string for graphQL
1✔
55
   * @param {Object} query - the query coming from the JSON file
1✔
56
   * @returns {Object} {query: queryString} - a valid graphQL query string to execute
1✔
57
   */
1✔
58
  buildQuery(query) {
1✔
59
    let client = this;
157✔
60
    let queryString = `${query["queryName"]}`; // query name
157✔
61

62
    // Handle query parameters
157✔
63
    if (query.queryParam) {
157✔
64
      queryString += "(";
22✔
65
      Object.keys(query.queryParam).forEach(function (key) {
22✔
66
        if (
30✔
67
          typeof query.queryParam[key] === "boolean" ||
30✔
68
          typeof query.queryParam[key] === "number"
26✔
69
        ) {
30✔
70
          queryString += `${key}:${query.queryParam[key]} `;
6✔
71
        } else if (typeof query.queryParam[key] === "string") {
30✔
72
          //Modified to adjust multiple arguments in GraphQl query params
11✔
73
          const regExp = /\(|\)|\{|\}/g;
11✔
74

75
          const hasBrackets = regExp.test(query.queryParam[key]);
11✔
76

77
          if (hasBrackets) queryString += `${key}:${query.queryParam[key]}`;
11✔
78
          else queryString += `${key}:"${query.queryParam[key]}" `;
10✔
79
          // queryString += `${key}:"${query.queryParam[key]}" `;
11✔
80
        } else {
24✔
81
          let param = [];
13✔
82
          query.queryParam[key].forEach(function (paramVal) {
13✔
83
            typeof paramVal !== "number"
44✔
84
              ? param.push('"' + paramVal + '"')
44✔
85
              : param.push(paramVal);
44✔
86
          });
13✔
87
          queryString += `${key}:[${param.join(",")}]`;
13✔
88
        }
13✔
89
      });
22✔
90
      queryString += ")";
22✔
91
    }
22✔
92

93
    // Handle query fields
157✔
94
    if (query.fields) {
157✔
95
      queryString += "{";
156✔
96
      query.fields.forEach(function (field) {
156✔
97
        if (typeof field === "string") {
581✔
98
          queryString += ` ${field}`;
428✔
99
        }
428✔
100
        if (typeof field === "object") {
581✔
101
          if ("$ref" in field) {
153✔
102
            let myRef = Fragments[field["$ref"]];
57✔
103
            for (let subField of myRef) {
57✔
104
              if (typeof subField === "string") {
233✔
105
                queryString += ` ${subField}`;
220✔
106
              } else {
233✔
107
                queryString += ` ${client.buildQuery(subField)}`;
13✔
108
              }
13✔
109
            }
233✔
110
          } else {
153✔
111
            queryString += ` ${field.name}{`;
96✔
112
            field.fields.forEach(function (subField) {
96✔
113
              if (typeof subField === "string") {
512✔
114
                queryString += `${subField} `;
398✔
115
              } else {
512✔
116
                queryString += `${client.buildQuery(subField)}`;
114✔
117
              }
114✔
118
            });
96✔
119
            queryString += "}";
96✔
120
          }
96✔
121
        }
153✔
122
      });
156✔
123
      queryString += "}";
156✔
124
    }
156✔
125
    return queryString;
157✔
126
  }
157✔
127

128
  /**
1✔
129
   * Add the authorization token to the headers
1✔
130
   * @param {String} jwt - the user json web token
1✔
131
   */
1✔
132
  setHeader(jwt) {
1✔
133
    this.headers["Authorization"] = `Bearer ${jwt}`;
101✔
134
  }
101✔
135

136
  initalizeHeader() {
1✔
137
    this.headers = {
767✔
138
      Accept: "application/json",
767✔
139
      "Content-Type": "application/json",
767✔
140
    };
767✔
141
    this.headers["X-Client-Id"] = import.meta.env.VITE_CLIENT_ID;
767✔
142
    /* istanbul ignore if */
767✔
143
    if (this.headers["X-Client-Id"] === undefined) {
767✔
144
      delete this.headers["X-Client-Id"];
766✔
145
    }
766✔
146
  }
767✔
147
}
1✔
148

149
export default GraphQLClient;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc