@omnigraph/sql 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.
Files changed (66) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/cjs/directives.js +59 -0
  3. package/dist/cjs/directives.js.map +7 -0
  4. package/dist/cjs/index.js +22 -0
  5. package/dist/cjs/index.js.map +7 -0
  6. package/dist/cjs/introspect/mysql.js +93 -0
  7. package/dist/cjs/introspect/postgres.js +106 -0
  8. package/dist/cjs/introspect/sqlite.js +60 -0
  9. package/dist/cjs/introspect/sqlite.js.map +7 -0
  10. package/dist/cjs/introspect/types.js +0 -0
  11. package/dist/cjs/introspect/types.js.map +7 -0
  12. package/dist/cjs/naming.js +54 -0
  13. package/dist/cjs/naming.js.map +7 -0
  14. package/dist/cjs/package.json +1 -0
  15. package/dist/cjs/schema.js +712 -0
  16. package/dist/cjs/schema.js.map +7 -0
  17. package/dist/cjs/type-map.js +40 -0
  18. package/dist/cjs/type-map.js.map +7 -0
  19. package/dist/esm/directives.js +55 -0
  20. package/dist/esm/directives.js.map +7 -0
  21. package/dist/esm/index.js +15 -0
  22. package/dist/esm/index.js.map +7 -0
  23. package/dist/esm/introspect/mysql.js +90 -0
  24. package/dist/esm/introspect/postgres.js +102 -0
  25. package/dist/esm/introspect/sqlite.js +57 -0
  26. package/dist/esm/introspect/sqlite.js.map +7 -0
  27. package/dist/esm/introspect/types.js +0 -0
  28. package/dist/esm/introspect/types.js.map +7 -0
  29. package/dist/esm/naming.js +50 -0
  30. package/dist/esm/naming.js.map +7 -0
  31. package/dist/esm/schema.js +707 -0
  32. package/dist/esm/schema.js.map +7 -0
  33. package/dist/esm/type-map.js +37 -0
  34. package/dist/esm/type-map.js.map +7 -0
  35. package/dist/package.json +54 -0
  36. package/dist/typings/directives.d.cts +17 -0
  37. package/dist/typings/directives.d.ts +17 -0
  38. package/dist/typings/index.d.cts +10 -0
  39. package/dist/typings/index.d.ts +10 -0
  40. package/dist/typings/introspect/mysql.d.cts +7 -0
  41. package/dist/typings/introspect/mysql.d.ts +7 -0
  42. package/dist/typings/introspect/postgres.d.cts +9 -0
  43. package/dist/typings/introspect/postgres.d.ts +9 -0
  44. package/dist/typings/introspect/sqlite.d.cts +8 -0
  45. package/dist/typings/introspect/sqlite.d.ts +8 -0
  46. package/dist/typings/introspect/types.d.cts +26 -0
  47. package/dist/typings/introspect/types.d.ts +26 -0
  48. package/dist/typings/naming.d.cts +17 -0
  49. package/dist/typings/naming.d.ts +17 -0
  50. package/dist/typings/schema.d.cts +39 -0
  51. package/dist/typings/schema.d.ts +39 -0
  52. package/dist/typings/type-map.d.cts +4 -0
  53. package/dist/typings/type-map.d.ts +4 -0
  54. package/package.json +61 -4
  55. package/src/directives.ts +67 -0
  56. package/src/index.ts +20 -0
  57. package/src/introspect/mysql.ts +110 -0
  58. package/src/introspect/postgres.ts +142 -0
  59. package/src/introspect/sqlite.ts +90 -0
  60. package/src/introspect/types.ts +31 -0
  61. package/src/naming.ts +67 -0
  62. package/src/schema.ts +834 -0
  63. package/src/type-map.ts +50 -0
  64. package/test/__snapshots__/sqlite-chinook.spec.ts.snap +328 -0
  65. package/test/sqlite-chinook.spec.ts +33 -0
  66. package/README.md +0 -3
