|
| 1 | +<!DOCTYPE html> |
| 2 | + |
| 3 | +<html lang="en"> |
| 4 | +<head> |
| 5 | + <meta charset="utf-8"> |
| 6 | + <title>FAIRsharing.org Source: /home/runner/work/FAIRsharing-Assistant/FAIRsharing-Assistant/src/lib/GraphClient/GraphClient.js</title> |
| 7 | + |
| 8 | + <!--[if lt IE 9]> |
| 9 | + <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> |
| 10 | + <![endif]--> |
| 11 | + <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css"> |
| 12 | + |
| 13 | + <link type="text/css" rel="stylesheet" href="styles/site.yeti.css"> |
| 14 | + |
| 15 | +</head> |
| 16 | + |
| 17 | +<body> |
| 18 | + |
| 19 | +<div class="navbar navbar-default navbar-fixed-top navbar-inverse"> |
| 20 | +<div class="container"> |
| 21 | + <div class="navbar-header"> |
| 22 | + <a class="navbar-brand" href="index.html">FAIRsharing.org</a> |
| 23 | + </div> |
| 24 | + <div class="navbar-collapse"> |
| 25 | + <ul class="nav navbar-nav"> |
| 26 | + |
| 27 | + <li class="dropdown"> |
| 28 | + <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a> |
| 29 | + <ul class="dropdown-menu "> |
| 30 | + <li><a href="GraphQLClient.html">GraphQLClient</a></li> |
| 31 | + </ul> |
| 32 | + </li> |
| 33 | + |
| 34 | + </ul> |
| 35 | + </div> |
| 36 | +</div> |
| 37 | +</div> |
| 38 | + |
| 39 | + |
| 40 | +<div class="container"> |
| 41 | +<div class="row"> |
| 42 | + |
| 43 | + |
| 44 | + <div class="col-md-12"> |
| 45 | + |
| 46 | + <div id="main"> |
| 47 | + |
| 48 | + |
| 49 | + <h1 class="page-title">Source: /home/runner/work/FAIRsharing-Assistant/FAIRsharing-Assistant/src/lib/GraphClient/GraphClient.js</h1> |
| 50 | + |
| 51 | +<section> |
| 52 | + <article> |
| 53 | + <pre |
| 54 | + class="sunlight-highlight-javascript linenums">const axios = require("axios"); |
| 55 | +import Fragments from "./queries/fragments/fragments.json" |
| 56 | + |
| 57 | +/* istanbul ignore next */ |
| 58 | +class GraphQLClient { |
| 59 | + |
| 60 | + /** The GraphQLClient retrieves data from the FAIRSharing API and sends it to the front-end. |
| 61 | + * Be careful, this is a singleton and trying to cast new instances will return the existing instance. Be |
| 62 | + * also careful, its constructor is async !! |
| 63 | + * @returns {Promise} - to use this object you need to do "await new ClassName()" or use .then(callback) |
| 64 | + */ |
| 65 | + constructor(){ |
| 66 | + this.initalizeHeader(); |
| 67 | + if (GraphQLClient._instance){ |
| 68 | + return GraphQLClient._instance |
| 69 | + } |
| 70 | + GraphQLClient._instance = this; |
| 71 | + this.url = process.env.VUE_APP_API_ENDPOINT + "/graphql"; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Execute the given query (coming from a json file, see /queries/getRecords.json) |
| 76 | + * @param {Object} query - the query coming from the JSON file |
| 77 | + * sending to the API. |
| 78 | + * @returns {Promise} |
| 79 | + */ |
| 80 | + async executeQuery(query){ |
| 81 | + let client = this; |
| 82 | + let queryString = { |
| 83 | + query: `{${client.buildQuery(query)}}` |
| 84 | + }; |
| 85 | + let resp = await this.getData(queryString); |
| 86 | + if (resp.data.errors) { |
| 87 | + return resp.data.errors; |
| 88 | + } |
| 89 | + return resp.data.data |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Takes the query, post it with axios and returns the raw data |
| 94 | + * @param {Object} queryString - processed request coming out of buildQuery() or a GraphQL query string |
| 95 | + * @returns {Promise} - an axios promise representing the server response. |
| 96 | + */ |
| 97 | + async getData(queryString){ |
| 98 | + let client = this; |
| 99 | + const fullQuery = { |
| 100 | + method: "post", |
| 101 | + baseURL: client.url, |
| 102 | + data: queryString, |
| 103 | + headers: client.headers |
| 104 | + }; |
| 105 | + return axios(fullQuery); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Transform the JSON query into a string for graphQL |
| 110 | + * @param {Object} query - the query coming from the JSON file |
| 111 | + * @returns {Object} {query: queryString} - a valid graphQL query string to execute |
| 112 | + */ |
| 113 | + buildQuery(query){ |
| 114 | + let client = this; |
| 115 | + let queryString = `${query["queryName"]}`; // query name |
| 116 | + |
| 117 | + // Handle query parameters |
| 118 | + if (query.queryParam) { |
| 119 | + queryString += "("; |
| 120 | + Object.keys(query.queryParam).forEach(function(key){ |
| 121 | + if (typeof query.queryParam[key] === "boolean" || typeof query.queryParam[key] === "number"){ |
| 122 | + queryString += `${key}:${query.queryParam[key]} `; |
| 123 | + } |
| 124 | + else if (typeof query.queryParam[key] === "string") { |
| 125 | + queryString += `${key}:"${query.queryParam[key]}" `; |
| 126 | + } |
| 127 | + else { |
| 128 | + let param = []; |
| 129 | + query.queryParam[key].forEach(function(paramVal){ |
| 130 | + typeof paramVal !== "number" ? param.push("\"" + paramVal + "\"") : param.push(paramVal); |
| 131 | + }); |
| 132 | + queryString += `${key}:[${param.join(",")}]`; |
| 133 | + } |
| 134 | + }); |
| 135 | + queryString += ")"; |
| 136 | + } |
| 137 | + |
| 138 | + // Handle query fields |
| 139 | + if (query.fields){ |
| 140 | + queryString += "{"; |
| 141 | + query.fields.forEach(function(field){ |
| 142 | + if (typeof field === "string"){ |
| 143 | + queryString += ` ${field}`; |
| 144 | + } |
| 145 | + if (typeof field === "object"){ |
| 146 | + if ("$ref" in field){ |
| 147 | + let myRef = Fragments[field["$ref"]]; |
| 148 | + for (let subField of myRef){ |
| 149 | + if (typeof subField === "string"){ |
| 150 | + queryString += ` ${subField}`; |
| 151 | + } |
| 152 | + else { |
| 153 | + queryString += ` ${client.buildQuery(subField)}`; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + else { |
| 158 | + queryString += ` ${field.name}{`; |
| 159 | + field.fields.forEach(function(subField){ |
| 160 | + if (typeof subField === "string"){ |
| 161 | + queryString += `${subField} `; |
| 162 | + } |
| 163 | + else { |
| 164 | + queryString += `${client.buildQuery(subField)}`; |
| 165 | + } |
| 166 | + }); |
| 167 | + queryString += "}"; |
| 168 | + } |
| 169 | + } |
| 170 | + }); |
| 171 | + queryString += "}"; |
| 172 | + } |
| 173 | + return queryString; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Add the authorization token to the headers |
| 178 | + * @param {String} jwt - the user json web token |
| 179 | + */ |
| 180 | + setHeader(jwt){ |
| 181 | + this.headers['Authorization'] = `Bearer ${jwt}`; |
| 182 | + } |
| 183 | + |
| 184 | + initalizeHeader(){ |
| 185 | + this.headers = { |
| 186 | + "Accept": "application/json", |
| 187 | + "Content-Type": "application/json", |
| 188 | + }; |
| 189 | + this.headers['X-Client-Id'] = process.env.VUE_APP_CLIENT_ID; |
| 190 | + /* istanbul ignore if */ |
| 191 | + if (this.headers['X-Client-Id'] === undefined){ |
| 192 | + delete this.headers['X-Client-Id'] |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | +} |
| 198 | + |
| 199 | +export default GraphQLClient; |
| 200 | +</pre> |
| 201 | + </article> |
| 202 | +</section> |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + </div> |
| 209 | + </div> |
| 210 | + |
| 211 | + <div class="clearfix"></div> |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | +</div> |
| 216 | +</div> |
| 217 | + |
| 218 | + |
| 219 | +<footer> |
| 220 | + |
| 221 | + |
| 222 | + <span class="copyright"> |
| 223 | + DocStrap Copyright © 2012-2015 The contributors to the JSDoc3 and DocStrap projects. |
| 224 | + </span> |
| 225 | + |
| 226 | +<span class="jsdoc-message"> |
| 227 | + Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> |
| 228 | + on Wed Aug 16th 2023 using the <a |
| 229 | + href="https://github.com/docstrap/docstrap">DocStrap template</a>. |
| 230 | +</span> |
| 231 | +</footer> |
| 232 | + |
| 233 | +<!--<script src="scripts/sunlight.js"></script>--> |
| 234 | +<script src="scripts/docstrap.lib.js"></script> |
| 235 | +<script src="scripts/bootstrap-dropdown.js"></script> |
| 236 | +<script src="scripts/toc.js"></script> |
| 237 | + |
| 238 | +<script> |
| 239 | +$( function () { |
| 240 | + $( "[id*='$']" ).each( function () { |
| 241 | + var $this = $( this ); |
| 242 | + |
| 243 | + $this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) ); |
| 244 | + } ); |
| 245 | + |
| 246 | + $( ".tutorial-section pre, .readme-section pre" ).each( function () { |
| 247 | + var $this = $( this ); |
| 248 | + |
| 249 | + var example = $this.find( "code" ); |
| 250 | + exampleText = example.html(); |
| 251 | + var lang = /{@lang (.*?)}/.exec( exampleText ); |
| 252 | + if ( lang && lang[1] ) { |
| 253 | + exampleText = exampleText.replace( lang[0], "" ); |
| 254 | + example.html( exampleText ); |
| 255 | + lang = lang[1]; |
| 256 | + } else { |
| 257 | + var langClassMatch = example.parent()[0].className.match(/lang\-(\S+)/); |
| 258 | + lang = langClassMatch ? langClassMatch[1] : "javascript"; |
| 259 | + } |
| 260 | + |
| 261 | + if ( lang ) { |
| 262 | + |
| 263 | + $this |
| 264 | + .addClass( "sunlight-highlight-" + lang ) |
| 265 | + .addClass( "linenums" ) |
| 266 | + .html( example.html() ); |
| 267 | + |
| 268 | + } |
| 269 | + } ); |
| 270 | + |
| 271 | + Sunlight.highlightAll( { |
| 272 | + lineNumbers : true, |
| 273 | + showMenu : true, |
| 274 | + enableDoclinks : true |
| 275 | + } ); |
| 276 | + |
| 277 | + $( "#toc" ).toc( { |
| 278 | + anchorName : function ( i, heading, prefix ) { |
| 279 | + var id = $( heading ).attr( "id" ); |
| 280 | + return id && id.replace(/\~/g, '-inner-').replace(/\./g, '-static-') || ( prefix + i ); |
| 281 | + }, |
| 282 | + selectors : "h1,h2,h3,h4", |
| 283 | + showAndHide : false, |
| 284 | + navbarOffset: 10, |
| 285 | + smoothScrolling: true |
| 286 | + } ); |
| 287 | + |
| 288 | + $( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" ); |
| 289 | + $( "#main span[id^='toc']" ).addClass( "toc-shim" ); |
| 290 | + $( '.dropdown-toggle' ).dropdown(); |
| 291 | + // $( ".tutorial-section pre, .readme-section pre" ).addClass( "sunlight-highlight-javascript" ).addClass( "linenums" ); |
| 292 | + |
| 293 | + $( "table" ).each( function () { |
| 294 | + var $this = $( this ); |
| 295 | + $this.addClass('table'); |
| 296 | + } ); |
| 297 | + |
| 298 | +} ); |
| 299 | +</script> |
| 300 | + |
| 301 | + |
| 302 | + |
| 303 | +<!--Navigation and Symbol Display--> |
| 304 | + |
| 305 | + |
| 306 | +<!--Google Analytics--> |
| 307 | + |
| 308 | + |
| 309 | +</body> |
| 310 | +</html> |
0 commit comments