apollo-conn-gen 0.0.2

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 (146) hide show
  1. package/dist/cli/json.d.ts +1 -0
  2. package/dist/cli/json.js +70 -0
  3. package/dist/cli/json.js.map +1 -0
  4. package/dist/cli/oas-helpers/index.d.ts +4 -0
  5. package/dist/cli/oas-helpers/index.js +54 -0
  6. package/dist/cli/oas-helpers/index.js.map +1 -0
  7. package/dist/cli/oas.d.ts +1 -0
  8. package/dist/cli/oas.js +49 -0
  9. package/dist/cli/oas.js.map +1 -0
  10. package/dist/index.d.ts +2 -0
  11. package/dist/index.js +4 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/json/index.d.ts +4 -0
  14. package/dist/json/index.js +5 -0
  15. package/dist/json/index.js.map +1 -0
  16. package/dist/json/io/index.d.ts +1 -0
  17. package/dist/json/io/index.js +2 -0
  18. package/dist/json/io/index.js.map +1 -0
  19. package/dist/json/io/writer.d.ts +15 -0
  20. package/dist/json/io/writer.js +45 -0
  21. package/dist/json/io/writer.js.map +1 -0
  22. package/dist/json/walker/index.d.ts +2 -0
  23. package/dist/json/walker/index.js +3 -0
  24. package/dist/json/walker/index.js.map +1 -0
  25. package/dist/json/walker/jsonContext.d.ts +12 -0
  26. package/dist/json/walker/jsonContext.js +42 -0
  27. package/dist/json/walker/jsonContext.js.map +1 -0
  28. package/dist/json/walker/jsonGen.d.ts +19 -0
  29. package/dist/json/walker/jsonGen.js +209 -0
  30. package/dist/json/walker/jsonGen.js.map +1 -0
  31. package/dist/json/walker/log/trace.d.ts +3 -0
  32. package/dist/json/walker/log/trace.js +11 -0
  33. package/dist/json/walker/log/trace.js.map +1 -0
  34. package/dist/json/walker/naming.d.ts +8 -0
  35. package/dist/json/walker/naming.js +66 -0
  36. package/dist/json/walker/naming.js.map +1 -0
  37. package/dist/json/walker/types/index.d.ts +4 -0
  38. package/dist/json/walker/types/index.js +5 -0
  39. package/dist/json/walker/types/index.js.map +1 -0
  40. package/dist/json/walker/types/jsonArray.d.ts +13 -0
  41. package/dist/json/walker/types/jsonArray.js +70 -0
  42. package/dist/json/walker/types/jsonArray.js.map +1 -0
  43. package/dist/json/walker/types/jsonObj.d.ts +17 -0
  44. package/dist/json/walker/types/jsonObj.js +76 -0
  45. package/dist/json/walker/types/jsonObj.js.map +1 -0
  46. package/dist/json/walker/types/jsonScalar.d.ts +12 -0
  47. package/dist/json/walker/types/jsonScalar.js +39 -0
  48. package/dist/json/walker/types/jsonScalar.js.map +1 -0
  49. package/dist/json/walker/types/jsonType.d.ts +16 -0
  50. package/dist/json/walker/types/jsonType.js +30 -0
  51. package/dist/json/walker/types/jsonType.js.map +1 -0
  52. package/dist/oas/index.d.ts +14 -0
  53. package/dist/oas/index.js +15 -0
  54. package/dist/oas/index.js.map +1 -0
  55. package/dist/oas/io/writer.d.ts +25 -0
  56. package/dist/oas/io/writer.js +313 -0
  57. package/dist/oas/io/writer.js.map +1 -0
  58. package/dist/oas/log/trace.d.ts +3 -0
  59. package/dist/oas/log/trace.js +7 -0
  60. package/dist/oas/log/trace.js.map +1 -0
  61. package/dist/oas/nodes/arr.d.ts +15 -0
  62. package/dist/oas/nodes/arr.js +47 -0
  63. package/dist/oas/nodes/arr.js.map +1 -0
  64. package/dist/oas/nodes/circularRef.d.ts +13 -0
  65. package/dist/oas/nodes/circularRef.js +39 -0
  66. package/dist/oas/nodes/circularRef.js.map +1 -0
  67. package/dist/oas/nodes/comp.d.ts +18 -0
  68. package/dist/oas/nodes/comp.js +156 -0
  69. package/dist/oas/nodes/comp.js.map +1 -0
  70. package/dist/oas/nodes/en.d.ts +14 -0
  71. package/dist/oas/nodes/en.js +57 -0
  72. package/dist/oas/nodes/en.js.map +1 -0
  73. package/dist/oas/nodes/factory.d.ts +17 -0
  74. package/dist/oas/nodes/factory.js +174 -0
  75. package/dist/oas/nodes/factory.js.map +1 -0
  76. package/dist/oas/nodes/get.d.ts +24 -0
  77. package/dist/oas/nodes/get.js +167 -0
  78. package/dist/oas/nodes/get.js.map +1 -0
  79. package/dist/oas/nodes/obj.d.ts +15 -0
  80. package/dist/oas/nodes/obj.js +177 -0
  81. package/dist/oas/nodes/obj.js.map +1 -0
  82. package/dist/oas/nodes/param/param.d.ts +17 -0
  83. package/dist/oas/nodes/param/param.js +64 -0
  84. package/dist/oas/nodes/param/param.js.map +1 -0
  85. package/dist/oas/nodes/props/prop.d.ts +12 -0
  86. package/dist/oas/nodes/props/prop.js +33 -0
  87. package/dist/oas/nodes/props/prop.js.map +1 -0
  88. package/dist/oas/nodes/props/propArray.d.ts +15 -0
  89. package/dist/oas/nodes/props/propArray.js +75 -0
  90. package/dist/oas/nodes/props/propArray.js.map +1 -0
  91. package/dist/oas/nodes/props/propObj.d.ts +16 -0
  92. package/dist/oas/nodes/props/propObj.js +70 -0
  93. package/dist/oas/nodes/props/propObj.js.map +1 -0
  94. package/dist/oas/nodes/props/propRef.d.ts +19 -0
  95. package/dist/oas/nodes/props/propRef.js +108 -0
  96. package/dist/oas/nodes/props/propRef.js.map +1 -0
  97. package/dist/oas/nodes/props/propScalar.d.ts +16 -0
  98. package/dist/oas/nodes/props/propScalar.js +47 -0
  99. package/dist/oas/nodes/props/propScalar.js.map +1 -0
  100. package/dist/oas/nodes/props/referenceObject.d.ts +3 -0
  101. package/dist/oas/nodes/props/referenceObject.js +2 -0
  102. package/dist/oas/nodes/props/referenceObject.js.map +1 -0
  103. package/dist/oas/nodes/ref.d.ts +17 -0
  104. package/dist/oas/nodes/ref.js +70 -0
  105. package/dist/oas/nodes/ref.js.map +1 -0
  106. package/dist/oas/nodes/response.d.ts +14 -0
  107. package/dist/oas/nodes/response.js +48 -0
  108. package/dist/oas/nodes/response.js.map +1 -0
  109. package/dist/oas/nodes/scalar.d.ts +12 -0
  110. package/dist/oas/nodes/scalar.js +26 -0
  111. package/dist/oas/nodes/scalar.js.map +1 -0
  112. package/dist/oas/nodes/type.d.ts +43 -0
  113. package/dist/oas/nodes/type.js +107 -0
  114. package/dist/oas/nodes/type.js.map +1 -0
  115. package/dist/oas/nodes/union.d.ts +16 -0
  116. package/dist/oas/nodes/union.js +133 -0
  117. package/dist/oas/nodes/union.js.map +1 -0
  118. package/dist/oas/oasContext.d.ts +23 -0
  119. package/dist/oas/oasContext.js +73 -0
  120. package/dist/oas/oasContext.js.map +1 -0
  121. package/dist/oas/oasGen.d.ts +28 -0
  122. package/dist/oas/oasGen.js +166 -0
  123. package/dist/oas/oasGen.js.map +1 -0
  124. package/dist/oas/prompts/base/theme.d.ts +1 -0
  125. package/dist/oas/prompts/base/theme.js +50 -0
  126. package/dist/oas/prompts/base/theme.js.map +1 -0
  127. package/dist/oas/prompts/base/utils.d.ts +3 -0
  128. package/dist/oas/prompts/base/utils.js +8 -0
  129. package/dist/oas/prompts/base/utils.js.map +1 -0
  130. package/dist/oas/prompts/prompt.d.ts +19 -0
  131. package/dist/oas/prompts/prompt.js +176 -0
  132. package/dist/oas/prompts/prompt.js.map +1 -0
  133. package/dist/oas/prompts/theme.d.ts +126 -0
  134. package/dist/oas/prompts/theme.js +2 -0
  135. package/dist/oas/prompts/theme.js.map +1 -0
  136. package/dist/oas/utils/gql.d.ts +5 -0
  137. package/dist/oas/utils/gql.js +35 -0
  138. package/dist/oas/utils/gql.js.map +1 -0
  139. package/dist/oas/utils/naming.d.ts +16 -0
  140. package/dist/oas/utils/naming.js +143 -0
  141. package/dist/oas/utils/naming.js.map +1 -0
  142. package/dist/oas/utils/typeUtils.d.ts +6 -0
  143. package/dist/oas/utils/typeUtils.js +25 -0
  144. package/dist/oas/utils/typeUtils.js.map +1 -0
  145. package/package.json +77 -0
  146. package/readme.md +383 -0
