Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add qualifiers #63

Merged
merged 5 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions __test__/integration/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ describe("Integration test", () => {
test("check response", async () => {
const query = new q(edges);
const res = await query.query(false);
const mydisease_res = await axios.get("http://mydisease.info/v1/disease/MONDO:0002494?fields=mondo.descendants&dotfield=true");
expect(res.length).toEqual(mydisease_res.data["mondo.descendants"].length)
const mydisease_res = await axios.get("http://mydisease.info/v1/disease/MONDO:0002494?fields=mondo.children&dotfield=true");
expect(res.length).toEqual(mydisease_res.data["mondo.children"].length)
})
})
})
204 changes: 104 additions & 100 deletions src/builder/trapi_query_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,116 +6,120 @@ const nunjucksConfig = require("./nunjucks_config");
const env = nunjucks.configure({ autoescape: false });
nunjucksConfig(env);
module.exports = class TRAPIQueryBuilder {
/**
* Constructor for Query Builder
* @param {object} APIEdge - BTE Edge object with input field provided
*/
constructor(APIEdge) {
this.start = 0
this.hasNext = false
this.APIEdge = APIEdge;
this.originalSubmitter;
}
/**
* Constructor for Query Builder
* @param {object} APIEdge - BTE Edge object with input field provided
*/
constructor(APIEdge) {
this.start = 0;
this.hasNext = false;
this.APIEdge = APIEdge;
this.originalSubmitter;
}

getUrl() {
return this.APIEdge.query_operation.server + this.APIEdge.query_operation.path;
}
getUrl() {
return this.APIEdge.query_operation.server + this.APIEdge.query_operation.path;
}

_getUrl(APIEdge, input) {
let server = APIEdge.query_operation.server;
if (server.endsWith('/')) {
server = server.substring(0, server.length - 1)
};
let path = APIEdge.query_operation.path;
if (Array.isArray(APIEdge.query_operation.path_params)) {
APIEdge.query_operation.path_params.map(param => {
const val = APIEdge.query_operation.params[param];
path = path.replace("{" + param + "}", val).replace("{inputs[0]}", input);
});
}
return server + path;
_getUrl(APIEdge, input) {
let server = APIEdge.query_operation.server;
if (server.endsWith("/")) {
server = server.substring(0, server.length - 1);
}

/**
* Construct input based on method and inputSeparator
*/
_getInput(APIEdge) {
return APIEdge.input;
let path = APIEdge.query_operation.path;
if (Array.isArray(APIEdge.query_operation.path_params)) {
APIEdge.query_operation.path_params.map(param => {
const val = APIEdge.query_operation.params[param];
path = path.replace("{" + param + "}", val).replace("{inputs[0]}", input);
});
}
return server + path;
}

addSubmitter(submitter) {
this.originalSubmitter = submitter;
}
/**
* Construct input based on method and inputSeparator
*/
_getInput(APIEdge) {
return APIEdge.input;
}

/**
* Construct TRAPI request body
*/
_getRequestBody(APIEdge, input) {
const qg = {
"message": {
"query_graph": {
"nodes": {
"n0": {
"ids": Array.isArray(input) ? input : [input],
"categories": ["biolink:" + APIEdge.association.input_type]
},
"n1": {
"categories": ["biolink:" + APIEdge.association.output_type]
}
},
"edges": {
"e01": {
"subject": "n0",
"object": "n1",
"predicates": ["biolink:" + APIEdge.association.predicate]
}
}
}
},
"submitter": "infores:bte"
};
const xmaturityMap = {
ci: "staging",
test: "test",
prod: "prod",
dev: "dev",
}
if (process.env.INSTANCE_ENV) qg.submitter += `; bte-${xmaturityMap[process.env.INSTANCE_ENV]}`;
if (this.originalSubmitter) qg.submitter += `; subquery for client "${this.originalSubmitter}"`;
return qg;
}
addSubmitter(submitter) {
this.originalSubmitter = submitter;
}

/**
* Construct the request config for Axios reqeust.
*/
constructAxiosRequestConfig() {
const input = this._getInput(this.APIEdge);
const config = {
url: this._getUrl(this.APIEdge, input),
data: this._getRequestBody(this.APIEdge, input),
method: this.APIEdge.query_operation.method,
headers: {
'Content-Type': 'application/json'
/**
* Construct TRAPI request body
*/
_getRequestBody(APIEdge, input) {
const qg = {
message: {
query_graph: {
nodes: {
n0: {
ids: Array.isArray(input) ? input : [input],
categories: ["biolink:" + APIEdge.association.input_type],
},
}
this.config = config;
return config;
n1: {
categories: ["biolink:" + APIEdge.association.output_type],
},
},
edges: {
e01: {
subject: "n0",
object: "n1",
predicates: ["biolink:" + APIEdge.association.predicate],
},
},
},
},
submitter: "infores:bte",
};
const qualifierConstraints = APIEdge.reasoner_edge?.getQualifierConstraints?.();
if (qualifierConstraints) {
qg.message.query_graph.edges.e01.qualifier_constraints = qualifierConstraints;
}
const xmaturityMap = {
ci: "staging",
test: "test",
prod: "prod",
dev: "dev",
};
if (process.env.INSTANCE_ENV) qg.submitter += `; bte-${xmaturityMap[process.env.INSTANCE_ENV]}`;
if (this.originalSubmitter) qg.submitter += `; subquery for client "${this.originalSubmitter}"`;
return qg;
}

needPagination(apiResponse) {
this.hasNext = false;
return false
}
/**
* Construct the request config for Axios reqeust.
*/
constructAxiosRequestConfig() {
const input = this._getInput(this.APIEdge);
const config = {
url: this._getUrl(this.APIEdge, input),
data: this._getRequestBody(this.APIEdge, input),
method: this.APIEdge.query_operation.method,
headers: {
"Content-Type": "application/json",
},
};
this.config = config;
return config;
}

getNext() {
const config = this.constructAxiosRequestConfig(this.APIEdge);
return config;
}
needPagination(apiResponse) {
this.hasNext = false;
return false;
}

getNext() {
const config = this.constructAxiosRequestConfig(this.APIEdge);
return config;
}

getConfig() {
if (this.hasNext === false) {
return this.constructAxiosRequestConfig(this.APIEdge);
}
return this.getNext();
getConfig() {
if (this.hasNext === false) {
return this.constructAxiosRequestConfig(this.APIEdge);
}
}
return this.getNext();
}
};