@bidirekt/typescript-sdk-cli 0.3.0 → 0.4.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/README.md
CHANGED
|
@@ -20,7 +20,7 @@ With decorators, your app imports them at runtime, so install them as a regular
|
|
|
20
20
|
npm install @bidirekt/typescript-sdk
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
Their types need TypeScript 5.0 or later. GraphQL contracts need the Bidirekt CLI and broker 0.4.0 or later.
|
|
23
|
+
Their types need TypeScript 5.0 or later. GraphQL contracts need the Bidirekt CLI and broker 0.4.0 or later, and `__typename` the broker 0.5.0. `bidirekt validate` and `bidirekt publish` without file arguments need the CLI 0.5.0.
|
|
24
24
|
|
|
25
25
|
## Quickstart
|
|
26
26
|
|
|
@@ -147,7 +147,10 @@ export class PetsController {
|
|
|
147
147
|
if (typeof body.name !== 'string') {
|
|
148
148
|
throw new BadRequestException()
|
|
149
149
|
}
|
|
150
|
-
const pet = Object.assign(new Pet(), {
|
|
150
|
+
const pet = Object.assign(new Pet(), {
|
|
151
|
+
petId: this.pets.size + 1,
|
|
152
|
+
...body,
|
|
153
|
+
})
|
|
151
154
|
this.pets.set(pet.petId, pet)
|
|
152
155
|
return pet
|
|
153
156
|
}
|
|
@@ -170,7 +173,8 @@ export class RatingsClient {
|
|
|
170
173
|
})
|
|
171
174
|
async find(petId: number): Promise<Rating | null> {
|
|
172
175
|
const response = await fetch(`http://petstore-reviews/ratings/${petId}`)
|
|
173
|
-
|
|
176
|
+
if (response.status === 404) return null
|
|
177
|
+
return (await response.json()) as Rating
|
|
174
178
|
}
|
|
175
179
|
}
|
|
176
180
|
```
|
|
@@ -299,7 +303,8 @@ import type { Rating } from './pet.ts'
|
|
|
299
303
|
*/
|
|
300
304
|
export async function findRating(petId: number): Promise<Rating | null> {
|
|
301
305
|
const response = await fetch(`http://petstore-reviews/ratings/${petId}`)
|
|
302
|
-
|
|
306
|
+
if (response.status === 404) return null
|
|
307
|
+
return (await response.json()) as Rating
|
|
303
308
|
}
|
|
304
309
|
```
|
|
305
310
|
|
|
@@ -517,17 +522,17 @@ You never edit `contracts/generated.yaml` by hand: the next `generate` writes ov
|
|
|
517
522
|
|
|
518
523
|
```sh
|
|
519
524
|
npx @bidirekt/typescript-sdk-cli generate
|
|
520
|
-
bidirekt validate
|
|
521
|
-
bidirekt validate
|
|
522
|
-
bidirekt publish
|
|
525
|
+
bidirekt validate
|
|
526
|
+
bidirekt validate --participant petstore_ts --environment production
|
|
527
|
+
bidirekt publish --participant petstore_ts --version 1.4.0
|
|
523
528
|
bidirekt can-i-deploy petstore_ts --version 1.4.0 --environment production
|
|
524
529
|
# deploy
|
|
525
530
|
bidirekt record-deployment petstore_ts --version 1.4.0 --environment production
|
|
526
531
|
```
|
|
527
532
|
|
|
528
|
-
`validate` without `--participant` and `--environment` checks only the file; with them, it also compares the file with what is deployed in that environment. When `can-i-deploy` reports a break, fix the annotation or the type in the code and generate again.
|
|
533
|
+
Without file arguments, `validate` and `publish` read the `contracts/` folder of the current directory. `validate` without `--participant` and `--environment` checks only the file; with them, it also compares the file with what is deployed in that environment. When `can-i-deploy` reports a break, fix the annotation or the type in the code and generate again.
|
|
529
534
|
|
|
530
|
-
A subgraph, a service that declares its graph with `@BidirektGraph` or `@bidirekt graph`, validates and publishes its own GraphQL schema together with the contract: `bidirekt validate contracts/
|
|
535
|
+
A subgraph, a service that declares its graph with `@BidirektGraph` or `@bidirekt graph`, validates and publishes its own GraphQL schema together with the contract: `bidirekt validate contracts/ src/schema.graphql`, and the same in `publish`. `contracts/` alone fails with `graph "petstore_graph" without a GraphQL schema`.
|
|
531
536
|
|
|
532
537
|
A package of your workspace, which npm links into `node_modules`, is read as your code when its `types` or `exports` point at its source. A class, interface or type reached through its built `.d.ts` stops `generate`.
|
|
533
538
|
|
|
@@ -596,11 +601,12 @@ Without a request (no `request` option, no `@bidirekt request` tag), the resourc
|
|
|
596
601
|
| a list or null | `{ list: Pet, nullable: true }` | `Pet[]?` | `PetListOrNull` |
|
|
597
602
|
| a list of items or null | `{ list: Pet, nullableItems: true }` | `Pet?[]` | `PetOrNullList` |
|
|
598
603
|
| a primitive | `'boolean'` | `boolean` | `Boolean` |
|
|
604
|
+
| a named primitive | `{ type: 'boolean', name: 'Flag' }` | `boolean as Flag` | `Flag` |
|
|
599
605
|
| a custom scalar | `{ scalar: 'DateTime' }` | `scalar:DateTime` | `DateTime` |
|
|
600
606
|
| an enum | `{ enum: ['AVAILABLE', 'SOLD'], name: 'Status' }` | `enum:AVAILABLE\|SOLD as Status` | `Status` |
|
|
601
607
|
| a union | `{ union: { Pet: Pet, Owner: Owner }, name: 'SearchResult' }` | `union={Pet: Pet, Owner: Owner} as SearchResult` | `SearchResult` |
|
|
602
608
|
|
|
603
|
-
A primitive, a scalar, an enum or a union also takes the place of the schema in a nullable or a list (`{ nullable: 'integer' }`, `'string[]'`, `scalar:DateTime?[]`). Each key of a union is the name of one of its types in the provider's GraphQL schema, and its value the schema the call reads from that type. Each form but a schema returned as it is becomes a schema of the contract, with the name above, and `name` in the decorator or `as <name>` in the tag gives it another one (`{ nullable: Pet, name: 'MaybePet' }`, `Pet? as MaybePet`). They name only a schema `generate` creates: a schema returned as it is is not copied, the call references it by its own name, so `@bidirekt returns Pet as MyPet` stops `generate`. To call it `MyPet`, name the schema itself, with `@BidirektSchema({ name })` or `@bidirekt schema name=<Name>`. A primitive returned as
|
|
609
|
+
A primitive, a scalar, an enum or a union also takes the place of the schema in a nullable or a list (`{ nullable: 'integer' }`, `'string[]'`, `scalar:DateTime?[]`). Each key of a union is the name of one of its types in the provider's GraphQL schema, and its value the schema the call reads from that type. Each form but a schema returned as it is becomes a schema of the contract, with the name above, and `name` in the decorator or `as <name>` in the tag gives it another one (`{ nullable: Pet, name: 'MaybePet' }`, `Pet? as MaybePet`). They name only a schema `generate` creates: a schema returned as it is is not copied, the call references it by its own name, so `@bidirekt returns Pet as MyPet` stops `generate`. To call it `MyPet`, name the schema itself, with `@BidirektSchema({ name })` or `@bidirekt schema name=<Name>`. A primitive returned as a bare string, `'boolean'`, is always written as `Boolean`; to name it, write `{ type: 'boolean', name: 'Flag' }` in the decorator or `boolean as Flag` in the tag, and both write `Flag: {type: boolean}`. An enum or a union in a nullable or a list is a schema of its own, under the name it declares, and the nullable or the list references it under the name above: `{ nullable: { enum: ['AVAILABLE', 'SOLD'], name: 'Status' } }` and `enum:AVAILABLE|SOLD? as Status` both write `Status` and `StatusOrNull: {ref: Status, nullable: true}`. In the tag, that `as` names the enum or the union, never the nullable or the list around it. A scalar in a nullable or a list takes no name of its own: the name goes on the nullable or the list, and in the tag `as` names them.
|
|
604
610
|
|
|
605
611
|
A call that reads a GraphQL interface both on the interface and on the types that implement it, as `node(id: $id) { id ... on User { name } ... on Pet { species } }`, returns a schema with a `union`: its fields are the ones read on the interface, and `union` maps each type read with `... on` to the schema the call reads from it. `@BidirektSchema({ union: { User: UserName, Pet: PetSpecies } })` on a class `Node` with `@BidirektField() id`, or `@bidirekt schema union={User: UserName, Pet: PetSpecies}` above `interface Node` with a `@bidirekt field` on `id`, writes `Node` as `type: union`, with `id` in its `properties` and `User` and `Pet` in its `members`. A member's schema holds only the fields read on its own type: a member class that extends `Node` would repeat `id`. A schema with a `union` and no field writes only the `members`, a named union like `{ union: {...}, name }`. `union=` goes above a class, an interface or an object type, never above an enum.
|
|
606
612
|
|
|
@@ -619,6 +625,8 @@ A `union` names each schema of the field's union once, by one key, and nothing e
|
|
|
619
625
|
|
|
620
626
|
In a GraphQL contract, `@BidirektGraphqlField` takes the place of `@BidirektField` on a field that needs GraphQL's own options, and `@bidirekt graphql field` that of `@bidirekt field`. They take the options above, plus `scalar: 'DateTime'`, a custom scalar that replaces the field's type and is written as `type: scalar, name: DateTime`, and `arguments: ReviewsArgs`, the schema of the arguments the call passes to the field, as `first` in `reviews(first: 10)`. In a tag, `scalar=DateTime` and `arguments=ReviewsArgs`. A field takes one of the two, never both.
|
|
621
627
|
|
|
628
|
+
A call that reads the type name declares a field `__typename` with the names it reads as a string literal type, `__typename: 'Pet'` or `__typename: 'Pet' | 'Toy'`, with `@BidirektField` or `@bidirekt field`. It is written as `type: enum` with `members: ["Pet", "Toy"]`, and breaks when the provider renames a type the call reads.
|
|
629
|
+
|
|
622
630
|
### Types
|
|
623
631
|
|
|
624
632
|
A field's type comes from TypeScript, or from the JSDoc `@type` in JavaScript:
|
|
@@ -27,6 +27,7 @@ const consumerOptions = [
|
|
|
27
27
|
const listKeys = ['list', 'nullable', 'nullableItems', 'name'];
|
|
28
28
|
const nullableKeys = ['nullable', 'name'];
|
|
29
29
|
const scalarKeys = ['scalar', 'name'];
|
|
30
|
+
const primitiveKeys = ['type', 'name'];
|
|
30
31
|
const enumKeys = ['enum', 'name'];
|
|
31
32
|
const unionKeys = ['union', 'name'];
|
|
32
33
|
const wrapperPlaces = {
|
|
@@ -199,7 +200,10 @@ function expressionToReturns(expression, context) {
|
|
|
199
200
|
return optionsToNullable(options, expression, context);
|
|
200
201
|
}
|
|
201
202
|
if (options.has('scalar') && hasOnlyKeys(options, scalarKeys)) {
|
|
202
|
-
return
|
|
203
|
+
return optionsToNamedTypeReturns(options, expression, optionsToScalar);
|
|
204
|
+
}
|
|
205
|
+
if (options.has('type') && hasOnlyKeys(options, primitiveKeys)) {
|
|
206
|
+
return optionsToNamedTypeReturns(options, expression, optionsToPrimitive);
|
|
203
207
|
}
|
|
204
208
|
}
|
|
205
209
|
const type = expressionToReturnedType(expression, context);
|
|
@@ -259,8 +263,8 @@ function optionsToNullable(options, expression, context) {
|
|
|
259
263
|
}
|
|
260
264
|
return returns;
|
|
261
265
|
}
|
|
262
|
-
function
|
|
263
|
-
const type =
|
|
266
|
+
function optionsToNamedTypeReturns(options, expression, readType) {
|
|
267
|
+
const type = readType(options, expression);
|
|
264
268
|
if (isDiagnostic(type)) {
|
|
265
269
|
return type;
|
|
266
270
|
}
|
|
@@ -274,6 +278,13 @@ function optionsToScalarReturns(options, expression) {
|
|
|
274
278
|
}
|
|
275
279
|
return returns;
|
|
276
280
|
}
|
|
281
|
+
function optionsToPrimitive(options, expression) {
|
|
282
|
+
const written = options.get('type');
|
|
283
|
+
if (written === undefined || !ts.isStringLiteralLike(written)) {
|
|
284
|
+
return unreadableReturns(expression);
|
|
285
|
+
}
|
|
286
|
+
return textToReturnedPrimitive(written.text) ?? unreadableReturns(expression);
|
|
287
|
+
}
|
|
277
288
|
function expressionToReturnedType(expression, context, wrapper) {
|
|
278
289
|
if (ts.isIdentifier(expression) ||
|
|
279
290
|
ts.isPropertyAccessExpression(expression)) {
|
|
@@ -395,7 +406,7 @@ function hasOnlyKeys(options, keys) {
|
|
|
395
406
|
}
|
|
396
407
|
function unreadableReturns(expression) {
|
|
397
408
|
return {
|
|
398
|
-
message: `cannot read "${expression.getText()}" as returns: write a @BidirektSchema class, a primitive such as 'boolean', [Schema], { list: Schema }, { nullable: Schema }, { scalar: '<name>' }, { enum: [...], name: '<name>' } or { union: {...}, name: '<name>' }`,
|
|
409
|
+
message: `cannot read "${expression.getText()}" as returns: write a @BidirektSchema class, a primitive such as 'boolean', { type: '<primitive>', name: '<name>' }, [Schema], { list: Schema }, { nullable: Schema }, { scalar: '<name>' }, { enum: [...], name: '<name>' } or { union: {...}, name: '<name>' }`,
|
|
399
410
|
location: nodeToLocation(expression),
|
|
400
411
|
};
|
|
401
412
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bidirekt/typescript-sdk-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"prepublishOnly": "npm run build"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@bidirekt/typescript-sdk": "0.
|
|
28
|
+
"@bidirekt/typescript-sdk": "0.4.0",
|
|
29
29
|
"typescript": "~6.0.3",
|
|
30
30
|
"yaml": "^2.9.1"
|
|
31
31
|
}
|