@snowtop/ent 0.2.0-alpha.5 → 0.2.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import { EdgeQuery, PaginationInfo } from "../../core/query/query";
|
|
2
1
|
import { Data, Ent, ID, Viewer } from "../../core/base";
|
|
2
|
+
import { EdgeQuery, PaginationInfo } from "../../core/query/query";
|
|
3
3
|
export interface GraphQLEdge<T extends Data> {
|
|
4
4
|
edge: T;
|
|
5
5
|
node: Ent;
|
|
6
6
|
cursor: string;
|
|
7
7
|
}
|
|
8
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
8
9
|
interface edgeQueryCtr<T extends Ent, TEdge extends Data, TViewer extends Viewer> {
|
|
9
|
-
(v: TViewer, src: T): EdgeQuery<T, Ent, TEdge
|
|
10
|
+
(v: TViewer, src: T): MaybePromise<EdgeQuery<T, Ent, TEdge>>;
|
|
10
11
|
}
|
|
11
12
|
interface edgeQueryCtr2<T extends Ent, TEdge extends Data, TViewer extends Viewer> {
|
|
12
|
-
(v: TViewer): EdgeQuery<T, Ent, TEdge
|
|
13
|
+
(v: TViewer): MaybePromise<EdgeQuery<T, Ent, TEdge>>;
|
|
13
14
|
}
|
|
14
15
|
export declare class GraphQLEdgeConnection<TSource extends Ent, TEdge extends Data, TViewer extends Viewer = Viewer> {
|
|
15
|
-
query: EdgeQuery<TSource, Ent, TEdge
|
|
16
|
+
query: Promise<EdgeQuery<TSource, Ent, TEdge>>;
|
|
16
17
|
private results;
|
|
17
18
|
private viewer;
|
|
18
19
|
private source?;
|
|
@@ -7,13 +7,13 @@ class GraphQLEdgeConnection {
|
|
|
7
7
|
this.results = [];
|
|
8
8
|
this.viewer = viewer;
|
|
9
9
|
if (typeof arg2 === "function") {
|
|
10
|
-
this.query = arg2(this.viewer);
|
|
10
|
+
this.query = Promise.resolve(arg2(this.viewer));
|
|
11
11
|
}
|
|
12
12
|
else {
|
|
13
13
|
this.source = arg2;
|
|
14
14
|
}
|
|
15
15
|
if (typeof arg3 === "function") {
|
|
16
|
-
this.query = arg3(this.viewer, this.source);
|
|
16
|
+
this.query = Promise.resolve(arg3(this.viewer, this.source));
|
|
17
17
|
}
|
|
18
18
|
else {
|
|
19
19
|
this.args = arg3;
|
|
@@ -28,28 +28,31 @@ class GraphQLEdgeConnection {
|
|
|
28
28
|
if (this.args.before && !this.args.before) {
|
|
29
29
|
throw new Error("cannot process before without last");
|
|
30
30
|
}
|
|
31
|
+
const argFirst = this.args.first;
|
|
32
|
+
const argLast = this.args.last;
|
|
33
|
+
const argCursor = this.args.cursor;
|
|
31
34
|
if (this.args.first) {
|
|
32
|
-
this.query = this.query.
|
|
35
|
+
this.query = this.query.then((query) => query.first(argFirst, argLast));
|
|
33
36
|
}
|
|
34
37
|
if (this.args.last) {
|
|
35
|
-
this.query = this.query.
|
|
38
|
+
this.query = this.query.then((query) => query.last(argLast, argCursor));
|
|
36
39
|
}
|
|
37
40
|
// TODO custom args
|
|
38
41
|
// how to proceed
|
|
39
42
|
}
|
|
40
43
|
}
|
|
41
44
|
first(limit, cursor) {
|
|
42
|
-
this.query = this.query.first(limit, cursor);
|
|
45
|
+
this.query = this.query.then((query) => query.first(limit, cursor));
|
|
43
46
|
}
|
|
44
47
|
last(limit, cursor) {
|
|
45
|
-
this.query = this.query.last(limit, cursor);
|
|
48
|
+
this.query = this.query.then((query) => query.last(limit, cursor));
|
|
46
49
|
}
|
|
47
50
|
// any custom filters can be applied here...
|
|
48
51
|
modifyQuery(fn) {
|
|
49
|
-
this.query =
|
|
52
|
+
this.query = this.query.then((query) => fn(query));
|
|
50
53
|
}
|
|
51
54
|
async queryTotalCount() {
|
|
52
|
-
return this.query.queryRawCount();
|
|
55
|
+
return (await this.query).queryRawCount();
|
|
53
56
|
}
|
|
54
57
|
async queryEdges() {
|
|
55
58
|
// because of privacy, we need to query the node regardless of if the node is there
|
|
@@ -60,7 +63,7 @@ class GraphQLEdgeConnection {
|
|
|
60
63
|
// if nodes queried just return ents
|
|
61
64
|
// unlikely to query nodes and pageInfo so we just load this separately for now
|
|
62
65
|
async queryNodes() {
|
|
63
|
-
return this.query.queryEnts();
|
|
66
|
+
return (await this.query).queryEnts();
|
|
64
67
|
}
|
|
65
68
|
defaultPageInfo() {
|
|
66
69
|
return {
|
|
@@ -72,7 +75,7 @@ class GraphQLEdgeConnection {
|
|
|
72
75
|
}
|
|
73
76
|
async queryPageInfo() {
|
|
74
77
|
await this.queryData();
|
|
75
|
-
const paginationInfo = this.query.paginationInfo();
|
|
78
|
+
const paginationInfo = (await this.query).paginationInfo();
|
|
76
79
|
if (this.source !== undefined) {
|
|
77
80
|
return paginationInfo.get(this.source.id) || this.defaultPageInfo();
|
|
78
81
|
}
|
|
@@ -85,24 +88,25 @@ class GraphQLEdgeConnection {
|
|
|
85
88
|
return this.defaultPageInfo();
|
|
86
89
|
}
|
|
87
90
|
async queryData() {
|
|
91
|
+
const query = await this.query;
|
|
88
92
|
const [edges, ents] = await Promise.all([
|
|
89
93
|
// TODO need a test that this will only fetch edges once
|
|
90
94
|
// and then fetch ents afterward
|
|
91
|
-
|
|
92
|
-
|
|
95
|
+
query.queryEdges(),
|
|
96
|
+
query.queryEnts(),
|
|
93
97
|
]);
|
|
94
98
|
let entsMap = new Map();
|
|
95
99
|
ents.forEach((ent) => entsMap.set(ent.id, ent));
|
|
96
100
|
let results = [];
|
|
97
101
|
for (const edge of edges) {
|
|
98
|
-
const node = entsMap.get(
|
|
102
|
+
const node = entsMap.get(query.dataToID(edge));
|
|
99
103
|
if (!node) {
|
|
100
104
|
continue;
|
|
101
105
|
}
|
|
102
106
|
results.push({
|
|
103
107
|
edge,
|
|
104
108
|
node,
|
|
105
|
-
cursor:
|
|
109
|
+
cursor: query.getCursor(edge),
|
|
106
110
|
});
|
|
107
111
|
}
|
|
108
112
|
this.results = results;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { FakeContact } from "../../testutils/fake_data/index";
|
|
1
|
+
import { Data, Ent, Viewer } from "../../core/base";
|
|
3
2
|
import { EdgeQuery } from "../../core/query/query";
|
|
3
|
+
import { FakeContact } from "../../testutils/fake_data/index";
|
|
4
4
|
interface options<TEnt extends Ent, TEdge extends Data> {
|
|
5
5
|
getQuery: (v: Viewer, src: Ent) => EdgeQuery<TEnt, Ent, TEdge>;
|
|
6
6
|
tableName: string;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.commonTests = void 0;
|
|
4
|
-
const viewer_1 = require("../../core/viewer");
|
|
5
4
|
const ent_1 = require("../../core/ent");
|
|
6
|
-
const
|
|
5
|
+
const viewer_1 = require("../../core/viewer");
|
|
7
6
|
const index_1 = require("../../testutils/fake_data/index");
|
|
8
7
|
const test_helpers_1 = require("../../testutils/fake_data/test_helpers");
|
|
8
|
+
const edge_connection_1 = require("./edge_connection");
|
|
9
9
|
class TestConnection {
|
|
10
10
|
constructor(getQuery, ents, filter) {
|
|
11
11
|
this.getQuery = getQuery;
|
|
@@ -18,10 +18,20 @@ class TestConnection {
|
|
|
18
18
|
this.allContacts = this.allContacts.reverse();
|
|
19
19
|
this.conn = new edge_connection_1.GraphQLEdgeConnection(new viewer_1.IDViewer(this.user.id), this.user, (v, user) => this.getQuery(v, user));
|
|
20
20
|
if (this.filter) {
|
|
21
|
-
this.filter(this.conn, this.user, this.allContacts);
|
|
21
|
+
await this.filter(this.conn, this.user, this.allContacts);
|
|
22
22
|
}
|
|
23
23
|
this.filteredContacts = this.ents(this.allContacts);
|
|
24
24
|
}
|
|
25
|
+
async testAsyncConn() {
|
|
26
|
+
const asyncConn = new edge_connection_1.GraphQLEdgeConnection(new viewer_1.IDViewer(this.user.id), this.user, (v, user) => new Promise((resolve) => {
|
|
27
|
+
setTimeout(() => resolve(this.getQuery(v, user)), 0);
|
|
28
|
+
}));
|
|
29
|
+
if (this.filter) {
|
|
30
|
+
await this.filter(asyncConn, this.user, this.allContacts);
|
|
31
|
+
}
|
|
32
|
+
const count = await asyncConn.queryTotalCount();
|
|
33
|
+
expect(count).toBe(test_helpers_1.inputs.length);
|
|
34
|
+
}
|
|
25
35
|
async testTotalCount() {
|
|
26
36
|
const count = await this.conn.queryTotalCount();
|
|
27
37
|
expect(count).toBe(test_helpers_1.inputs.length);
|
|
@@ -39,7 +49,7 @@ class TestConnection {
|
|
|
39
49
|
for (let i = 0; i < this.filteredContacts.length; i++) {
|
|
40
50
|
const edge = edges[i];
|
|
41
51
|
expect(edge.node.id).toBe(this.filteredContacts[i].id);
|
|
42
|
-
expect(this.conn.query.dataToID(edge.edge)).toBe(this.filteredContacts[i].id);
|
|
52
|
+
expect((await this.conn.query).dataToID(edge.edge)).toBe(this.filteredContacts[i].id);
|
|
43
53
|
}
|
|
44
54
|
}
|
|
45
55
|
}
|
|
@@ -92,7 +102,7 @@ const commonTests = (opts) => {
|
|
|
92
102
|
});
|
|
93
103
|
});
|
|
94
104
|
describe("filters. firstN", () => {
|
|
95
|
-
const filter = new TestConnection((v, user) => opts.getQuery(v, user), (contacts) => contacts.slice(0, 2), (conn) => {
|
|
105
|
+
const filter = new TestConnection((v, user) => opts.getQuery(v, user), (contacts) => contacts.slice(0, 2), async (conn) => {
|
|
96
106
|
conn.first(2);
|
|
97
107
|
});
|
|
98
108
|
beforeEach(async () => {
|
|
@@ -124,8 +134,8 @@ const commonTests = (opts) => {
|
|
|
124
134
|
const N = 3;
|
|
125
135
|
const filter = new TestConnection((v, user) => opts.getQuery(v, user),
|
|
126
136
|
// get the next 2
|
|
127
|
-
(contacts) => contacts.slice(idx + 1, idx + N), (conn, user, contacts) => {
|
|
128
|
-
const cursor = getCursorFrom(conn.query, contacts, idx);
|
|
137
|
+
(contacts) => contacts.slice(idx + 1, idx + N), async (conn, user, contacts) => {
|
|
138
|
+
const cursor = getCursorFrom(await conn.query, contacts, idx);
|
|
129
139
|
conn.first(2, cursor);
|
|
130
140
|
});
|
|
131
141
|
beforeEach(async () => {
|
|
@@ -153,9 +163,9 @@ const commonTests = (opts) => {
|
|
|
153
163
|
});
|
|
154
164
|
});
|
|
155
165
|
describe("filters. before cursor", () => {
|
|
156
|
-
const filter = new TestConnection((v, user) => opts.getQuery(v, user), (contacts) => contacts.slice(2, 4).reverse(), (conn, user, contacts) => {
|
|
166
|
+
const filter = new TestConnection((v, user) => opts.getQuery(v, user), (contacts) => contacts.slice(2, 4).reverse(), async (conn, user, contacts) => {
|
|
157
167
|
// get the 2 before it
|
|
158
|
-
const cursor = getCursorFrom(conn.query, contacts, 4);
|
|
168
|
+
const cursor = getCursorFrom(await conn.query, contacts, 4);
|
|
159
169
|
conn.last(2, cursor);
|
|
160
170
|
});
|
|
161
171
|
beforeEach(async () => {
|