@@ -0,0 +1,50 @@
1
+ import {
2
+ GraphQLBoolean,
3
+ GraphQLFloat,
4
+ GraphQLID,
5
+ GraphQLInt,
6
+ GraphQLString,
7
+ type GraphQLScalarType,
8
+ } from 'graphql';
9
+ import { GraphQLBigInt, GraphQLDate, GraphQLDateTime, GraphQLJSON } from 'graphql-scalars';
10
+
11
+ export function mapSqlTypeToGraphQL(
12
+ dataType: string,
13
+ opts?: { asId?: boolean },
14
+ ): GraphQLScalarType {
15
+ if (opts?.asId) {
16
+ return GraphQLID;
17
+ }
18
+ const t = dataType.toUpperCase();
19
+ if (t.includes('BOOL') || t === 'BIT') {
20
+ return GraphQLBoolean;
21
+ }
22
+ if (t.includes('INT') || t === 'SERIAL' || t === 'BIGSERIAL') {
23
+ if (t.includes('BIG') || t.includes('UNSIGNED')) {
24
+ return GraphQLBigInt as unknown as GraphQLScalarType;
25
+ }
26
+ return GraphQLInt;
27
+ }
28
+ if (
29
+ t.includes('DOUBLE') ||
30
+ t.includes('FLOAT') ||
31
+ t.includes('REAL') ||
32
+ t.includes('NUMERIC') ||
33
+ t.includes('DECIMAL')
34
+ ) {
35
+ return GraphQLFloat;
36
+ }
37
+ if (t.includes('TIMESTAMP') || t.includes('DATETIME')) {
38
+ return GraphQLDateTime as unknown as GraphQLScalarType;
39
+ }
40
+ if (t === 'DATE') {
41
+ return GraphQLDate as unknown as GraphQLScalarType;
42
+ }
43
+ if (t.includes('JSON') || t.includes('JSONB')) {
44
+ return GraphQLJSON as unknown as GraphQLScalarType;
45
+ }
46
+ if (t.includes('BLOB') || t.includes('BINARY') || t.includes('BYTEA')) {
47
+ return GraphQLString;
48
+ }
49
+ return GraphQLString;
50
+ }
@@ -0,0 +1,328 @@
1
+ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2
+
3
+ exports[`@omnigraph/sql sqlite chinook emits @sql directives and slim @transport(kind: sql): chinook-albums-artists 1`] = `
4
+ "schema @transport(subgraph: "chinook", kind: "sql", location: "file:<chinook.db>", options: {adapter: "sqlite"}) @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 @sql(subgraph: String, table: String!, kind: SqlKind!, where: String, orderBy: String, limit: String, offset: String, first: String, after: String, set: String, selection: String) on FIELD_DEFINITION | OBJECT
12
+
13
+ type Query {
14
+ album(id: ID!): Album @sql(subgraph: "chinook", table: "albums", kind: ROW, where: """
15
+
16
+ AlbumId: $args.id
17
+ """, selection: """
18
+ id: AlbumId
19
+ title: Title
20
+ artistId: ArtistId
21
+ """)
22
+ albums(where: AlbumFilter, orderBy: [AlbumOrderBy!], limit: Int = 10, offset: Int = 0): [Album!]! @sql(subgraph: "chinook", table: "albums", kind: ROWS, where: "$args.where", orderBy: "$args.orderBy", limit: "$args.limit", offset: "$args.offset", selection: """
23
+ id: AlbumId
24
+ title: Title
25
+ artistId: ArtistId
26
+ """)
27
+ allAlbums(where: AlbumFilter, orderBy: [AlbumOrderBy!], first: Int, after: String): AlbumConnection! @sql(subgraph: "chinook", table: "albums", kind: CONNECTION, where: "$args.where", orderBy: "$args.orderBy", first: "$args.first", after: "$args.after", selection: """
28
+ nodes {
29
+ id: AlbumId
30
+ title: Title
31
+ artistId: ArtistId
32
+ }
33
+ edges {
34
+ cursor: $cursor
35
+ node {
36
+ id: AlbumId
37
+ title: Title
38
+ artistId: ArtistId
39
+ }
40
+ }
41
+ pageInfo {
42
+ hasNextPage: $hasNextPage
43
+ hasPreviousPage: $hasPreviousPage
44
+ startCursor: $startCursor
45
+ endCursor: $endCursor
46
+ }
47
+ totalCount: $totalCount
48
+ """)
49
+ albumsCount(where: AlbumFilter): Int! @sql(subgraph: "chinook", table: "albums", kind: COUNT, where: "$args.where", selection: """
50
+
51
+ $
52
+ """)
53
+ artist(id: ID!): Artist @sql(subgraph: "chinook", table: "artists", kind: ROW, where: """
54
+
55
+ ArtistId: $args.id
56
+ """, selection: """
57
+ id: ArtistId
58
+ name: Name
59
+ """)
60
+ artists(where: ArtistFilter, orderBy: [ArtistOrderBy!], limit: Int = 10, offset: Int = 0): [Artist!]! @sql(subgraph: "chinook", table: "artists", kind: ROWS, where: "$args.where", orderBy: "$args.orderBy", limit: "$args.limit", offset: "$args.offset", selection: """
61
+ id: ArtistId
62
+ name: Name
63
+ """)
64
+ allArtists(where: ArtistFilter, orderBy: [ArtistOrderBy!], first: Int, after: String): ArtistConnection! @sql(subgraph: "chinook", table: "artists", kind: CONNECTION, where: "$args.where", orderBy: "$args.orderBy", first: "$args.first", after: "$args.after", selection: """
65
+ nodes {
66
+ id: ArtistId
67
+ name: Name
68
+ }
69
+ edges {
70
+ cursor: $cursor
71
+ node {
72
+ id: ArtistId
73
+ name: Name
74
+ }
75
+ }
76
+ pageInfo {
77
+ hasNextPage: $hasNextPage
78
+ hasPreviousPage: $hasPreviousPage
79
+ startCursor: $startCursor
80
+ endCursor: $endCursor
81
+ }
82
+ totalCount: $totalCount
83
+ """)
84
+ artistsCount(where: ArtistFilter): Int! @sql(subgraph: "chinook", table: "artists", kind: COUNT, where: "$args.where", selection: """
85
+
86
+ $
87
+ """)
88
+ }
89
+
90
+ type Album @key(fields: "id") @sql(subgraph: "chinook", table: "albums", kind: ROW, where: """
91
+
92
+ AlbumId: $this.id
93
+ """, selection: """
94
+ id: AlbumId
95
+ title: Title
96
+ artistId: ArtistId
97
+ """) {
98
+ id: ID!
99
+ title: String!
100
+ artistId: Int!
101
+ artist: Artist @sql(subgraph: "chinook", table: "artists", kind: ROW, where: """
102
+
103
+ ArtistId: $this.artistId
104
+ """, selection: """
105
+ id: ArtistId
106
+ name: Name
107
+ """)
108
+ }
109
+
110
+ type Artist @key(fields: "id") @sql(subgraph: "chinook", table: "artists", kind: ROW, where: """
111
+
112
+ ArtistId: $this.id
113
+ """, selection: """
114
+ id: ArtistId
115
+ name: Name
116
+ """) {
117
+ id: ID!
118
+ name: String
119
+ albums(where: AlbumFilter, orderBy: [AlbumOrderBy!], limit: Int, offset: Int): [Album!]! @sql(subgraph: "chinook", table: "albums", kind: ROWS, where: """
120
+ ArtistId: $this.id
121
+ ...$args.where
122
+ """, orderBy: "$args.orderBy", limit: "$args.limit", offset: "$args.offset", selection: """
123
+ id: AlbumId
124
+ title: Title
125
+ artistId: ArtistId
126
+ """)
127
+ }
128
+
129
+ input AlbumFilter {
130
+ and: [AlbumFilter!]
131
+ or: [AlbumFilter!]
132
+ not: AlbumFilter
133
+ id: StringFilter
134
+ title: StringFilter
135
+ artistId: IntFilter
136
+ }
137
+
138
+ input StringFilter {
139
+ eq: String
140
+ ne: String
141
+ in: [String!]
142
+ nin: [String!]
143
+ lt: String
144
+ lte: String
145
+ gt: String
146
+ gte: String
147
+ includes: String
148
+ startsWith: String
149
+ endsWith: String
150
+ isNull: Boolean
151
+ }
152
+
153
+ input IntFilter {
154
+ eq: Int
155
+ ne: Int
156
+ in: [Int!]
157
+ nin: [Int!]
158
+ lt: Int
159
+ lte: Int
160
+ gt: Int
161
+ gte: Int
162
+ isNull: Boolean
163
+ }
164
+
165
+ enum AlbumOrderBy {
166
+ ID_ASC
167
+ ID_DESC
168
+ TITLE_ASC
169
+ TITLE_DESC
170
+ ARTIST_ID_ASC
171
+ ARTIST_ID_DESC
172
+ }
173
+
174
+ type AlbumConnection {
175
+ edges: [AlbumEdge!]!
176
+ pageInfo: PageInfo!
177
+ nodes: [Album!]!
178
+ totalCount: Int!
179
+ }
180
+
181
+ type AlbumEdge {
182
+ cursor: String!
183
+ node: Album!
184
+ }
185
+
186
+ type PageInfo {
187
+ hasNextPage: Boolean!
188
+ hasPreviousPage: Boolean!
189
+ startCursor: String
190
+ endCursor: String
191
+ }
192
+
193
+ input ArtistFilter {
194
+ and: [ArtistFilter!]
195
+ or: [ArtistFilter!]
196
+ not: ArtistFilter
197
+ id: StringFilter
198
+ name: StringFilter
199
+ }
200
+
201
+ enum ArtistOrderBy {
202
+ ID_ASC
203
+ ID_DESC
204
+ NAME_ASC
205
+ NAME_DESC
206
+ }
207
+
208
+ type ArtistConnection {
209
+ edges: [ArtistEdge!]!
210
+ pageInfo: PageInfo!
211
+ nodes: [Artist!]!
212
+ totalCount: Int!
213
+ }
214
+
215
+ type ArtistEdge {
216
+ cursor: String!
217
+ node: Artist!
218
+ }
219
+
220
+ type Mutation {
221
+ createAlbum(input: CreateAlbumInput!): AlbumPayload! @sql(subgraph: "chinook", table: "albums", kind: INSERT, set: """
222
+ Title: $args.input.title
223
+ ArtistId: $args.input.artistId
224
+ """, selection: """
225
+ album {
226
+ id: AlbumId
227
+ title: Title
228
+ artistId: ArtistId
229
+ }
230
+ """)
231
+ updateAlbum(id: ID!, input: UpdateAlbumInput!): AlbumPayload! @sql(subgraph: "chinook", table: "albums", kind: UPDATE, set: """
232
+ Title: $args.input.title
233
+ ArtistId: $args.input.artistId
234
+ """, where: """
235
+
236
+ AlbumId: $args.id
237
+ """, selection: """
238
+ album {
239
+ id: AlbumId
240
+ title: Title
241
+ artistId: ArtistId
242
+ }
243
+ """)
244
+ deleteAlbum(id: ID!): DeleteAlbumPayload! @sql(subgraph: "chinook", table: "albums", kind: DELETE, where: """
245
+
246
+ AlbumId: $args.id
247
+ """, selection: """
248
+ ok: $affectedRows
249
+ affectedRows: $affectedRows
250
+ """)
251
+ createArtist(input: CreateArtistInput!): ArtistPayload! @sql(subgraph: "chinook", table: "artists", kind: INSERT, set: """
252
+
253
+ Name: $args.input.name
254
+ """, selection: """
255
+ artist {
256
+ id: ArtistId
257
+ name: Name
258
+ }
259
+ """)
260
+ updateArtist(id: ID!, input: UpdateArtistInput!): ArtistPayload! @sql(subgraph: "chinook", table: "artists", kind: UPDATE, set: """
261
+
262
+ Name: $args.input.name
263
+ """, where: """
264
+
265
+ ArtistId: $args.id
266
+ """, selection: """
267
+ artist {
268
+ id: ArtistId
269
+ name: Name
270
+ }
271
+ """)
272
+ deleteArtist(id: ID!): DeleteArtistPayload! @sql(subgraph: "chinook", table: "artists", kind: DELETE, where: """
273
+
274
+ ArtistId: $args.id
275
+ """, selection: """
276
+ ok: $affectedRows
277
+ affectedRows: $affectedRows
278
+ """)
279
+ }
280
+
281
+ type AlbumPayload {
282
+ album: Album
283
+ }
284
+
285
+ input CreateAlbumInput {
286
+ title: String!
287
+ artistId: Int!
288
+ }
289
+
290
+ input UpdateAlbumInput {
291
+ title: String
292
+ artistId: Int
293
+ }
294
+
295
+ type DeleteAlbumPayload {
296
+ ok: Boolean!
297
+ affectedRows: Int!
298
+ }
299
+
300
+ type ArtistPayload {
301
+ artist: Artist
302
+ }
303
+
304
+ input CreateArtistInput {
305
+ name: String
306
+ }
307
+
308
+ input UpdateArtistInput {
309
+ name: String
310
+ }
311
+
312
+ type DeleteArtistPayload {
313
+ ok: Boolean!
314
+ affectedRows: Int!
315
+ }
316
+
317
+ scalar TransportOptions
318
+
319
+ enum SqlKind {
320
+ ROW
321
+ ROWS
322
+ CONNECTION
323
+ COUNT
324
+ INSERT
325
+ UPDATE
326
+ DELETE
327
+ }"
328
+ `;
@@ -0,0 +1,33 @@
1
+ import { join } from 'node:path';
2
+ import { printSchemaWithDirectives } from '@graphql-tools/utils';
3
+ import { loadGraphQLSchemaFromSql } from '../src/index.js';
4
+
5
+ const chinookDb = join(__dirname, '../../../../e2e/sqlite-chinook/chinook.db');
6
+
7
+ describe('@omnigraph/sql sqlite chinook', () => {
8
+ it('emits @sql directives and slim @transport(kind: sql)', async () => {
9
+ // Adapter inferred from path / file: (Bun-style; adapter optional)
10
+ const schema = await loadGraphQLSchemaFromSql('chinook', {
11
+ endpoint: chinookDb,
12
+ includeTables: ['albums', 'artists'],
13
+ });
14
+ // Normalize absolute DB path so the snapshot is portable across machines/CI.
15
+ const sdl = printSchemaWithDirectives(schema).replace(
16
+ /location: "file:[^"]+"/,
17
+ 'location: "file:<chinook.db>"',
18
+ );
19
+ expect(sdl).toContain('@transport');
20
+ expect(sdl).toContain('kind: "sql"');
21
+ expect(sdl).toContain('adapter: "sqlite"');
22
+ expect(sdl).toContain('@sql(');
23
+ expect(sdl).toContain('kind: INSERT');
24
+ expect(sdl).toContain('kind: ROW');
25
+ expect(sdl).toContain('type Album');
26
+ expect(sdl).toContain('type Artist');
27
+ expect(sdl).toContain('allAlbums');
28
+ // Type-level entity resolver (not Query entity: true)
29
+ expect(sdl).toMatch(/type Album[\s\S]*@sql\([\s\S]*\$this\.id/);
30
+ expect(sdl).not.toContain('entity: true');
31
+ expect(sdl).toMatchSnapshot('chinook-albums-artists');
32
+ });
33
+ });
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.