Skip to content

Commit af341db

Browse files
authored
Merge pull request #87 from semi-technologies/WEAVIATE-332-js-client
Add raw GraphQL ability to javascript client
2 parents d61a267 + 6bf2ff8 commit af341db

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

graphql/explorer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import NearVector from "./nearVector";
33
import NearObject from "./nearObject";
44
import NearImage from "./nearImage";
55
import Ask from "./ask";
6+
import Raw from "./raw";
67
import { DEFAULT_KIND, validateKind } from "../kinds";
78

89
export default class Explorer {

graphql/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import Aggregator from "./aggregator";
22
import Getter from "./getter";
33
import Explorer from "./explorer";
4+
import Raw from "./raw";
45

56
const graphql = (client) => {
67
return {
78
get: () => new Getter(client),
89
aggregate: () => new Aggregator(client),
910
explore: () => new Explorer(client),
11+
raw: () => new Raw(client),
1012
};
1113
};
1214

graphql/journey.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ describe("the graphql journey", () => {
1212
return setup(client);
1313
});
1414

15+
16+
test("graphql raw method", () => {
17+
return client.graphql
18+
.raw()
19+
.withQuery("{Get{Article{title url wordCount}}}")
20+
.do()
21+
.then(function (result) {
22+
expect(result.data.Get.Article.length).toEqual(3);
23+
});
24+
});
25+
26+
1527
test("graphql get method with minimal fields", () => {
1628
return client.graphql
1729
.get()

graphql/raw.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
export default class RawGraphQL {
2+
3+
constructor(client, quer) {
4+
this.client = client;
5+
this.errors = [];
6+
}
7+
8+
withQuery = (query) => {
9+
this.query = query;
10+
return this;
11+
};
12+
13+
14+
validateIsSet = (prop, name, setter) => {
15+
if (prop == undefined || prop == null || prop.length == 0) {
16+
this.errors = [
17+
...this.errors,
18+
`${name} must be set - set with ${setter}`,
19+
];
20+
}
21+
};
22+
23+
validate = () => {
24+
this.validateIsSet(
25+
this.query,
26+
"query",
27+
".raw().withQuery(query)"
28+
);
29+
};
30+
31+
do = () => {
32+
let params = "";
33+
34+
this.validate();
35+
if (this.errors.length > 0) {
36+
return Promise.reject(
37+
new Error("invalid usage: " + this.errors.join(", "))
38+
);
39+
}
40+
41+
if (this.query) {
42+
return this.client.query(this.query);
43+
};
44+
}
45+
}

graphql/raw.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Raw from "./raw";
2+
3+
test("a simple raw query", () => {
4+
const mockClient = {
5+
query: jest.fn(),
6+
};
7+
8+
const expectedQuery = `{Get{Person{name}}}`;
9+
10+
new Raw(mockClient).withQuery(expectedQuery).do();
11+
12+
expect(mockClient.query).toHaveBeenCalledWith(expectedQuery);
13+
});
14+
15+
test("reject empty raw query", () => {
16+
const mockClient = {
17+
query: jest.fn(),
18+
};
19+
20+
new Raw(mockClient, "").do().catch(err => {
21+
expect(err).toMatchObject(new Error("invalid usage: query must be set - set with .raw().withQuery(query)"));
22+
});
23+
});

0 commit comments

Comments
 (0)