@omnigraph/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.
@@ -0,0 +1,57 @@
1
+ import {
2
+ GraphQLBoolean,
3
+ GraphQLFloat,
4
+ GraphQLID,
5
+ GraphQLInt,
6
+ GraphQLList,
7
+ GraphQLString,
8
+ type GraphQLOutputType,
9
+ type GraphQLScalarType,
10
+ } from 'graphql';
11
+ import { GraphQLByte, GraphQLDateTime, GraphQLJSON, GraphQLObjectID } from 'graphql-scalars';
12
+
13
+ /**
14
+ * Map a Mongoose / BSON path instance type name to a GraphQL output type.
15
+ * Sole document PK (`_id` / asId) uses GraphQL `ID` (federation-friendly).
16
+ * Other ObjectId paths keep graphql-scalars `ObjectID`.
17
+ */
18
+ export function mapMongoPathToGraphQL(
19
+ instance: string,
20
+ opts?: { asId?: boolean },
21
+ ): GraphQLOutputType | GraphQLScalarType {
22
+ if (opts?.asId) {
23
+ return GraphQLID;
24
+ }
25
+ if (instance === 'ObjectID' || instance === 'ObjectId') {
26
+ return GraphQLObjectID;
27
+ }
28
+ switch (instance) {
29
+ case 'String':
30
+ return GraphQLString;
31
+ case 'Number':
32
+ return GraphQLFloat;
33
+ case 'Decimal128':
34
+ return GraphQLString;
35
+ case 'Boolean':
36
+ return GraphQLBoolean;
37
+ case 'Date':
38
+ return GraphQLDateTime;
39
+ case 'Buffer':
40
+ return GraphQLByte;
41
+ case 'Mixed':
42
+ case 'Array':
43
+ return GraphQLJSON;
44
+ case 'Map':
45
+ return GraphQLJSON;
46
+ case 'Int32':
47
+ return GraphQLInt;
48
+ case 'Double':
49
+ return GraphQLFloat;
50
+ default:
51
+ return GraphQLJSON;
52
+ }
53
+ }
54
+
55
+ export function mapMongoPathToList(inner: GraphQLOutputType): GraphQLOutputType {
56
+ return new GraphQLList(inner);
57
+ }
@@ -0,0 +1,369 @@
1
+ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2
+
3
+ exports[`@omnigraph/mongo schema emit emits @mongo directives with filter binders and v0-style roots: user-full-slice 1`] = `
4
+ "schema @transport(subgraph: "users", kind: "mongo", location: "mongodb://localhost:27017/test") @link(url: "https://specs.apollo.dev/federation/v2.6", import: ["@key"]) {
5
+ query: Query
6
+ mutation: Mutation
7
+ }
8
+
9
+ directive @transport(subgraph: String!, kind: String!, location: String, headers: [[String]], options: TransportOptions) repeatable on SCHEMA
10
+
11
+ directive @mongo(subgraph: String, collection: String!, kind: MongoKind!, filter: String, where: String, sort: String, limit: String, skip: String, first: String, after: String, page: String, perPage: String, set: String, selection: String, pipeline: String) on FIELD_DEFINITION | OBJECT
12
+
13
+ type Query {
14
+ User_findById(_id: ID!): User @mongo(subgraph: "users", collection: "user_users", kind: FIND_ONE, selection: """
15
+ id: _id
16
+ name: name
17
+ age: age
18
+ gender: gender
19
+ someMixed: someMixed
20
+ """, filter: """
21
+
22
+ _id: $args._id
23
+ """)
24
+ User_dataLoader(_id: ID!): User @mongo(subgraph: "users", collection: "user_users", kind: FIND_ONE, selection: """
25
+ id: _id
26
+ name: name
27
+ age: age
28
+ gender: gender
29
+ someMixed: someMixed
30
+ """, filter: """
31
+
32
+ _id: $args._id
33
+ """)
34
+ User_findByIds(_ids: [ID!]!, limit: Int = 100, sort: UserSort): [User!]! @mongo(subgraph: "users", collection: "user_users", kind: FIND_MANY, selection: """
35
+ id: _id
36
+ name: name
37
+ age: age
38
+ gender: gender
39
+ someMixed: someMixed
40
+ """, filter: """
41
+
42
+ _id: $args._ids
43
+ """, limit: "$args.limit", sort: "$args.sort")
44
+ User_dataLoaderMany(_ids: [ID!]!): [User]! @mongo(subgraph: "users", collection: "user_users", kind: FIND_MANY, selection: """
45
+ id: _id
46
+ name: name
47
+ age: age
48
+ gender: gender
49
+ someMixed: someMixed
50
+ """, filter: """
51
+
52
+ _id: $args._ids
53
+ """)
54
+ User_findMany(filter: UserFilter, limit: Int = 100, skip: Int, sort: UserSort): [User!]! @mongo(subgraph: "users", collection: "user_users", kind: FIND_MANY, selection: """
55
+ id: _id
56
+ name: name
57
+ age: age
58
+ gender: gender
59
+ someMixed: someMixed
60
+ """, filter: "$args.filter", sort: "$args.sort", limit: "$args.limit", skip: "$args.skip")
61
+ User_findOne(filter: UserFilter, skip: Int, sort: UserSort): User @mongo(subgraph: "users", collection: "user_users", kind: FIND_ONE, selection: """
62
+ id: _id
63
+ name: name
64
+ age: age
65
+ gender: gender
66
+ someMixed: someMixed
67
+ """, filter: "$args.filter", sort: "$args.sort", skip: "$args.skip")
68
+ User_count(filter: UserFilter): Int! @mongo(subgraph: "users", collection: "user_users", kind: COUNT, selection: """
69
+
70
+ $
71
+ """, filter: "$args.filter")
72
+ User_connection(filter: UserFilter, first: Int, after: String, sort: UserSort): UserConnection @mongo(subgraph: "users", collection: "user_users", kind: CONNECTION, selection: """
73
+ nodes {
74
+ id: _id
75
+ name: name
76
+ age: age
77
+ gender: gender
78
+ someMixed: someMixed
79
+ }
80
+ edges {
81
+ cursor: $cursor
82
+ node {
83
+ id: _id
84
+ name: name
85
+ age: age
86
+ gender: gender
87
+ someMixed: someMixed
88
+ }
89
+ }
90
+ pageInfo {
91
+ hasNextPage: $hasNextPage
92
+ hasPreviousPage: $hasPreviousPage
93
+ startCursor: $startCursor
94
+ endCursor: $endCursor
95
+ }
96
+ totalCount: $totalCount
97
+ """, filter: "$args.filter", sort: "$args.sort", first: "$args.first", after: "$args.after")
98
+ User_pagination(filter: UserFilter, page: Int, perPage: Int = 20, sort: UserSort): UserPagination @mongo(subgraph: "users", collection: "user_users", kind: PAGINATION, selection: """
99
+ count: $count
100
+ items {
101
+ id: _id
102
+ name: name
103
+ age: age
104
+ gender: gender
105
+ someMixed: someMixed
106
+ }
107
+ pageInfo {
108
+ currentPage: $currentPage
109
+ perPage: $perPage
110
+ pageCount: $pageCount
111
+ itemCount: $itemCount
112
+ hasNextPage: $hasNextPage
113
+ hasPreviousPage: $hasPreviousPage
114
+ }
115
+ """, filter: "$args.filter", sort: "$args.sort", page: "$args.page", perPage: "$args.perPage")
116
+ }
117
+
118
+ type User @key(fields: "id") @mongo(subgraph: "users", collection: "user_users", kind: FIND_ONE, filter: """
119
+
120
+ _id: $this.id
121
+ """, selection: """
122
+ id: _id
123
+ name: name
124
+ age: age
125
+ gender: gender
126
+ someMixed: someMixed
127
+ """) {
128
+ id: ID!
129
+ name: String
130
+ age: Float
131
+ gender: String
132
+ someMixed: JSON
133
+ }
134
+
135
+ """
136
+ The \`JSON\` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
137
+ """
138
+ scalar JSON @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")
139
+
140
+ input UserSort {
141
+ id: SortDirection
142
+ name: SortDirection
143
+ age: SortDirection
144
+ gender: SortDirection
145
+ someMixed: SortDirection
146
+ }
147
+
148
+ enum SortDirection {
149
+ ASC
150
+ DESC
151
+ }
152
+
153
+ input UserFilter {
154
+ AND: [UserFilter!]
155
+ OR: [UserFilter!]
156
+ NOT: UserFilter
157
+ id: IDFilter
158
+ name: StringFilter
159
+ age: FloatFilter
160
+ gender: StringFilter
161
+ someMixed: StringFilter
162
+ }
163
+
164
+ input IDFilter {
165
+ eq: ID
166
+ ne: ID
167
+ in: [ID!]
168
+ nin: [ID!]
169
+ exists: Boolean
170
+ }
171
+
172
+ input StringFilter {
173
+ eq: String
174
+ ne: String
175
+ in: [String!]
176
+ nin: [String!]
177
+ regex: String
178
+ exists: Boolean
179
+ }
180
+
181
+ input FloatFilter {
182
+ eq: Float
183
+ ne: Float
184
+ in: [Float!]
185
+ nin: [Float!]
186
+ gt: Float
187
+ gte: Float
188
+ lt: Float
189
+ lte: Float
190
+ exists: Boolean
191
+ }
192
+
193
+ type UserConnection {
194
+ edges: [UserEdge!]!
195
+ nodes: [User!]!
196
+ pageInfo: PageInfo!
197
+ totalCount: Int!
198
+ }
199
+
200
+ type UserEdge {
201
+ cursor: String!
202
+ node: User!
203
+ }
204
+
205
+ type PageInfo {
206
+ hasNextPage: Boolean!
207
+ hasPreviousPage: Boolean!
208
+ startCursor: String
209
+ endCursor: String
210
+ }
211
+
212
+ type UserPagination {
213
+ count: Int
214
+ items: [User]
215
+ pageInfo: PaginationInfo
216
+ }
217
+
218
+ type PaginationInfo {
219
+ currentPage: Int!
220
+ perPage: Int!
221
+ pageCount: Int
222
+ itemCount: Int
223
+ hasNextPage: Boolean
224
+ hasPreviousPage: Boolean
225
+ }
226
+
227
+ type Mutation {
228
+ User_createOne(record: CreateOneUserInput!): UserPayload @mongo(subgraph: "users", collection: "user_users", kind: INSERT_ONE, selection: """
229
+ record {
230
+ id: _id
231
+ name: name
232
+ age: age
233
+ gender: gender
234
+ someMixed: someMixed
235
+ }
236
+ recordId: $_id
237
+ """, set: """
238
+ name: $args.record.name
239
+ age: $args.record.age
240
+ gender: $args.record.gender
241
+ someMixed: $args.record.someMixed
242
+ """)
243
+ User_createMany(records: [CreateOneUserInput!]!): UserPayload @mongo(subgraph: "users", collection: "user_users", kind: INSERT_MANY, selection: """
244
+ records {
245
+ id: _id
246
+ name: name
247
+ age: age
248
+ gender: gender
249
+ someMixed: someMixed
250
+ }
251
+ recordIds: $recordIds
252
+ createdCount: $createdCount
253
+ """, set: """
254
+
255
+ records: $args.records
256
+ """)
257
+ User_updateById(_id: ID!, record: UpdateByIdUserInput!): UserPayload @mongo(subgraph: "users", collection: "user_users", kind: UPDATE_ONE, selection: """
258
+ record {
259
+ id: _id
260
+ name: name
261
+ age: age
262
+ gender: gender
263
+ someMixed: someMixed
264
+ }
265
+ recordId: $_id
266
+ """, filter: """
267
+
268
+ _id: $args._id
269
+ """, set: """
270
+ name: $args.record.name
271
+ age: $args.record.age
272
+ gender: $args.record.gender
273
+ someMixed: $args.record.someMixed
274
+ """)
275
+ User_updateOne(filter: UserFilter, record: UpdateByIdUserInput!): UserPayload @mongo(subgraph: "users", collection: "user_users", kind: UPDATE_ONE, selection: """
276
+ record {
277
+ id: _id
278
+ name: name
279
+ age: age
280
+ gender: gender
281
+ someMixed: someMixed
282
+ }
283
+ recordId: $_id
284
+ """, filter: "$args.filter", set: """
285
+ name: $args.record.name
286
+ age: $args.record.age
287
+ gender: $args.record.gender
288
+ someMixed: $args.record.someMixed
289
+ """)
290
+ User_updateMany(filter: UserFilter, record: UpdateByIdUserInput!): UserPayload @mongo(subgraph: "users", collection: "user_users", kind: UPDATE_MANY, selection: """
291
+ modifiedCount: $modifiedCount
292
+ matchedCount: $matchedCount
293
+ """, filter: "$args.filter", set: """
294
+ name: $args.record.name
295
+ age: $args.record.age
296
+ gender: $args.record.gender
297
+ someMixed: $args.record.someMixed
298
+ """)
299
+ User_removeById(_id: ID!): UserPayload @mongo(subgraph: "users", collection: "user_users", kind: DELETE_ONE, selection: """
300
+ record {
301
+ id: _id
302
+ name: name
303
+ age: age
304
+ gender: gender
305
+ someMixed: someMixed
306
+ }
307
+ recordId: $_id
308
+ """, filter: """
309
+
310
+ _id: $args._id
311
+ """)
312
+ User_removeOne(filter: UserFilter): UserPayload @mongo(subgraph: "users", collection: "user_users", kind: DELETE_ONE, selection: """
313
+ record {
314
+ id: _id
315
+ name: name
316
+ age: age
317
+ gender: gender
318
+ someMixed: someMixed
319
+ }
320
+ recordId: $_id
321
+ """, filter: "$args.filter")
322
+ User_removeMany(filter: UserFilter!): UserPayload @mongo(subgraph: "users", collection: "user_users", kind: DELETE_MANY, selection: """
323
+
324
+ deletedCount: $deletedCount
325
+ """, filter: "$args.filter")
326
+ }
327
+
328
+ type UserPayload {
329
+ record: User
330
+ recordId: ID
331
+ records: [User!]
332
+ recordIds: [ID!]
333
+ createdCount: Int
334
+ modifiedCount: Int
335
+ matchedCount: Int
336
+ deletedCount: Int
337
+ }
338
+
339
+ input CreateOneUserInput {
340
+ name: String
341
+ age: Float
342
+ gender: String
343
+ someMixed: JSON
344
+ }
345
+
346
+ input UpdateByIdUserInput {
347
+ name: String
348
+ age: Float
349
+ gender: String
350
+ someMixed: JSON
351
+ }
352
+
353
+ scalar TransportOptions
354
+
355
+ enum MongoKind {
356
+ FIND_ONE
357
+ FIND_MANY
358
+ COUNT
359
+ CONNECTION
360
+ PAGINATION
361
+ AGGREGATE
362
+ INSERT_ONE
363
+ INSERT_MANY
364
+ UPDATE_ONE
365
+ UPDATE_MANY
366
+ DELETE_ONE
367
+ DELETE_MANY
368
+ }"
369
+ `;
@@ -0,0 +1,58 @@
1
+ import { printSchemaWithDirectives } from '@graphql-tools/utils';
2
+ import { Schema } from 'mongoose';
3
+ import { loadGraphQLSchemaFromMongo } from '../src/index.js';
4
+
5
+ describe('@omnigraph/mongo schema emit', () => {
6
+ it('emits @mongo directives with filter binders and v0-style roots', async () => {
7
+ const userSchema = new Schema(
8
+ {
9
+ name: { type: String, index: true },
10
+ age: { type: Number, index: true },
11
+ gender: {
12
+ type: String,
13
+ enum: ['male', 'female', 'not-specified'],
14
+ },
15
+ someMixed: { type: Schema.Types.Mixed },
16
+ },
17
+ { collection: 'user_users' },
18
+ );
19
+
20
+ const schema = await loadGraphQLSchemaFromMongo('users', {
21
+ endpoint: 'mongodb://localhost:27017/test',
22
+ models: [
23
+ {
24
+ name: 'User',
25
+ schema: userSchema,
26
+ collection: 'user_users',
27
+ },
28
+ ],
29
+ });
30
+
31
+ const sdl = printSchemaWithDirectives(schema);
32
+
33
+ expect(sdl).toContain('@transport');
34
+ expect(sdl).toContain('kind: "mongo"');
35
+ expect(sdl).toContain('@mongo(');
36
+ expect(sdl).toContain('kind: FIND_ONE');
37
+ expect(sdl).toContain('kind: FIND_MANY');
38
+ expect(sdl).toContain('kind: COUNT');
39
+ expect(sdl).toContain('kind: CONNECTION');
40
+ expect(sdl).toContain('kind: PAGINATION');
41
+ expect(sdl).toContain('kind: INSERT_ONE');
42
+ expect(sdl).toContain('kind: UPDATE_ONE');
43
+ expect(sdl).toContain('kind: DELETE_ONE');
44
+ expect(sdl).toContain('User_findById');
45
+ expect(sdl).toContain('User_findByIds');
46
+ expect(sdl).toContain('User_findMany');
47
+ expect(sdl).toContain('User_connection');
48
+ expect(sdl).toContain('User_pagination');
49
+ expect(sdl).toContain('User_createOne');
50
+ expect(sdl).toContain('User_dataLoaderMany');
51
+ expect(sdl).toContain('filter: UserFilter');
52
+ expect(sdl).toContain('$args.filter');
53
+ expect(sdl).toContain('id: ID!');
54
+ expect(sdl).toContain('_id: ID!');
55
+ expect(sdl).not.toContain('where: UserWhere');
56
+ expect(sdl).toMatchSnapshot('user-full-slice');
57
+ });
58
+ });
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.