@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,19 @@
|
|
|
1
|
+
import type { MongoMappingValue, ParsedMongoMapping } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Parse `@mongo` `where` / `set` / `selection` block strings.
|
|
4
|
+
* Skips `# ...` comments to EOL. Supports nested `{ ... }` blocks and `$args` / `$this` / `$pseudo`.
|
|
5
|
+
*/
|
|
6
|
+
export declare function parseMongoMapping(source: string | null | undefined): ParsedMongoMapping;
|
|
7
|
+
export declare function resolveMappingValue(value: MongoMappingValue | undefined, ctx: {
|
|
8
|
+
args?: Record<string, unknown>;
|
|
9
|
+
parent?: unknown;
|
|
10
|
+
literals?: Record<string, unknown>;
|
|
11
|
+
}): unknown;
|
|
12
|
+
export declare function columnMapFromSelection(parsed: ParsedMongoMapping): Map<string, string>;
|
|
13
|
+
/** Build a Mongo projection `{ path: 1 }` from selection column mappings. */
|
|
14
|
+
export declare function projectionFromSelection(parsed: ParsedMongoMapping): Record<string, 0 | 1>;
|
|
15
|
+
/**
|
|
16
|
+
* Map a Mongo document to GraphQL fields via selection.
|
|
17
|
+
* Nested blocks become nested objects; lists of subdocs pass through for now.
|
|
18
|
+
*/
|
|
19
|
+
export declare function mapDocumentToGraphql(doc: Record<string, unknown> | null | undefined, selection: ParsedMongoMapping): Record<string, unknown> | null;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export type MongoKind = 'FIND_ONE' | 'FIND_MANY' | 'COUNT' | 'CONNECTION' | 'PAGINATION' | 'AGGREGATE' | 'INSERT_ONE' | 'INSERT_MANY' | 'UPDATE_ONE' | 'UPDATE_MANY' | 'DELETE_ONE' | 'DELETE_MANY';
|
|
2
|
+
export interface MongoTransportOptions {
|
|
3
|
+
maxPoolSize?: number;
|
|
4
|
+
}
|
|
5
|
+
export interface MongoMappingValue {
|
|
6
|
+
kind: 'column' | 'pseudo' | 'args' | 'this' | 'literal';
|
|
7
|
+
value: string;
|
|
8
|
+
path?: string[];
|
|
9
|
+
}
|
|
10
|
+
export interface MongoMappingEntry {
|
|
11
|
+
key: string;
|
|
12
|
+
value?: MongoMappingValue;
|
|
13
|
+
children?: MongoMappingEntry[];
|
|
14
|
+
spread?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface ParsedMongoMapping {
|
|
17
|
+
entries: MongoMappingEntry[];
|
|
18
|
+
}
|
|
19
|
+
export interface MongoFindOptions {
|
|
20
|
+
projection?: Record<string, 0 | 1>;
|
|
21
|
+
sort?: Record<string, 1 | -1>;
|
|
22
|
+
limit?: number;
|
|
23
|
+
skip?: number;
|
|
24
|
+
}
|
|
25
|
+
export interface MongoClientLike {
|
|
26
|
+
findOne(collection: string, filter: Record<string, unknown>, options?: MongoFindOptions): Promise<Record<string, unknown> | null>;
|
|
27
|
+
findMany(collection: string, filter: Record<string, unknown>, options?: MongoFindOptions): Promise<Record<string, unknown>[]>;
|
|
28
|
+
count(collection: string, filter: Record<string, unknown>): Promise<number>;
|
|
29
|
+
insertOne(collection: string, doc: Record<string, unknown>): Promise<{
|
|
30
|
+
insertedId: unknown;
|
|
31
|
+
}>;
|
|
32
|
+
insertMany(collection: string, docs: Record<string, unknown>[]): Promise<{
|
|
33
|
+
insertedIds: unknown[];
|
|
34
|
+
insertedCount: number;
|
|
35
|
+
}>;
|
|
36
|
+
updateOne(collection: string, filter: Record<string, unknown>, update: Record<string, unknown>): Promise<{
|
|
37
|
+
matchedCount: number;
|
|
38
|
+
modifiedCount: number;
|
|
39
|
+
upsertedId?: unknown;
|
|
40
|
+
}>;
|
|
41
|
+
updateMany(collection: string, filter: Record<string, unknown>, update: Record<string, unknown>): Promise<{
|
|
42
|
+
matchedCount: number;
|
|
43
|
+
modifiedCount: number;
|
|
44
|
+
}>;
|
|
45
|
+
deleteOne(collection: string, filter: Record<string, unknown>): Promise<{
|
|
46
|
+
deletedCount: number;
|
|
47
|
+
}>;
|
|
48
|
+
deleteMany(collection: string, filter: Record<string, unknown>): Promise<{
|
|
49
|
+
deletedCount: number;
|
|
50
|
+
}>;
|
|
51
|
+
aggregate(collection: string, pipeline: Record<string, unknown>[]): Promise<Record<string, unknown>[]>;
|
|
52
|
+
dispose(): void | Promise<void>;
|
|
53
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export type MongoKind = 'FIND_ONE' | 'FIND_MANY' | 'COUNT' | 'CONNECTION' | 'PAGINATION' | 'AGGREGATE' | 'INSERT_ONE' | 'INSERT_MANY' | 'UPDATE_ONE' | 'UPDATE_MANY' | 'DELETE_ONE' | 'DELETE_MANY';
|
|
2
|
+
export interface MongoTransportOptions {
|
|
3
|
+
maxPoolSize?: number;
|
|
4
|
+
}
|
|
5
|
+
export interface MongoMappingValue {
|
|
6
|
+
kind: 'column' | 'pseudo' | 'args' | 'this' | 'literal';
|
|
7
|
+
value: string;
|
|
8
|
+
path?: string[];
|
|
9
|
+
}
|
|
10
|
+
export interface MongoMappingEntry {
|
|
11
|
+
key: string;
|
|
12
|
+
value?: MongoMappingValue;
|
|
13
|
+
children?: MongoMappingEntry[];
|
|
14
|
+
spread?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface ParsedMongoMapping {
|
|
17
|
+
entries: MongoMappingEntry[];
|
|
18
|
+
}
|
|
19
|
+
export interface MongoFindOptions {
|
|
20
|
+
projection?: Record<string, 0 | 1>;
|
|
21
|
+
sort?: Record<string, 1 | -1>;
|
|
22
|
+
limit?: number;
|
|
23
|
+
skip?: number;
|
|
24
|
+
}
|
|
25
|
+
export interface MongoClientLike {
|
|
26
|
+
findOne(collection: string, filter: Record<string, unknown>, options?: MongoFindOptions): Promise<Record<string, unknown> | null>;
|
|
27
|
+
findMany(collection: string, filter: Record<string, unknown>, options?: MongoFindOptions): Promise<Record<string, unknown>[]>;
|
|
28
|
+
count(collection: string, filter: Record<string, unknown>): Promise<number>;
|
|
29
|
+
insertOne(collection: string, doc: Record<string, unknown>): Promise<{
|
|
30
|
+
insertedId: unknown;
|
|
31
|
+
}>;
|
|
32
|
+
insertMany(collection: string, docs: Record<string, unknown>[]): Promise<{
|
|
33
|
+
insertedIds: unknown[];
|
|
34
|
+
insertedCount: number;
|
|
35
|
+
}>;
|
|
36
|
+
updateOne(collection: string, filter: Record<string, unknown>, update: Record<string, unknown>): Promise<{
|
|
37
|
+
matchedCount: number;
|
|
38
|
+
modifiedCount: number;
|
|
39
|
+
upsertedId?: unknown;
|
|
40
|
+
}>;
|
|
41
|
+
updateMany(collection: string, filter: Record<string, unknown>, update: Record<string, unknown>): Promise<{
|
|
42
|
+
matchedCount: number;
|
|
43
|
+
modifiedCount: number;
|
|
44
|
+
}>;
|
|
45
|
+
deleteOne(collection: string, filter: Record<string, unknown>): Promise<{
|
|
46
|
+
deletedCount: number;
|
|
47
|
+
}>;
|
|
48
|
+
deleteMany(collection: string, filter: Record<string, unknown>): Promise<{
|
|
49
|
+
deletedCount: number;
|
|
50
|
+
}>;
|
|
51
|
+
aggregate(collection: string, pipeline: Record<string, unknown>[]): Promise<Record<string, unknown>[]>;
|
|
52
|
+
dispose(): void | Promise<void>;
|
|
53
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-mesh/transport-mongo",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "ardatan/graphql-mesh",
|
|
8
|
+
"directory": "packages/transports/mongo"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=22.18.0"
|
|
13
|
+
},
|
|
14
|
+
"main": "dist/cjs/index.js",
|
|
15
|
+
"module": "dist/esm/index.js",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"require": {
|
|
19
|
+
"types": "./dist/typings/index.d.cts",
|
|
20
|
+
"default": "./dist/cjs/index.js"
|
|
21
|
+
},
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./dist/typings/index.d.ts",
|
|
24
|
+
"default": "./dist/esm/index.js"
|
|
25
|
+
},
|
|
26
|
+
"default": {
|
|
27
|
+
"types": "./dist/typings/index.d.ts",
|
|
28
|
+
"default": "./dist/esm/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"./package.json": "./package.json"
|
|
32
|
+
},
|
|
33
|
+
"typings": "dist/typings/index.d.ts",
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"graphql": "^16.0.0 || ^17.0.0"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@graphql-mesh/transport-common": "^1.1.2",
|
|
39
|
+
"@graphql-mesh/utils": "^0.107.3",
|
|
40
|
+
"@graphql-tools/utils": "^13.2.2",
|
|
41
|
+
"@whatwg-node/disposablestack": "^0.1.0",
|
|
42
|
+
"mongodb": "^7.0.0",
|
|
43
|
+
"tslib": "^2.4.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@omnigraph/mongo": "workspace:*",
|
|
47
|
+
"graphql": "16.14.0"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public",
|
|
51
|
+
"directory": "dist"
|
|
52
|
+
},
|
|
53
|
+
"sideEffects": false,
|
|
54
|
+
"typescript": {
|
|
55
|
+
"definition": "dist/typings/index.d.ts"
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { MongoClient, type Db } from 'mongodb';
|
|
2
|
+
import type { MongoClientLike, MongoTransportOptions } from './types.js';
|
|
3
|
+
|
|
4
|
+
export interface CreateMongoClientOptions extends MongoTransportOptions {
|
|
5
|
+
location: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function createMongoClient(opts: CreateMongoClientOptions): MongoClientLike {
|
|
9
|
+
const client = new MongoClient(opts.location, {
|
|
10
|
+
maxPoolSize: opts.maxPoolSize,
|
|
11
|
+
});
|
|
12
|
+
let dbPromise: Promise<Db> | undefined;
|
|
13
|
+
|
|
14
|
+
async function getDb(): Promise<Db> {
|
|
15
|
+
if (!dbPromise) {
|
|
16
|
+
dbPromise = client.connect().then(() => client.db());
|
|
17
|
+
}
|
|
18
|
+
return dbPromise;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
async findOne(collection, filter, options) {
|
|
23
|
+
const db = await getDb();
|
|
24
|
+
return db.collection(collection).findOne(filter, {
|
|
25
|
+
projection: options?.projection,
|
|
26
|
+
sort: options?.sort,
|
|
27
|
+
}) as Promise<Record<string, unknown> | null>;
|
|
28
|
+
},
|
|
29
|
+
async findMany(collection, filter, options) {
|
|
30
|
+
const db = await getDb();
|
|
31
|
+
let cursor = db.collection(collection).find(filter, {
|
|
32
|
+
projection: options?.projection,
|
|
33
|
+
sort: options?.sort,
|
|
34
|
+
});
|
|
35
|
+
if (options?.skip != null) cursor = cursor.skip(options.skip);
|
|
36
|
+
if (options?.limit != null) cursor = cursor.limit(options.limit);
|
|
37
|
+
return cursor.toArray() as Promise<Record<string, unknown>[]>;
|
|
38
|
+
},
|
|
39
|
+
async count(collection, filter) {
|
|
40
|
+
const db = await getDb();
|
|
41
|
+
return db.collection(collection).countDocuments(filter);
|
|
42
|
+
},
|
|
43
|
+
async insertOne(collection, doc) {
|
|
44
|
+
const db = await getDb();
|
|
45
|
+
const result = await db.collection(collection).insertOne(doc);
|
|
46
|
+
return { insertedId: result.insertedId };
|
|
47
|
+
},
|
|
48
|
+
async insertMany(collection, docs) {
|
|
49
|
+
const db = await getDb();
|
|
50
|
+
const result = await db.collection(collection).insertMany(docs);
|
|
51
|
+
return {
|
|
52
|
+
insertedIds: Object.values(result.insertedIds),
|
|
53
|
+
insertedCount: result.insertedCount,
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
async updateOne(collection, filter, update) {
|
|
57
|
+
const db = await getDb();
|
|
58
|
+
const result = await db.collection(collection).updateOne(filter, update);
|
|
59
|
+
return {
|
|
60
|
+
matchedCount: result.matchedCount,
|
|
61
|
+
modifiedCount: result.modifiedCount,
|
|
62
|
+
upsertedId: result.upsertedId ?? undefined,
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
async updateMany(collection, filter, update) {
|
|
66
|
+
const db = await getDb();
|
|
67
|
+
const result = await db.collection(collection).updateMany(filter, update);
|
|
68
|
+
return {
|
|
69
|
+
matchedCount: result.matchedCount,
|
|
70
|
+
modifiedCount: result.modifiedCount,
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
async deleteOne(collection, filter) {
|
|
74
|
+
const db = await getDb();
|
|
75
|
+
const result = await db.collection(collection).deleteOne(filter);
|
|
76
|
+
return { deletedCount: result.deletedCount ?? 0 };
|
|
77
|
+
},
|
|
78
|
+
async deleteMany(collection, filter) {
|
|
79
|
+
const db = await getDb();
|
|
80
|
+
const result = await db.collection(collection).deleteMany(filter);
|
|
81
|
+
return { deletedCount: result.deletedCount ?? 0 };
|
|
82
|
+
},
|
|
83
|
+
async aggregate(collection, pipeline) {
|
|
84
|
+
const db = await getDb();
|
|
85
|
+
return db.collection(collection).aggregate(pipeline).toArray() as Promise<
|
|
86
|
+
Record<string, unknown>[]
|
|
87
|
+
>;
|
|
88
|
+
},
|
|
89
|
+
async dispose() {
|
|
90
|
+
await client.close();
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
package/src/execution.ts
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import type { GraphQLFieldConfig, GraphQLSchema } from 'graphql';
|
|
2
|
+
import { createDefaultExecutor, type DisposableExecutor } from '@graphql-mesh/transport-common';
|
|
3
|
+
import { getDirective, MapperKind, mapSchema } from '@graphql-tools/utils';
|
|
4
|
+
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
5
|
+
import { createMongoClient } from './client.js';
|
|
6
|
+
import {
|
|
7
|
+
buildMongoFilter,
|
|
8
|
+
buildMongoSort,
|
|
9
|
+
buildSetDocument,
|
|
10
|
+
coerceFilterValue,
|
|
11
|
+
} from './filter.js';
|
|
12
|
+
import {
|
|
13
|
+
mapDocumentToGraphql,
|
|
14
|
+
parseMongoMapping,
|
|
15
|
+
projectionFromSelection,
|
|
16
|
+
resolveMappingValue,
|
|
17
|
+
} from './mapping.js';
|
|
18
|
+
import type { MongoClientLike, MongoKind, MongoTransportOptions } from './types.js';
|
|
19
|
+
|
|
20
|
+
export interface GetMongoExecutorOpts {
|
|
21
|
+
subgraph: GraphQLSchema;
|
|
22
|
+
client?: MongoClientLike;
|
|
23
|
+
cwd?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface MongoDirectiveArgs {
|
|
27
|
+
collection: string;
|
|
28
|
+
kind: MongoKind;
|
|
29
|
+
filter?: string;
|
|
30
|
+
where?: string;
|
|
31
|
+
sort?: string;
|
|
32
|
+
limit?: string;
|
|
33
|
+
skip?: string;
|
|
34
|
+
first?: string;
|
|
35
|
+
after?: string;
|
|
36
|
+
page?: string;
|
|
37
|
+
perPage?: string;
|
|
38
|
+
selection?: string;
|
|
39
|
+
set?: string;
|
|
40
|
+
pipeline?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getMongoExecutor({ subgraph, client }: GetMongoExecutorOpts): DisposableExecutor {
|
|
44
|
+
const transportDirectives = getDirective(subgraph, subgraph, 'transport');
|
|
45
|
+
if (!transportDirectives?.length && !client) {
|
|
46
|
+
throw new Error('No @transport directive found on schema definition');
|
|
47
|
+
}
|
|
48
|
+
const transport = transportDirectives?.[0] as
|
|
49
|
+
| { location?: string; options?: MongoTransportOptions }
|
|
50
|
+
| undefined;
|
|
51
|
+
const mongoClient =
|
|
52
|
+
client ||
|
|
53
|
+
createMongoClient({
|
|
54
|
+
location: transport!.location!,
|
|
55
|
+
...(transport?.options || {}),
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const executable = mapSchema(subgraph, {
|
|
59
|
+
[MapperKind.OBJECT_FIELD](fieldConfig) {
|
|
60
|
+
const mongoDirs = getDirective(subgraph, fieldConfig, 'mongo') as
|
|
61
|
+
| MongoDirectiveArgs[]
|
|
62
|
+
| undefined;
|
|
63
|
+
if (!mongoDirs?.length) return fieldConfig;
|
|
64
|
+
return attachMongoResolver(fieldConfig, mongoDirs[0], mongoClient);
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const defaultExecutor = createDefaultExecutor(executable);
|
|
69
|
+
const disposable = defaultExecutor as DisposableExecutor;
|
|
70
|
+
disposable[DisposableSymbols.asyncDispose] = async () => {
|
|
71
|
+
await mongoClient.dispose();
|
|
72
|
+
};
|
|
73
|
+
return disposable;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function attachMongoResolver(
|
|
77
|
+
fieldConfig: GraphQLFieldConfig<any, any>,
|
|
78
|
+
directive: MongoDirectiveArgs,
|
|
79
|
+
client: MongoClientLike,
|
|
80
|
+
): GraphQLFieldConfig<any, any> {
|
|
81
|
+
const selection = parseMongoMapping(directive.selection);
|
|
82
|
+
const projection = projectionFromSelection(selection);
|
|
83
|
+
const filterSource = directive.filter ?? directive.where;
|
|
84
|
+
|
|
85
|
+
fieldConfig.resolve = async function mongoResolver(parent, graphqlArgs) {
|
|
86
|
+
const ctx = { args: graphqlArgs, parent };
|
|
87
|
+
const filter = buildMongoFilter(filterSource, ctx);
|
|
88
|
+
const sort = buildMongoSort(directive.sort, ctx);
|
|
89
|
+
const proj = Object.keys(projection).length ? projection : undefined;
|
|
90
|
+
|
|
91
|
+
switch (directive.kind) {
|
|
92
|
+
case 'FIND_ONE': {
|
|
93
|
+
const doc = await client.findOne(directive.collection, filter, {
|
|
94
|
+
projection: proj,
|
|
95
|
+
sort,
|
|
96
|
+
skip: resolveBinderNumber(directive.skip, ctx),
|
|
97
|
+
});
|
|
98
|
+
return mapDocumentToGraphql(doc, selection);
|
|
99
|
+
}
|
|
100
|
+
case 'FIND_MANY': {
|
|
101
|
+
const docs = await client.findMany(directive.collection, filter, {
|
|
102
|
+
projection: proj,
|
|
103
|
+
sort,
|
|
104
|
+
limit: resolveBinderNumber(directive.limit, ctx),
|
|
105
|
+
skip: resolveBinderNumber(directive.skip, ctx),
|
|
106
|
+
});
|
|
107
|
+
return docs.map(doc => mapDocumentToGraphql(doc, selection));
|
|
108
|
+
}
|
|
109
|
+
case 'COUNT':
|
|
110
|
+
return client.count(directive.collection, filter);
|
|
111
|
+
case 'CONNECTION':
|
|
112
|
+
return resolveConnection(client, directive, filter, sort, proj, ctx, selection);
|
|
113
|
+
case 'PAGINATION':
|
|
114
|
+
return resolvePagination(client, directive, filter, sort, proj, ctx, selection);
|
|
115
|
+
case 'INSERT_ONE': {
|
|
116
|
+
const doc = buildSetDocument(directive.set, ctx);
|
|
117
|
+
const { insertedId } = await client.insertOne(directive.collection, doc);
|
|
118
|
+
const created = await client.findOne(directive.collection, { _id: insertedId });
|
|
119
|
+
return mapPayload(created, insertedId, selection);
|
|
120
|
+
}
|
|
121
|
+
case 'INSERT_MANY': {
|
|
122
|
+
const records = (graphqlArgs.records || []) as Record<string, unknown>[];
|
|
123
|
+
const { insertedIds, insertedCount } = await client.insertMany(
|
|
124
|
+
directive.collection,
|
|
125
|
+
records,
|
|
126
|
+
);
|
|
127
|
+
const docs = await client.findMany(directive.collection, {
|
|
128
|
+
_id: { $in: insertedIds },
|
|
129
|
+
});
|
|
130
|
+
return {
|
|
131
|
+
records: docs.map(d => mapDocumentToGraphql(d, nestedSelection(selection, 'records'))),
|
|
132
|
+
recordIds: insertedIds.map(id => String(id)),
|
|
133
|
+
createdCount: insertedCount,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
case 'UPDATE_ONE': {
|
|
137
|
+
const setDoc = buildSetDocument(directive.set, ctx);
|
|
138
|
+
await client.updateOne(directive.collection, filter, { $set: setDoc });
|
|
139
|
+
const updated = await client.findOne(directive.collection, filter);
|
|
140
|
+
return mapPayload(updated, updated?._id, selection);
|
|
141
|
+
}
|
|
142
|
+
case 'UPDATE_MANY': {
|
|
143
|
+
const setDoc = buildSetDocument(directive.set, ctx);
|
|
144
|
+
const result = await client.updateMany(directive.collection, filter, { $set: setDoc });
|
|
145
|
+
return {
|
|
146
|
+
matchedCount: result.matchedCount,
|
|
147
|
+
modifiedCount: result.modifiedCount,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
case 'DELETE_ONE': {
|
|
151
|
+
const existing = await client.findOne(directive.collection, filter);
|
|
152
|
+
await client.deleteOne(directive.collection, filter);
|
|
153
|
+
return mapPayload(existing, existing?._id, selection);
|
|
154
|
+
}
|
|
155
|
+
case 'DELETE_MANY': {
|
|
156
|
+
const result = await client.deleteMany(directive.collection, filter);
|
|
157
|
+
return { deletedCount: result.deletedCount };
|
|
158
|
+
}
|
|
159
|
+
case 'AGGREGATE': {
|
|
160
|
+
const pipeline = resolvePipeline(directive.pipeline, ctx, filter);
|
|
161
|
+
const rows = await client.aggregate(directive.collection, pipeline);
|
|
162
|
+
return rows.map(doc => mapDocumentToGraphql(doc, selection));
|
|
163
|
+
}
|
|
164
|
+
default:
|
|
165
|
+
throw new Error(`@mongo kind ${directive.kind as string} is not implemented`);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
return fieldConfig;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function resolveConnection(
|
|
173
|
+
client: MongoClientLike,
|
|
174
|
+
directive: MongoDirectiveArgs,
|
|
175
|
+
filter: Record<string, unknown>,
|
|
176
|
+
sort: Record<string, 1 | -1> | undefined,
|
|
177
|
+
projection: Record<string, 0 | 1> | undefined,
|
|
178
|
+
ctx: { args?: Record<string, unknown>; parent?: unknown },
|
|
179
|
+
selection: ReturnType<typeof parseMongoMapping>,
|
|
180
|
+
) {
|
|
181
|
+
const first = resolveBinderNumber(directive.first, ctx) ?? 10;
|
|
182
|
+
const after = resolveBinderString(directive.after, ctx);
|
|
183
|
+
const connFilter = { ...filter };
|
|
184
|
+
if (after) {
|
|
185
|
+
connFilter._id = {
|
|
186
|
+
...(typeof connFilter._id === 'object' && connFilter._id ? (connFilter._id as object) : {}),
|
|
187
|
+
$gt: coerceFilterValue(after),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
const docs = await client.findMany(directive.collection, connFilter, {
|
|
191
|
+
projection,
|
|
192
|
+
sort: sort || { _id: 1 },
|
|
193
|
+
limit: first + 1,
|
|
194
|
+
});
|
|
195
|
+
const hasNextPage = docs.length > first;
|
|
196
|
+
const pageDocs = hasNextPage ? docs.slice(0, first) : docs;
|
|
197
|
+
const totalCount = await client.count(directive.collection, filter);
|
|
198
|
+
const nodesSel = nestedSelection(selection, 'nodes');
|
|
199
|
+
const nodes = pageDocs.map(d => mapDocumentToGraphql(d, nodesSel));
|
|
200
|
+
const edges = pageDocs.map(d => ({
|
|
201
|
+
cursor: String(d._id),
|
|
202
|
+
node: mapDocumentToGraphql(d, nestedSelection(nestedSelection(selection, 'edges'), 'node')),
|
|
203
|
+
}));
|
|
204
|
+
return {
|
|
205
|
+
nodes,
|
|
206
|
+
edges,
|
|
207
|
+
pageInfo: {
|
|
208
|
+
hasNextPage,
|
|
209
|
+
hasPreviousPage: Boolean(after),
|
|
210
|
+
startCursor: edges[0]?.cursor ?? null,
|
|
211
|
+
endCursor: edges[edges.length - 1]?.cursor ?? null,
|
|
212
|
+
},
|
|
213
|
+
totalCount,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function resolvePagination(
|
|
218
|
+
client: MongoClientLike,
|
|
219
|
+
directive: MongoDirectiveArgs,
|
|
220
|
+
filter: Record<string, unknown>,
|
|
221
|
+
sort: Record<string, 1 | -1> | undefined,
|
|
222
|
+
projection: Record<string, 0 | 1> | undefined,
|
|
223
|
+
ctx: { args?: Record<string, unknown>; parent?: unknown },
|
|
224
|
+
selection: ReturnType<typeof parseMongoMapping>,
|
|
225
|
+
) {
|
|
226
|
+
const page = Math.max(1, resolveBinderNumber(directive.page, ctx) ?? 1);
|
|
227
|
+
const perPage = Math.max(1, resolveBinderNumber(directive.perPage, ctx) ?? 20);
|
|
228
|
+
const itemCount = await client.count(directive.collection, filter);
|
|
229
|
+
const pageCount = Math.ceil(itemCount / perPage) || 0;
|
|
230
|
+
const docs = await client.findMany(directive.collection, filter, {
|
|
231
|
+
projection,
|
|
232
|
+
sort,
|
|
233
|
+
skip: (page - 1) * perPage,
|
|
234
|
+
limit: perPage,
|
|
235
|
+
});
|
|
236
|
+
const itemsSel = nestedSelection(selection, 'items');
|
|
237
|
+
return {
|
|
238
|
+
count: docs.length,
|
|
239
|
+
items: docs.map(d => mapDocumentToGraphql(d, itemsSel)),
|
|
240
|
+
pageInfo: {
|
|
241
|
+
currentPage: page,
|
|
242
|
+
perPage,
|
|
243
|
+
pageCount,
|
|
244
|
+
itemCount,
|
|
245
|
+
hasNextPage: page < pageCount,
|
|
246
|
+
hasPreviousPage: page > 1,
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function mapPayload(
|
|
252
|
+
doc: Record<string, unknown> | null | undefined,
|
|
253
|
+
id: unknown,
|
|
254
|
+
selection: ReturnType<typeof parseMongoMapping>,
|
|
255
|
+
) {
|
|
256
|
+
const recordSel = nestedSelection(selection, 'record');
|
|
257
|
+
return {
|
|
258
|
+
record: mapDocumentToGraphql(doc, recordSel.entries.length ? recordSel : selection),
|
|
259
|
+
recordId: id != null ? String(id) : null,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function nestedSelection(
|
|
264
|
+
selection: ReturnType<typeof parseMongoMapping>,
|
|
265
|
+
key: string,
|
|
266
|
+
): ReturnType<typeof parseMongoMapping> {
|
|
267
|
+
const entry = selection.entries.find(e => e.key === key);
|
|
268
|
+
return { entries: entry?.children || [] };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function resolvePipeline(
|
|
272
|
+
pipelineSource: string | undefined,
|
|
273
|
+
ctx: { args?: Record<string, unknown>; parent?: unknown },
|
|
274
|
+
filter: Record<string, unknown>,
|
|
275
|
+
): Record<string, unknown>[] {
|
|
276
|
+
if (!pipelineSource || !pipelineSource.trim()) {
|
|
277
|
+
return Object.keys(filter).length ? [{ $match: filter }] : [];
|
|
278
|
+
}
|
|
279
|
+
const trimmed = pipelineSource.trim();
|
|
280
|
+
if (trimmed.startsWith('[')) {
|
|
281
|
+
try {
|
|
282
|
+
const parsed = JSON.parse(trimmed) as Record<string, unknown>[];
|
|
283
|
+
return parsed.map(stage => {
|
|
284
|
+
if (stage.$match === '$args.filter' || stage.$match === '$filter') {
|
|
285
|
+
return { $match: filter };
|
|
286
|
+
}
|
|
287
|
+
return stage;
|
|
288
|
+
});
|
|
289
|
+
} catch {
|
|
290
|
+
// fall through
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
// Mapping-style: treat as match binder only
|
|
294
|
+
const fromMapping = buildMongoFilter(pipelineSource, ctx);
|
|
295
|
+
return Object.keys(fromMapping).length ? [{ $match: fromMapping }] : [{ $match: filter }];
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function resolveBinderNumber(
|
|
299
|
+
source: string | undefined,
|
|
300
|
+
ctx: { args?: Record<string, unknown>; parent?: unknown },
|
|
301
|
+
): number | undefined {
|
|
302
|
+
if (!source) return undefined;
|
|
303
|
+
const parsed = parseMongoMapping(source);
|
|
304
|
+
if (parsed.entries.length === 1 && parsed.entries[0].value) {
|
|
305
|
+
const val = resolveMappingValue(parsed.entries[0].value, ctx);
|
|
306
|
+
if (typeof val === 'number') return val;
|
|
307
|
+
if (typeof val === 'string' && val.trim() !== '') return Number(val);
|
|
308
|
+
}
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function resolveBinderString(
|
|
313
|
+
source: string | undefined,
|
|
314
|
+
ctx: { args?: Record<string, unknown>; parent?: unknown },
|
|
315
|
+
): string | undefined {
|
|
316
|
+
if (!source) return undefined;
|
|
317
|
+
const parsed = parseMongoMapping(source);
|
|
318
|
+
if (parsed.entries.length === 1 && parsed.entries[0].value) {
|
|
319
|
+
const val = resolveMappingValue(parsed.entries[0].value, ctx);
|
|
320
|
+
if (typeof val === 'string') return val;
|
|
321
|
+
}
|
|
322
|
+
return undefined;
|
|
323
|
+
}
|