package/readme.md ADDED
@@ -0,0 +1,383 @@
1
+ # OAS to Apollo Connector Generator
2
+
3
+ ## Introduction
4
+
5
+ This project is a library designed to convert an OpenAPI Specification (OAS) file (YAML or JSON) into an Apollo GraphQL Connector. It also includes CLI tools to facilitate this conversion process.
6
+
7
+ Key features:
8
+
9
+ * Generates an Apollo Connector from an OAS specification, converting all types and `GET` entry points defined in the spec (*only* `GET` methods are supported for now)
10
+ * Generates a schema based on a single or a collection of`JSON` files.
11
+
12
+ ## Prerequisites
13
+
14
+ * [Node.js](https://nodejs.org/) version 18 or higher. Built using Typescript 5.1.6.
15
+
16
+ ## Installation
17
+
18
+ 1. **Clone the Repository**:
19
+
20
+ ```bash
21
+ git clone https://github.com/fernando-apollo/oas-to-connector.git
22
+ cd oas-helpers-to-connector
23
+ ```
24
+
25
+ 2. **Install Dependencies**:
26
+
27
+ ```bash
28
+ npm install
29
+ ```
30
+
31
+ 3. **Build the Project**:
32
+
33
+ ```bash
34
+ npm run build
35
+ ```
36
+
37
+ ## Running the `cli/oas` tool
38
+
39
+ To generate an Apollo Connector from your OAS file, run:
40
+
41
+ ```bash
42
+ node ./dist/cli/oas <path-to-oas-spec>
43
+ ```
44
+
45
+ Replace `<path-to-oas-spec>` with the relative or absolute path to your OAS YAML or JSON file.
46
+
47
+ ### Example with *Petstore*
48
+
49
+ *Note: the petstore spec can be downloaded from (<https://petstore3.swagger.io>)*
50
+
51
+ ```bash
52
+ node ./dist/cli/oas ./tests/resources/petstore.yaml
53
+ ```
54
+
55
+ The output should be similar to the following:
56
+ ![Screenshot showing a list of paths available to generate](./docs/screenshot-01.png)
57
+
58
+ ## Running the `cli/json` tool
59
+
60
+ To generate an Apollo Connector from a `JSON` (or a set of) file(s) you can use the `json` command:
61
+
62
+ ```bash
63
+ node ./dist/cli/json <file|folder>
64
+ ```
65
+
66
+ Replace `<file|folder>` with a path to a `JSON` file or a folder that contains `JSON` files.
67
+
68
+ ### Example with the following `JSON` payload
69
+
70
+ If we have a file `tests/resources/json/preferences/user/50.json` with the following contents:
71
+
72
+ ```json
73
+ {
74
+ "userId": 50,
75
+ "favouriteTeams": ["Luton"],
76
+ "favouriteLeagues": [
77
+ "premier-league",
78
+ "championship",
79
+ "scottish-premiership"
80
+ ],
81
+ "joiningDate": "2023-12-11"
82
+ }
83
+ ```
84
+
85
+ Then running the tool with
86
+
87
+ ```shell
88
+ node ./dist/cli/json tests/resources/json/preferences/user/50.json
89
+ ```
90
+
91
+ Will result in the following Apollo connector schema:
92
+
93
+ ```graphql
94
+ extend schema
95
+ @link(url: "https://specs.apollo.dev/federation/v2.10", import: ["@key"])
96
+ @link(
97
+ url: "https://specs.apollo.dev/connect/v0.1"
98
+ import: ["@connect", "@source"]
99
+ )
100
+ @source(name: "api", http: { baseURL: "http://localhost:4010" })
101
+
102
+ type Root {
103
+ userId: Int
104
+ favouriteTeams: [String]
105
+ joiningDate: String
106
+ favouriteLeagues: [String]
107
+ }
108
+
109
+ type Query {
110
+ root: Root
111
+ @connect(
112
+ source: "api"
113
+ http: { GET: "/test" }
114
+ selection: """
115
+ userId
116
+ favouriteTeams
117
+ joiningDate
118
+ favouriteLeagues
119
+ """
120
+ )}
121
+ ```
122
+
123
+ ## Using the `apollo-conn-gen` library
124
+
125
+ The library provides two entry classes:
126
+
127
+ * `OasGen`, for generating from OAS specifications, and
128
+ * `JsonGen` for working with `JSON` files
129
+
130
+ ### Installation for JS/TS projects
131
+
132
+ In your project, run
133
+
134
+ ```shell
135
+ npm i "apollo-conn-gen`
136
+ ```
137
+
138
+ to install the library. Next, in your JS/TS file yu can import the tools using
139
+
140
+ ```typescript
141
+ import { OasGen } from "apollo-conn-gen/oas"
142
+ import { JsonGen } from "apollo-conn-gen/json"
143
+ ```
144
+
145
+ ## Additional details
146
+
147
+ ### Detailed usage for the `oas` CLI
148
+
149
+ Navigate using the `arrow` keys and select the fields you want to include in the generated connector schema using the 'x' key. Other options are:
150
+
151
+ * `a` to select all fields in the current type, or
152
+ * `n` key to deselect all fields.
153
+
154
+ Once you've made your selection, press the `Enter` key to generate the Apollo Connector.
155
+
156
+ Here's an example of the output when selecting all the fields from `[GET] /pet/{petId`:
157
+
158
+ ```graphql
159
+ extend schema
160
+ @link(url: "https://specs.apollo.dev/federation/v2.10", import: ["@key"])
161
+ @link(
162
+ url: "https://specs.apollo.dev/connect/v0.1"
163
+ import: ["@connect", "@source"]
164
+ )
165
+ @source(name: "api", http: { baseURL: "https://petstore3.swagger.io/v3" })
166
+
167
+ scalar JSON
168
+
169
+ type Pet {
170
+ category: Category
171
+ id: Int
172
+ name: String
173
+ photoUrls: [String]
174
+ "pet status in the store"
175
+ status: String
176
+ tags: [Tag]
177
+ }
178
+
179
+ type Category {
180
+ id: Int
181
+ name: String
182
+ }
183
+
184
+ type Tag {
185
+ id: Int
186
+ name: String
187
+ }
188
+
189
+ type Query {
190
+ """
191
+ Find pet by ID (/pet/{petId})
192
+ """
193
+ petByPetId(petId: Int!): Pet
194
+ @connect(
195
+ source: "api"
196
+ http: { GET: "/pet/{$args.petId}" }
197
+ selection: """
198
+ category {
199
+ id
200
+ name
201
+ }
202
+ id
203
+ name
204
+ photoUrls
205
+ status
206
+ tags {
207
+ id
208
+ name
209
+ }
210
+ """
211
+ )
212
+ }
213
+ ```
214
+
215
+ ## Options
216
+
217
+ * `-i, --skip-validation`: Skip the validation step (default: `false`).
218
+ * `-n, --skip-selection`: Generate all filtered paths without prompting for selection (default: `false`).
219
+ * `-l, --list-paths`: Only list the paths that can be generated (default: `false`).
220
+
221
+ For a complete list of options, run:
222
+
223
+ ```bash
224
+ node ./dist/cli/oas -h
225
+ ```
226
+
227
+ ### Filtering paths
228
+
229
+ The tool allows filtering the list of paths using a regular expression. This is useful when you have large specs and only want to generate (or list) a subset. As shown above, you can list all the paths using the `-l` flag:
230
+
231
+ ```shell
232
+ node ./dist/cli/oas ./tests/petstore.yaml --list-paths
233
+
234
+ get:/pet/{petId}
235
+ get:/pet/findByStatus
236
+ get:/pet/findByTags
237
+ get:/store/inventory
238
+ get:/store/order/{orderId}
239
+ get:/user/{username}
240
+ get:/user/login
241
+ get:/user/logout
242
+ ```
243
+
244
+ If you'd like to filter the paths using a regular expression, you can use the `-g` flag. For example, to only list the operations ending with an argument, you can use the following command:
245
+
246
+ ```shell
247
+ node ./dist/cli/oas ./tests/petstore.yaml --list-paths --grep "{\\w+}$"
248
+
249
+ get:/pet/{petId}
250
+ get:/store/order/{orderId}
251
+ ```
252
+
253
+ or, for instance, filtering by a specific path:
254
+
255
+ ```shell
256
+ node ./dist/cli/oas ./tests/petstore.yaml --list-paths --grep "/pet/"
257
+
258
+ get:/pet/{petId}
259
+ get:/pet/findByTags
260
+ ```
261
+
262
+ ### Skipping validation
263
+
264
+ By default, the tool will validate the OAS specification before generating the Apollo Connector. However, sometimes specifications are not fully compliant with the OAS standard, or you may want to skip this step for other reasons. To do so, simply add the `-i` (or `--skip-validation`) flag to the command.
265
+
266
+ ### Page size
267
+
268
+ When selecting paths, the tool will display a list of paths with a default page size of `10`. You can change this value using the `-p` (or `--page-size`) flag. For example, to display `40` rows per page, you can use the following command:
269
+
270
+ ```shell
271
+ node ./dist/cli/oas ./tests/petstore.yaml --page-size 40
272
+ ```
273
+
274
+ ## Generating a connector from an existing selection set
275
+
276
+ When a connector is generated, the tool also outputs the list of selected fields as paths. This list can then be used to generate a connector from a file without the need to select the fields again.
277
+
278
+ To do so, save the output to a file in `JSON` format and run the tool with the `-s` (or `--load-selections`) flag and the path to the file.
279
+
280
+ ### Example
281
+
282
+ File: `tests/sample-petstore-selection.json`:
283
+
284
+ ```json
285
+ [
286
+ "get:/pet/{petId}>res:r>ref:#/c/s/Pet>obj:#/c/s/Pet>prop:scalar:id",
287
+ "get:/pet/{petId}>res:r>ref:#/c/s/Pet>obj:#/c/s/Pet>prop:scalar:name",
288
+ "get:/pet/{petId}>res:r>ref:#/c/s/Pet>obj:#/c/s/Pet>prop:array:#photoUrls",
289
+ "get:/pet/{petId}>res:r>ref:#/c/s/Pet>obj:#/c/s/Pet>prop:scalar:status"
290
+ ]
291
+ ```
292
+
293
+ Running the following command:
294
+
295
+ ```shell
296
+ node ./dist/cli/oas -s tests/sample-petstore-selection.json tests/petstore.yaml
297
+ ```
298
+
299
+ will output the following:
300
+
301
+ ```graphql
302
+ --------------- Apollo Connector schema -----------------
303
+ extend schema
304
+ @link(url: "https://specs.apollo.dev/federation/v2.10", import: ["@key"])
305
+ @link(
306
+ url: "https://specs.apollo.dev/connect/v0.1"
307
+ import: ["@connect", "@source"]
308
+ )
309
+ @source(name: "api", http: { baseURL: "https://petstore3.swagger.io/v3" })
310
+
311
+
312
+ scalar JSON
313
+
314
+ type Pet {
315
+ id: Int
316
+ name: String
317
+ photoUrls: [String]
318
+ "pet status in the store"
319
+ status: String
320
+ }
321
+
322
+ type Query {
323
+ """
324
+ Find pet by ID (/pet/{petId})
325
+ """
326
+ petByPetId(petId: Int!): Pet
327
+ @connect(
328
+ source: "api"
329
+ http: { GET: "/pet/{$args.petId}"
330
+ }
331
+ selection: """
332
+ id
333
+ name
334
+ photoUrls
335
+ status
336
+ """
337
+ )
338
+ }
339
+ ```
340
+
341
+ ## Generating all paths
342
+
343
+ Whilst this option is not recommended for large specifications, you can generate all paths without prompting for a specific selection. To do so, you can use the `-n` (or `--skip-selection`) flag. This may result in a very large Apollo Connector schema, might take a long time to process and not be particularly useful, so use with caution.
344
+
345
+ ## Buildling the library
346
+
347
+ The tool can be built as a library to use in other projects. To do this, simply run
348
+
349
+ ```shell
350
+ npm run lib
351
+ ```
352
+
353
+ Which will build everything under the `./dist` folder:
354
+
355
+ ```shell
356
+ ls dist/
357
+ index.d.ts index.esm.js index.esm.js.map index.js index.js.map
358
+ ```
359
+
360
+ ### Detailed usage for the `cli/json` tool
361
+
362
+ ```shell
363
+ node ./dist/cli/json -h
364
+ Usage: json [options] <file|folder>
365
+
366
+ Arguments:
367
+ file|folder A single JSON file or a folder with a collection of JSON files
368
+
369
+ Options:
370
+ -V, --version output the version number
371
+ -s --schema-types Output the GraphQL schema types (default: false)
372
+ -e --selection-set Output the Apollo Connector selection set (default: false)
373
+ -o --output-file <file> Where to write the output (default: "stdout")
374
+ -h, --help display help for command
375
+ ```
376
+
377
+ The CLI options affect what is generated by the tool. There are three possibilities:
378
+
379
+ * generate the whole connector schema,
380
+ * generate only the types for the schema, or
381
+ * generate the selection set
382
+
383
+ The `-o` (or `--output-file`) allows sending the output to a file instead of the console.