@graphql-mesh/transport-mongo 0.0.0-stage → 0.1.0
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.
- package/CHANGELOG.md +8 -0
- package/dist/cjs/client.js +88 -0
- package/dist/cjs/execution.js +239 -0
- package/dist/cjs/filter.js +178 -0
- package/dist/cjs/index.js +16 -0
- package/dist/cjs/mapping.js +328 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types.js +0 -0
- package/dist/esm/client.js +85 -0
- package/dist/esm/execution.js +236 -0
- package/dist/esm/filter.js +168 -0
- package/dist/esm/index.js +13 -0
- package/dist/esm/mapping.js +321 -0
- package/dist/esm/types.js +0 -0
- package/dist/package.json +49 -0
- package/dist/typings/client.d.cts +5 -0
- package/dist/typings/client.d.ts +5 -0
- package/dist/typings/execution.d.cts +9 -0
- package/dist/typings/execution.d.ts +9 -0
- package/dist/typings/filter.d.cts +24 -0
- package/dist/typings/filter.d.ts +24 -0
- package/dist/typings/index.d.cts +10 -0
- package/dist/typings/index.d.ts +10 -0
- package/dist/typings/mapping.d.cts +19 -0
- package/dist/typings/mapping.d.ts +19 -0
- package/dist/typings/types.d.cts +53 -0
- package/dist/typings/types.d.ts +53 -0
- package/package.json +55 -4
- package/src/client.ts +93 -0
- package/src/execution.ts +323 -0
- package/src/filter.ts +190 -0
- package/src/index.ts +18 -0
- package/src/mapping.ts +348 -0
- package/src/types.ts +86 -0
- package/test/execution.test.ts +111 -0
- package/test/filter.test.ts +67 -0
- package/test/mapping.test.ts +93 -0
- package/README.md +0 -3
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { parse } from 'graphql';
|
|
2
|
+
import { loadGraphQLSchemaFromMongo } from '@omnigraph/mongo';
|
|
3
|
+
import { Schema } from 'mongoose';
|
|
4
|
+
import { getMongoExecutor } from '../src/execution.js';
|
|
5
|
+
import type { MongoClientLike } from '../src/types.js';
|
|
6
|
+
|
|
7
|
+
describe('getMongoExecutor FIND_* + writes', () => {
|
|
8
|
+
it('runs find/count/create via @mongo directives', async () => {
|
|
9
|
+
const userSchema = new Schema(
|
|
10
|
+
{
|
|
11
|
+
name: { type: String, index: true },
|
|
12
|
+
age: { type: Number, index: true },
|
|
13
|
+
gender: { type: String, enum: ['male', 'female', 'not-specified'] },
|
|
14
|
+
},
|
|
15
|
+
{ collection: 'user_users' },
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
const schema = await loadGraphQLSchemaFromMongo('users', {
|
|
19
|
+
endpoint: 'mongodb://localhost:27017/test',
|
|
20
|
+
models: [{ name: 'User', schema: userSchema, collection: 'user_users' }],
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const adaId = '507f1f77bcf86cd799439011';
|
|
24
|
+
const alanId = '507f1f77bcf86cd799439012';
|
|
25
|
+
const docs: Array<Record<string, unknown>> = [
|
|
26
|
+
{ _id: adaId, name: 'Ada', age: 36, gender: 'female' },
|
|
27
|
+
{ _id: alanId, name: 'Alan', age: 41, gender: 'male' },
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
const client: MongoClientLike = {
|
|
31
|
+
async findOne(_c, filter) {
|
|
32
|
+
return docs.find(d => String(d._id) === String((filter as any)._id)) || null;
|
|
33
|
+
},
|
|
34
|
+
async findMany(_c, filter, options) {
|
|
35
|
+
let rows = docs.slice();
|
|
36
|
+
if ((filter as any).name) rows = rows.filter(d => d.name === (filter as any).name);
|
|
37
|
+
if ((filter as any)._id?.$in) {
|
|
38
|
+
const ids = (filter as any)._id.$in.map(String);
|
|
39
|
+
rows = rows.filter(d => ids.includes(String(d._id)));
|
|
40
|
+
}
|
|
41
|
+
if (options?.limit != null) rows = rows.slice(0, options.limit);
|
|
42
|
+
return rows;
|
|
43
|
+
},
|
|
44
|
+
async count(_c, filter) {
|
|
45
|
+
if ((filter as any).gender) {
|
|
46
|
+
return docs.filter(d => d.gender === (filter as any).gender).length;
|
|
47
|
+
}
|
|
48
|
+
return docs.length;
|
|
49
|
+
},
|
|
50
|
+
async insertOne(_c, doc) {
|
|
51
|
+
const insertedId = '507f1f77bcf86cd799439099';
|
|
52
|
+
docs.push({ ...doc, _id: insertedId });
|
|
53
|
+
return { insertedId };
|
|
54
|
+
},
|
|
55
|
+
async insertMany() {
|
|
56
|
+
return { insertedIds: [], insertedCount: 0 };
|
|
57
|
+
},
|
|
58
|
+
async updateOne() {
|
|
59
|
+
return { matchedCount: 1, modifiedCount: 1 };
|
|
60
|
+
},
|
|
61
|
+
async updateMany() {
|
|
62
|
+
return { matchedCount: 0, modifiedCount: 0 };
|
|
63
|
+
},
|
|
64
|
+
async deleteOne() {
|
|
65
|
+
return { deletedCount: 1 };
|
|
66
|
+
},
|
|
67
|
+
async deleteMany() {
|
|
68
|
+
return { deletedCount: 0 };
|
|
69
|
+
},
|
|
70
|
+
async aggregate() {
|
|
71
|
+
return [];
|
|
72
|
+
},
|
|
73
|
+
dispose() {},
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const executor = getMongoExecutor({ subgraph: schema, client });
|
|
77
|
+
|
|
78
|
+
const byId = await executor({
|
|
79
|
+
document: parse(`query { User_findById(_id: "${adaId}") { id name age } }`),
|
|
80
|
+
});
|
|
81
|
+
expect(byId.errors).toBeUndefined();
|
|
82
|
+
expect(byId.data).toEqual({
|
|
83
|
+
User_findById: { id: adaId, name: 'Ada', age: 36 },
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const many = await executor({
|
|
87
|
+
document: parse(`query { User_findMany(filter: { name: { eq: "Alan" } }) { id name } }`),
|
|
88
|
+
});
|
|
89
|
+
expect(many.data).toEqual({
|
|
90
|
+
User_findMany: [{ id: alanId, name: 'Alan' }],
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const count = await executor({
|
|
94
|
+
document: parse(`query { User_count(filter: { gender: { eq: "female" } }) }`),
|
|
95
|
+
});
|
|
96
|
+
expect(count.data).toEqual({ User_count: 1 });
|
|
97
|
+
|
|
98
|
+
const created = await executor({
|
|
99
|
+
document: parse(
|
|
100
|
+
`mutation { User_createOne(record: { name: "Grace", age: 45 }) { recordId record { name } } }`,
|
|
101
|
+
),
|
|
102
|
+
});
|
|
103
|
+
expect(created.errors).toBeUndefined();
|
|
104
|
+
expect(created.data).toEqual({
|
|
105
|
+
User_createOne: {
|
|
106
|
+
recordId: '507f1f77bcf86cd799439099',
|
|
107
|
+
record: { name: 'Grace' },
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { buildMongoFilter, graphqlFilterToMongo } from '../src/filter.js';
|
|
2
|
+
|
|
3
|
+
describe('mongo filter', () => {
|
|
4
|
+
it('builds equality from mapping binders', () => {
|
|
5
|
+
expect(
|
|
6
|
+
buildMongoFilter(
|
|
7
|
+
`
|
|
8
|
+
_id: $args._id
|
|
9
|
+
gender: "male"
|
|
10
|
+
`,
|
|
11
|
+
{ args: { _id: '507f1f77bcf86cd799439011' } },
|
|
12
|
+
),
|
|
13
|
+
).toMatchObject({
|
|
14
|
+
gender: 'male',
|
|
15
|
+
});
|
|
16
|
+
expect(
|
|
17
|
+
String(
|
|
18
|
+
buildMongoFilter(`_id: $args._id`, {
|
|
19
|
+
args: { _id: '507f1f77bcf86cd799439011' },
|
|
20
|
+
})._id,
|
|
21
|
+
),
|
|
22
|
+
).toBe('507f1f77bcf86cd799439011');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('merges structured filter via bare binder and spread', () => {
|
|
26
|
+
expect(
|
|
27
|
+
buildMongoFilter(`$args.filter`, {
|
|
28
|
+
args: { filter: { name: { eq: 'Ada' }, age: { gte: 30 } } },
|
|
29
|
+
}),
|
|
30
|
+
).toEqual({
|
|
31
|
+
name: 'Ada',
|
|
32
|
+
age: { $gte: 30 },
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
expect(
|
|
36
|
+
buildMongoFilter(
|
|
37
|
+
`
|
|
38
|
+
gender: "female"
|
|
39
|
+
...$args.filter
|
|
40
|
+
`,
|
|
41
|
+
{ args: { filter: { age: { lt: 40 } } } },
|
|
42
|
+
),
|
|
43
|
+
).toEqual({
|
|
44
|
+
gender: 'female',
|
|
45
|
+
age: { $lt: 40 },
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('maps array id binders to $in', () => {
|
|
50
|
+
const ids = ['507f1f77bcf86cd799439011', '507f1f77bcf86cd799439012'];
|
|
51
|
+
const filter = buildMongoFilter(`_id: $args._ids`, { args: { _ids: ids } });
|
|
52
|
+
expect(filter._id).toMatchObject({ $in: expect.any(Array) });
|
|
53
|
+
expect((filter._id as any).$in.map(String)).toEqual(ids);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('maps GraphQL filter ops to Mongo operators', () => {
|
|
57
|
+
expect(
|
|
58
|
+
graphqlFilterToMongo({
|
|
59
|
+
AND: [{ name: { regex: '^A' } }, { age: { in: [1, 2] } }],
|
|
60
|
+
OR: [{ gender: { eq: 'male' } }],
|
|
61
|
+
}),
|
|
62
|
+
).toEqual({
|
|
63
|
+
$and: [{ name: { $regex: '^A' } }, { age: { $in: [1, 2] } }],
|
|
64
|
+
$or: [{ gender: 'male' }],
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
columnMapFromSelection,
|
|
3
|
+
mapDocumentToGraphql,
|
|
4
|
+
parseMongoMapping,
|
|
5
|
+
projectionFromSelection,
|
|
6
|
+
resolveMappingValue,
|
|
7
|
+
} from '../src/mapping.js';
|
|
8
|
+
|
|
9
|
+
describe('parseMongoMapping', () => {
|
|
10
|
+
it('parses column selection and skips comments', () => {
|
|
11
|
+
const parsed = parseMongoMapping(`
|
|
12
|
+
# User columns
|
|
13
|
+
id: _id
|
|
14
|
+
name: name
|
|
15
|
+
age: age
|
|
16
|
+
`);
|
|
17
|
+
expect(columnMapFromSelection(parsed)).toEqual(
|
|
18
|
+
new Map([
|
|
19
|
+
['id', '_id'],
|
|
20
|
+
['name', 'name'],
|
|
21
|
+
['age', 'age'],
|
|
22
|
+
]),
|
|
23
|
+
);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('parses $args / $this, spreads, and nested blocks', () => {
|
|
27
|
+
const parsed = parseMongoMapping(`
|
|
28
|
+
_id: $this.id
|
|
29
|
+
...$args.where
|
|
30
|
+
address {
|
|
31
|
+
street: address.street
|
|
32
|
+
}
|
|
33
|
+
`);
|
|
34
|
+
expect(parsed.entries[0].value).toEqual({
|
|
35
|
+
kind: 'this',
|
|
36
|
+
value: 'id',
|
|
37
|
+
path: ['id'],
|
|
38
|
+
});
|
|
39
|
+
expect(parsed.entries[1]).toMatchObject({
|
|
40
|
+
spread: true,
|
|
41
|
+
key: '...$args.where',
|
|
42
|
+
value: {
|
|
43
|
+
kind: 'args',
|
|
44
|
+
value: 'where',
|
|
45
|
+
path: ['where'],
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
const address = parsed.entries.find(e => e.key === 'address');
|
|
49
|
+
expect(address?.children?.[0].key).toBe('street');
|
|
50
|
+
expect(address?.children?.[0].value).toEqual({
|
|
51
|
+
kind: 'column',
|
|
52
|
+
value: 'address.street',
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('parses bare $args.where as a whole-value binder', () => {
|
|
57
|
+
const parsed = parseMongoMapping(`$args.where`);
|
|
58
|
+
expect(parsed.entries).toEqual([
|
|
59
|
+
{
|
|
60
|
+
key: '$args.where',
|
|
61
|
+
value: { kind: 'args', value: 'where', path: ['where'] },
|
|
62
|
+
},
|
|
63
|
+
]);
|
|
64
|
+
expect(
|
|
65
|
+
resolveMappingValue(parsed.entries[0].value, {
|
|
66
|
+
args: { where: { name: { eq: 'Ada' } } },
|
|
67
|
+
}),
|
|
68
|
+
).toEqual({ name: { eq: 'Ada' } });
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('builds projection and maps documents', () => {
|
|
72
|
+
const parsed = parseMongoMapping(`
|
|
73
|
+
id: _id
|
|
74
|
+
name: name
|
|
75
|
+
street: address.street
|
|
76
|
+
`);
|
|
77
|
+
expect(projectionFromSelection(parsed)).toEqual({
|
|
78
|
+
_id: 1,
|
|
79
|
+
name: 1,
|
|
80
|
+
'address.street': 1,
|
|
81
|
+
});
|
|
82
|
+
expect(
|
|
83
|
+
mapDocumentToGraphql(
|
|
84
|
+
{ _id: 'abc', name: 'Ada', address: { street: 'Main' } },
|
|
85
|
+
parsed,
|
|
86
|
+
),
|
|
87
|
+
).toEqual({
|
|
88
|
+
id: 'abc',
|
|
89
|
+
name: 'Ada',
|
|
90
|
+
street: 'Main',
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
package/README.md
DELETED