@distilled.cloud/core 1.0.0-rc.20 → 1.0.0-rc.21

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 (161) hide show
  1. package/README.md +33 -0
  2. package/lib/_bundle/aws.js +1 -1
  3. package/lib/_bundle/aws.js.map +1 -1
  4. package/lib/_bundle/base.js +2 -2
  5. package/lib/_bundle/base.js.map +1 -1
  6. package/lib/_bundle/graphql.js +2 -2
  7. package/lib/_bundle/graphql.js.map +1 -1
  8. package/lib/_bundle/grpc.js +2 -1
  9. package/lib/_bundle/grpc.js.map +1 -1
  10. package/lib/_bundle/rest.js +1 -1
  11. package/lib/_bundle/rest.js.map +1 -1
  12. package/lib/annotations.d.ts.map +1 -1
  13. package/lib/api.d.ts +71 -58
  14. package/lib/api.d.ts.map +1 -1
  15. package/lib/api.js +1 -1
  16. package/lib/category.d.ts +15 -12
  17. package/lib/category.d.ts.map +1 -1
  18. package/lib/chunks.d.ts.map +1 -1
  19. package/lib/config.d.ts +13 -0
  20. package/lib/config.d.ts.map +1 -1
  21. package/lib/crypto.d.ts.map +1 -1
  22. package/lib/error-category.d.ts.map +1 -1
  23. package/lib/errors.d.ts +35 -2
  24. package/lib/errors.d.ts.map +1 -1
  25. package/lib/errors.js +1 -1
  26. package/lib/graphql.d.ts +21 -4
  27. package/lib/graphql.d.ts.map +1 -1
  28. package/lib/graphql.types.d.ts +0 -1
  29. package/lib/graphql.types.d.ts.map +1 -1
  30. package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -1
  31. package/lib/grpc-transport-cloudflare-http2.js +2 -2
  32. package/lib/grpc-transport-cloudflare-http2.js.map +1 -1
  33. package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -1
  34. package/lib/grpc-transport-cloudflare-sockets.js +4 -4
  35. package/lib/grpc-transport-cloudflare-sockets.js.map +1 -1
  36. package/lib/grpc-transport-node-http2.d.ts.map +1 -1
  37. package/lib/grpc-transport-node-http2.js +1 -1
  38. package/lib/grpc-transport-node-http2.js.map +1 -1
  39. package/lib/grpc-transport.d.ts +53 -18
  40. package/lib/grpc-transport.d.ts.map +1 -1
  41. package/lib/grpc-transport.js +1 -1
  42. package/lib/hpack.d.ts.map +1 -1
  43. package/lib/index.d.ts.map +1 -1
  44. package/lib/max-message-bytes.d.ts +42 -0
  45. package/lib/max-message-bytes.d.ts.map +1 -0
  46. package/lib/max-message-bytes.js +2 -0
  47. package/lib/max-message-bytes.js.map +1 -0
  48. package/lib/ndjson.d.ts +3 -0
  49. package/lib/ndjson.d.ts.map +1 -0
  50. package/lib/ndjson.js +2 -0
  51. package/lib/ndjson.js.map +1 -0
  52. package/lib/pagination.d.ts +4 -1
  53. package/lib/pagination.d.ts.map +1 -1
  54. package/lib/plain-values.d.ts +11 -0
  55. package/lib/plain-values.d.ts.map +1 -1
  56. package/lib/plain-values.js +1 -1
  57. package/lib/plain-values.js.map +1 -1
  58. package/lib/protobuf.d.ts +12 -19
  59. package/lib/protobuf.d.ts.map +1 -1
  60. package/lib/protobuf.js +1 -1
  61. package/lib/protocol-grpc.d.ts +47 -10
  62. package/lib/protocol-grpc.d.ts.map +1 -1
  63. package/lib/protocol-grpc.js +1 -1
  64. package/lib/protocol-http.d.ts +110 -8
  65. package/lib/protocol-http.d.ts.map +1 -1
  66. package/lib/protocol-http.js +1 -1
  67. package/lib/protocol-rest.d.ts +161 -37
  68. package/lib/protocol-rest.d.ts.map +1 -1
  69. package/lib/protocol-rest.js +1 -1
  70. package/lib/protocol.d.ts +113 -0
  71. package/lib/protocol.d.ts.map +1 -0
  72. package/lib/protocol.js +0 -0
  73. package/lib/query.d.ts.map +1 -1
  74. package/lib/response-validation-strict.d.ts.map +1 -1
  75. package/lib/response-validation.d.ts.map +1 -1
  76. package/lib/retry-after.d.ts +1 -1
  77. package/lib/retry-after.d.ts.map +1 -1
  78. package/lib/retry-state.d.ts +26 -0
  79. package/lib/retry-state.d.ts.map +1 -0
  80. package/lib/retry-state.js +2 -0
  81. package/lib/retry-state.js.map +1 -0
  82. package/lib/retry.d.ts +100 -20
  83. package/lib/retry.d.ts.map +1 -1
  84. package/lib/retry.js +1 -1
  85. package/lib/schema-lite-impl.d.ts +25 -4
  86. package/lib/schema-lite-impl.d.ts.map +1 -1
  87. package/lib/schema-lite-impl.js +1 -1
  88. package/lib/schema-lite-typed.d.ts.map +1 -1
  89. package/lib/schema-lite.d.ts +1 -0
  90. package/lib/schema-lite.d.ts.map +1 -1
  91. package/lib/schema-lite.js +1 -1
  92. package/lib/schema.d.ts.map +1 -1
  93. package/lib/sdk-error.d.ts +4 -0
  94. package/lib/sdk-error.d.ts.map +1 -1
  95. package/lib/send.d.ts +37 -0
  96. package/lib/send.d.ts.map +1 -0
  97. package/lib/send.js +2 -0
  98. package/lib/send.js.map +1 -0
  99. package/lib/sensitive.d.ts.map +1 -1
  100. package/lib/sensitive.js +1 -1
  101. package/lib/sensitive.js.map +1 -1
  102. package/lib/telemetry-debug.d.ts.map +1 -1
  103. package/lib/telemetry-debug.js +1 -1
  104. package/lib/telemetry-debug.js.map +1 -1
  105. package/lib/telemetry-hooks.d.ts.map +1 -1
  106. package/lib/telemetry-host-rules.d.ts.map +1 -1
  107. package/lib/telemetry-lambda-context.d.ts.map +1 -1
  108. package/lib/telemetry-message-context.d.ts.map +1 -1
  109. package/lib/telemetry-metrics.d.ts.map +1 -1
  110. package/lib/telemetry-metrics.js +1 -1
  111. package/lib/telemetry-metrics.js.map +1 -1
  112. package/lib/telemetry-name-template.d.ts.map +1 -1
  113. package/lib/telemetry-stream.d.ts.map +1 -1
  114. package/lib/telemetry-stream.js +1 -1
  115. package/lib/telemetry-stream.js.map +1 -1
  116. package/lib/telemetry-xray.d.ts.map +1 -1
  117. package/lib/telemetry.d.ts +29 -0
  118. package/lib/telemetry.d.ts.map +1 -1
  119. package/lib/telemetry.js +2 -2
  120. package/lib/telemetry.js.map +1 -1
  121. package/lib/token-cache.d.ts +31 -0
  122. package/lib/token-cache.d.ts.map +1 -0
  123. package/lib/token-cache.js +2 -0
  124. package/lib/token-cache.js.map +1 -0
  125. package/lib/trait.d.ts +24 -1
  126. package/lib/trait.d.ts.map +1 -1
  127. package/lib/trait.js +1 -1
  128. package/lib/unknown-error.d.ts.map +1 -1
  129. package/lib/xml.d.ts.map +1 -1
  130. package/package.json +3 -3
  131. package/src/api.ts +402 -209
  132. package/src/category.ts +21 -28
  133. package/src/config.ts +17 -1
  134. package/src/errors.ts +24 -2
  135. package/src/graphql.ts +65 -26
  136. package/src/graphql.types.ts +15 -0
  137. package/src/grpc-transport-cloudflare-http2.ts +374 -299
  138. package/src/grpc-transport-cloudflare-sockets.ts +150 -81
  139. package/src/grpc-transport-node-http2.ts +119 -46
  140. package/src/grpc-transport.ts +211 -42
  141. package/src/max-message-bytes.ts +58 -0
  142. package/src/ndjson.ts +55 -0
  143. package/src/pagination.ts +26 -15
  144. package/src/plain-values.ts +30 -1
  145. package/src/protobuf.ts +40 -5
  146. package/src/protocol-grpc.ts +518 -348
  147. package/src/protocol-http.ts +474 -100
  148. package/src/protocol-rest.ts +580 -140
  149. package/src/protocol.ts +150 -0
  150. package/src/response-validation-strict.ts +9 -3
  151. package/src/retry-after.ts +1 -1
  152. package/src/retry-state.ts +105 -0
  153. package/src/retry.ts +176 -47
  154. package/src/schema-lite-impl.ts +81 -10
  155. package/src/schema-lite.ts +1 -0
  156. package/src/sdk-error.ts +4 -0
  157. package/src/send.ts +156 -0
  158. package/src/sensitive.ts +49 -15
  159. package/src/telemetry.ts +2 -34
  160. package/src/token-cache.ts +117 -0
  161. package/src/trait.ts +34 -1
package/src/category.ts CHANGED
@@ -254,35 +254,28 @@ const isHttpTransportError = (error: unknown): boolean =>
254
254
  HttpClientError.isHttpClientError(error) && error.reason._tag === "TransportError";
255
255
 
256
256
  /**
257
- * Check if an error is a transient error that should be automatically retried.
258
- * Transient errors include:
259
- * - Errors marked with withRetryable()
260
- * - RetryableError category
261
- * - ThrottlingError (rate limiting)
262
- * - ServerError (5xx responses)
263
- * - NetworkError (connection issues)
264
- * - TimeoutError (request timed out)
265
- * - LockedError (423 - resource temporarily locked)
266
- * - Effect `HttpClientError` with `reason._tag === "TransportError"`
267
- * (undici connect/read timeout, socket reset, DNS failure, premature
268
- * stream close)
257
+ * Check if an error should be retried automatically. Only errors that say
258
+ * so are retried; an HTTP status alone does not make an error retryable:
259
+ *
260
+ * - errors marked with `withRetryable()` (Smithy `@retryable`, a patch, or
261
+ * the SDK's own error classes);
262
+ * - the `RetryableError` and `ThrottlingError` categories (`ThrottlingError`
263
+ * is HTTP 429, the server's own "try again later");
264
+ * - the `NetworkError` category: the request failed on the way, before any
265
+ * server answered;
266
+ * - an Effect `HttpClientError` with `reason._tag === "TransportError"`
267
+ * (connect timeout, socket reset, DNS failure, premature stream close),
268
+ * which no spec can mark.
269
+ *
270
+ * `ServerError`, `TimeoutError` and `LockedError` describe an
271
+ * error; they do not retry it.
269
272
  */
270
- export const isTransientError = (error: unknown): boolean => {
271
- // Check for retryable trait first
272
- if (isRetryable(error)) {
273
- return true;
274
- }
275
- // Fall back to category-based checking
276
- return (
277
- hasCategory(error, RetryableError) ||
278
- hasCategory(error, ThrottlingError) ||
279
- hasCategory(error, ServerError) ||
280
- hasCategory(error, NetworkError) ||
281
- hasCategory(error, TimeoutError) ||
282
- hasCategory(error, LockedError) ||
283
- isHttpTransportError(error)
284
- );
285
- };
273
+ export const isTransientError = (error: unknown): boolean =>
274
+ isRetryable(error) ||
275
+ hasCategory(error, RetryableError) ||
276
+ hasCategory(error, ThrottlingError) ||
277
+ hasCategory(error, NetworkError) ||
278
+ isHttpTransportError(error);
286
279
 
287
280
  // ============================================================================
288
281
  // Category Type Utilities
package/src/config.ts CHANGED
@@ -42,6 +42,19 @@ export class ConfigError
42
42
  key: /*@__PURE__*/ Schema.optional(Schema.String),
43
43
  }).pipe(Category.withConfigurationError) {}
44
44
 
45
+ declare const checked: unique symbol;
46
+
47
+ /**
48
+ * A configuration its SDK has checked. `ConfigError` is a setup error: a
49
+ * layer reads and checks what the user configured when the layer is built,
50
+ * and fails there. The SDK's `check(config)` is the one place that returns
51
+ * a `Checked<Config>`, and its `Credentials` service holds one, so the code
52
+ * that builds a request can use the values without checking them again and
53
+ * no call fails for a malformed one. A `Credentials` layer written by hand
54
+ * has to go through `check` too.
55
+ */
56
+ export type Checked<Config> = Config & { readonly [checked]: true };
57
+
45
58
  const CONFIG_PROVIDER_KEY = "effect/ConfigProvider";
46
59
 
47
60
  interface ProvidedConfigProvider {
@@ -55,7 +68,10 @@ const fromEnv = (name: string): string | undefined =>
55
68
 
56
69
  /** A required value that is missing, inside a synchronous read. */
57
70
  class Missing {
58
- constructor(readonly name: string) {}
71
+ readonly name: string;
72
+ constructor(name: string) {
73
+ this.name = name;
74
+ }
59
75
  }
60
76
 
61
77
  /**
package/src/errors.ts CHANGED
@@ -24,6 +24,16 @@ import * as Schema from "./schema-lite-typed.ts";
24
24
  */
25
25
  export const DurationSchema = /*@__PURE__*/ Schema.declare<Duration.Duration>(Duration.isDuration);
26
26
 
27
+ /**
28
+ * Fields every error built from an HTTP response declares: the parsed body
29
+ * (the raw text when it wasn't JSON) and the response headers, for whatever
30
+ * the SDK does not type -- a request ID, nested details.
31
+ */
32
+ export const ResponseErrorFields = {
33
+ body: /*@__PURE__*/ Schema.optional(Schema.Unknown),
34
+ headers: /*@__PURE__*/ Schema.optional(/*@__PURE__*/ Schema.Record(Schema.String, Schema.String)),
35
+ };
36
+
27
37
  // ============================================================================
28
38
  // Common HTTP Status Error Classes
29
39
  // ============================================================================
@@ -34,6 +44,7 @@ export const DurationSchema = /*@__PURE__*/ Schema.declare<Duration.Duration>(Du
34
44
  export class Unauthorized
35
45
  extends /*@__PURE__*/ Schema.TaggedError<Unauthorized>()("Unauthorized", {
36
46
  message: Schema.String,
47
+ ...ResponseErrorFields,
37
48
  }).pipe(Category.withAuthError) {}
38
49
 
39
50
  /**
@@ -42,6 +53,7 @@ export class Unauthorized
42
53
  export class Forbidden
43
54
  extends /*@__PURE__*/ Schema.TaggedError<Forbidden>()("Forbidden", {
44
55
  message: Schema.String,
56
+ ...ResponseErrorFields,
45
57
  }).pipe(Category.withAuthError) {}
46
58
 
47
59
  /**
@@ -50,6 +62,7 @@ export class Forbidden
50
62
  export class NotFound
51
63
  extends /*@__PURE__*/ Schema.TaggedError<NotFound>()("NotFound", {
52
64
  message: Schema.String,
65
+ ...ResponseErrorFields,
53
66
  }).pipe(Category.withNotFoundError) {}
54
67
 
55
68
  /**
@@ -58,6 +71,7 @@ export class NotFound
58
71
  export class BadRequest
59
72
  extends /*@__PURE__*/ Schema.TaggedError<BadRequest>()("BadRequest", {
60
73
  message: Schema.String,
74
+ ...ResponseErrorFields,
61
75
  }).pipe(Category.withBadRequestError) {}
62
76
 
63
77
  /**
@@ -66,6 +80,7 @@ export class BadRequest
66
80
  export class Conflict
67
81
  extends /*@__PURE__*/ Schema.TaggedError<Conflict>()("Conflict", {
68
82
  message: Schema.String,
83
+ ...ResponseErrorFields,
69
84
  }).pipe(Category.withConflictError) {}
70
85
 
71
86
  /**
@@ -74,6 +89,7 @@ export class Conflict
74
89
  export class UnprocessableEntity
75
90
  extends /*@__PURE__*/ Schema.TaggedError<UnprocessableEntity>()("UnprocessableEntity", {
76
91
  message: Schema.String,
92
+ ...ResponseErrorFields,
77
93
  }).pipe(Category.withBadRequestError) {}
78
94
 
79
95
  /**
@@ -82,6 +98,7 @@ export class UnprocessableEntity
82
98
  export class TooManyRequests
83
99
  extends /*@__PURE__*/ Schema.TaggedError<TooManyRequests>()("TooManyRequests", {
84
100
  message: Schema.String,
101
+ ...ResponseErrorFields,
85
102
  retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
86
103
  }).pipe(
87
104
  Category.withThrottlingError,
@@ -94,6 +111,7 @@ export class TooManyRequests
94
111
  export class Locked
95
112
  extends /*@__PURE__*/ Schema.TaggedError<Locked>()("Locked", {
96
113
  message: Schema.String,
114
+ ...ResponseErrorFields,
97
115
  retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
98
116
  }).pipe(Category.withLockedError, /*@__PURE__*/ Category.withRetryable()) {}
99
117
 
@@ -108,6 +126,7 @@ export class Locked
108
126
  export class InternalServerError
109
127
  extends /*@__PURE__*/ Schema.TaggedError<InternalServerError>()("InternalServerError", {
110
128
  message: Schema.String,
129
+ ...ResponseErrorFields,
111
130
  code: /*@__PURE__*/ Schema.optional(Schema.Number),
112
131
  retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
113
132
  }).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
@@ -118,6 +137,7 @@ export class InternalServerError
118
137
  export class BadGateway
119
138
  extends /*@__PURE__*/ Schema.TaggedError<BadGateway>()("BadGateway", {
120
139
  message: Schema.String,
140
+ ...ResponseErrorFields,
121
141
  retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
122
142
  }).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
123
143
 
@@ -127,6 +147,7 @@ export class BadGateway
127
147
  export class ServiceUnavailable
128
148
  extends /*@__PURE__*/ Schema.TaggedError<ServiceUnavailable>()("ServiceUnavailable", {
129
149
  message: Schema.String,
150
+ ...ResponseErrorFields,
130
151
  retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
131
152
  }).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
132
153
 
@@ -136,6 +157,7 @@ export class ServiceUnavailable
136
157
  export class GatewayTimeout
137
158
  extends /*@__PURE__*/ Schema.TaggedError<GatewayTimeout>()("GatewayTimeout", {
138
159
  message: Schema.String,
160
+ ...ResponseErrorFields,
139
161
  retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
140
162
  }).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
141
163
 
@@ -186,8 +208,8 @@ export const DEFAULT_ERROR_STATUSES = /*@__PURE__*/ new Set([401, 429, 500, 502,
186
208
 
187
209
  /**
188
210
  * HTTP status codes whose corresponding error class in {@link HTTP_STATUS_MAP}
189
- * declares a `retryAfter` field. The retry policy honors `error.retryAfter`
190
- * with precedence over the default backoff for these.
211
+ * declares a `retryAfter` field. The retry policy waits at least
212
+ * `error.retryAfter` for these.
191
213
  *
192
214
  * `matchError` implementations should only pass `retryAfter` into the
193
215
  * constructor when the status is in this set; passing it on non-retryable
package/src/graphql.ts CHANGED
@@ -229,7 +229,7 @@ export type FieldMeta = {
229
229
  readonly kind: "scalar" | "object" | "list" | "connection";
230
230
  readonly of?: TypeMeta;
231
231
  readonly argTypes?: ArgMeta;
232
- /** The value is a secret: extracted strings are wrapped in `Redacted`. */
232
+ /** The value is a secret: extracted values are wrapped in `Redacted`. */
233
233
  readonly sensitive?: boolean;
234
234
  };
235
235
 
@@ -240,16 +240,18 @@ export type TypeMeta = {
240
240
 
241
241
  export const scalarField = (
242
242
  name: string,
243
- options: { readonly sensitive?: boolean } = {},
243
+ options: { readonly sensitive?: boolean; readonly argTypes?: ArgMeta } = {},
244
244
  ): FieldMeta => ({
245
245
  name,
246
246
  kind: "scalar",
247
+ ...(options.argTypes ? { argTypes: options.argTypes } : {}),
247
248
  ...(options.sensitive ? { sensitive: true } : {}),
248
249
  });
249
- export const objectField = (name: string, objectType: TypeMeta): FieldMeta => ({
250
+ export const objectField = (name: string, objectType: TypeMeta, argTypes?: ArgMeta): FieldMeta => ({
250
251
  name,
251
252
  kind: "object",
252
253
  of: objectType,
254
+ ...(argTypes ? { argTypes } : {}),
253
255
  });
254
256
  export const listField = (name: string, itemType: TypeMeta, argTypes?: ArgMeta): FieldMeta => ({
255
257
  name,
@@ -355,8 +357,9 @@ const log = (..._args: Array<unknown>): void => {};
355
357
  */
356
358
  type QueryFields<Value, Error> = [NonNullable<Value>] extends [ReadonlyArray<infer Item>]
357
359
  ? {
358
- readonly [Field in keyof NonNullable<Item>]: Query<
359
- NonNullable<Item>[Field] | Nullish<Item>,
360
+ readonly [Field in keyof NonNullable<Item>]: FieldQuery<
361
+ NonNullable<Item>[Field],
362
+ Nullish<Item>,
360
363
  Error
361
364
  >;
362
365
  } & {
@@ -364,13 +367,36 @@ type QueryFields<Value, Error> = [NonNullable<Value>] extends [ReadonlyArray<inf
364
367
  }
365
368
  : [NonNullable<Value>] extends [object]
366
369
  ? {
367
- readonly [Field in keyof NonNullable<Value>]: Query<
368
- NonNullable<Value>[Field] | Nullish<Value>,
370
+ readonly [Field in keyof NonNullable<Value>]: FieldQuery<
371
+ NonNullable<Value>[Field],
372
+ Nullish<Value>,
369
373
  Error
370
374
  >;
371
375
  }
372
376
  : {};
373
377
 
378
+ /**
379
+ * A field that takes arguments, as it appears in a generated data type.
380
+ * Through a Query it is a call: `account.byId({ accountId })`. When every
381
+ * argument is optional (`Required` false) the field also reads bare.
382
+ */
383
+ export interface FieldWithArgs<Value, Args, Required extends boolean = true> {
384
+ readonly "~fieldArgs": {
385
+ readonly value: Value;
386
+ readonly args: Args;
387
+ readonly required: Required;
388
+ };
389
+ }
390
+
391
+ /** The Query for one field: a call for a field with arguments, else a property. */
392
+ type FieldQuery<Field, ParentNull, Error> = [Field] extends [
393
+ FieldWithArgs<infer Value, infer Args, infer Required>,
394
+ ]
395
+ ? [Required] extends [true]
396
+ ? (args: Args) => Query<Value | ParentNull, Error>
397
+ : Query<Value | ParentNull, Error> & ((args?: Args) => Query<Value | ParentNull, Error>)
398
+ : Query<Field | ParentNull, Error>;
399
+
374
400
  /** A field read through a `null` parent is `null`. */
375
401
  type Nullish<Value> = Extract<Value, null | undefined>;
376
402
 
@@ -862,7 +888,9 @@ const paintAbs = (forest: Forest, expr: Expr): Path => {
862
888
  isList: expr.field.kind === "list",
863
889
  sensitive: expr.field.sensitive,
864
890
  });
865
- const path = [...parentPath, node.alias ?? node.field];
891
+ const key = node.alias ?? node.field;
892
+ forest.rootAlias.set(expr, key);
893
+ const path = [...parentPath, key];
866
894
  return expr.field.kind === "connection" ? [...path, ...paintConnection(forest, node)] : path;
867
895
  }
868
896
  default:
@@ -892,13 +920,14 @@ const connectionNodeSel = (connection: SelNode): SelNode => {
892
920
  return node;
893
921
  };
894
922
 
895
- const relSel = (expr: Expr, listSel: SelNode): SelNode => {
923
+ const relSel = (forest: Forest, expr: Expr, listSel: SelNode): SelNode => {
896
924
  switch (expr._tag) {
897
925
  case "Item":
898
926
  return listSel;
899
927
  case "Prop": {
900
- const parent = relSel(expr.parent, listSel);
901
- const child = [...parent.children.values()].find((c) => c.field === expr.field.name);
928
+ const parent = relSel(forest, expr.parent, listSel);
929
+ const key = forest.rootAlias.get(expr) ?? expr.field.name;
930
+ const child = [...parent.children.values()].find((c) => (c.alias ?? c.field) === key);
902
931
  if (!child) {
903
932
  throw new GqlError(`missing ${expr.field.name} in list selection`);
904
933
  }
@@ -916,13 +945,14 @@ const paintRel = (forest: Forest, expr: Expr, listSel: SelNode): void => {
916
945
  return;
917
946
  case "Prop": {
918
947
  paintRel(forest, expr.parent, listSel);
919
- const child = ensureChild(forest, relSel(expr.parent, listSel), expr.field.name, {
948
+ const child = ensureChild(forest, relSel(forest, expr.parent, listSel), expr.field.name, {
920
949
  args: expr.args,
921
950
  argTypes: expr.field.argTypes,
922
951
  isScalar: expr.field.kind === "scalar",
923
952
  isList: expr.field.kind === "list",
924
953
  sensitive: expr.field.sensitive,
925
954
  });
955
+ forest.rootAlias.set(expr, child.alias ?? child.field);
926
956
  if (expr.field.kind === "connection") paintConnection(forest, child);
927
957
  return;
928
958
  }
@@ -932,11 +962,11 @@ const paintRel = (forest: Forest, expr: Expr, listSel: SelNode): void => {
932
962
  return;
933
963
  case "MapItems":
934
964
  paintRel(forest, expr.parent, listSel);
935
- visitRel(expr.mapped, forest, relSel(expr.parent, listSel));
965
+ visitRel(expr.mapped, forest, relSel(forest, expr.parent, listSel));
936
966
  return;
937
967
  case "Filter":
938
968
  paintRel(forest, expr.parent, listSel);
939
- paintRel(forest, expr.predicate, relSel(expr.parent, listSel));
969
+ paintRel(forest, expr.predicate, relSel(forest, expr.parent, listSel));
940
970
  return;
941
971
  case "Root":
942
972
  throw new GqlError("root inside list item");
@@ -1102,22 +1132,25 @@ const readField = (parent: unknown, name: string): unknown => {
1102
1132
  return (parent as Record<string, unknown>)[name];
1103
1133
  };
1104
1134
 
1105
- /** Wrap every string of a sensitive scalar (or list of them) in `Redacted`. */
1106
- const redactStrings = (value: unknown): unknown =>
1107
- typeof value === "string"
1108
- ? Redacted.make(value)
1135
+ /**
1136
+ * Wrap a sensitive scalar in `Redacted`: a string, a JSON object, or each
1137
+ * item of a list of them.
1138
+ */
1139
+ const redactValues = (value: unknown): unknown =>
1140
+ value === null || value === undefined
1141
+ ? value
1109
1142
  : Array.isArray(value)
1110
- ? value.map(redactStrings)
1111
- : value;
1143
+ ? value.map(redactValues)
1144
+ : Redacted.make(value);
1112
1145
 
1113
- /** Copy `data` with the strings of every sensitive field `sel` selects wrapped in `Redacted`. */
1146
+ /** Copy `data` with the value of every sensitive field `sel` selects wrapped in `Redacted`. */
1114
1147
  const redactSelected = (sel: SelNode, data: unknown): unknown => {
1115
1148
  if (sel.children.size === 0 || data === null || typeof data !== "object") return data;
1116
1149
  if (Array.isArray(data)) return data.map((item) => redactSelected(sel, item));
1117
1150
  const out: Record<string, unknown> = { ...data };
1118
1151
  for (const child of sel.children.values()) {
1119
1152
  const key = child.alias ?? child.field;
1120
- out[key] = child.sensitive ? redactStrings(out[key]) : redactSelected(child, out[key]);
1153
+ out[key] = child.sensitive ? redactValues(out[key]) : redactSelected(child, out[key]);
1121
1154
  }
1122
1155
  return out;
1123
1156
  };
@@ -1129,7 +1162,10 @@ const extractAbs = (expr: Expr, data: unknown, rootAlias: WeakMap<object, string
1129
1162
  return expr.connection ? nodesFromConnection(raw) : raw;
1130
1163
  }
1131
1164
  case "Prop": {
1132
- const value = readField(extractAbs(expr.parent, data, rootAlias), expr.field.name);
1165
+ const value = readField(
1166
+ extractAbs(expr.parent, data, rootAlias),
1167
+ rootAlias.get(expr) ?? expr.field.name,
1168
+ );
1133
1169
  return expr.field.kind === "connection" ? nodesFromConnection(value) : value;
1134
1170
  }
1135
1171
  default:
@@ -1137,12 +1173,15 @@ const extractAbs = (expr: Expr, data: unknown, rootAlias: WeakMap<object, string
1137
1173
  }
1138
1174
  };
1139
1175
 
1140
- const extractRel = (expr: Expr, item: unknown): unknown => {
1176
+ const extractRel = (expr: Expr, item: unknown, rootAlias: WeakMap<object, string>): unknown => {
1141
1177
  switch (expr._tag) {
1142
1178
  case "Item":
1143
1179
  return item;
1144
1180
  case "Prop": {
1145
- const value = readField(extractRel(expr.parent, item), expr.field.name);
1181
+ const value = readField(
1182
+ extractRel(expr.parent, item, rootAlias),
1183
+ rootAlias.get(expr) ?? expr.field.name,
1184
+ );
1146
1185
  return expr.field.kind === "connection" ? nodesFromConnection(value) : value;
1147
1186
  }
1148
1187
  default:
@@ -1178,7 +1217,7 @@ const interpretExpr = (
1178
1217
  return item;
1179
1218
  case "Root":
1180
1219
  case "Prop":
1181
- return inItem(expr) ? extractRel(expr, item) : extractAbs(expr, data, rootAlias);
1220
+ return inItem(expr) ? extractRel(expr, item, rootAlias) : extractAbs(expr, data, rootAlias);
1182
1221
  case "MapValue":
1183
1222
  return expr.mapFn(interpretExpr(expr.parent, data, rootAlias, item));
1184
1223
  case "Filter": {
@@ -9,6 +9,7 @@ import type {
9
9
  UnknownGraphQLError,
10
10
  } from "./graphql.ts";
11
11
  /** Compile-only: Query.fn unwraps Query/Effect fields on the returned plan. */
12
+ import type { FieldWithArgs } from "./graphql.ts";
12
13
  import type { PlanError, Query, QueryError, UnwrapPlan } from "./query.ts";
13
14
 
14
15
  type Plan = {
@@ -103,3 +104,17 @@ const popular = /*@__PURE__*/ service.stars.pipe(/*@__PURE__*/ mapQuery((stars)
103
104
  type _ScalarMap = Assert<Equal<UnwrapPlan<typeof popular>, boolean>>;
104
105
  const labels = /*@__PURE__*/ service.tags.pipe(/*@__PURE__*/ mapQuery((tag) => tag.label));
105
106
  type _ListMap = Assert<Equal<UnwrapPlan<typeof labels>, ReadonlyArray<string>>>;
107
+
108
+ // Fields with arguments: a required argument makes the field a call only;
109
+ // all-optional arguments also allow the bare read.
110
+ declare const accountQuery: Query<{
111
+ byId: FieldWithArgs<{ name: string }, { readonly accountId: string }>;
112
+ avatarUrl: FieldWithArgs<string, { readonly size?: number | undefined }, false>;
113
+ }>;
114
+ type _ById = Assert<Equal<UnwrapPlan<ReturnType<typeof accountQuery.byId>["name"]>, string>>;
115
+ type _AvatarCall = Assert<Equal<UnwrapPlan<ReturnType<typeof accountQuery.avatarUrl>>, string>>;
116
+ type _AvatarBare = Assert<Equal<UnwrapPlan<typeof accountQuery.avatarUrl>, string>>;
117
+ // @ts-expect-error a required argument cannot be left out
118
+ accountQuery.byId({});
119
+ // @ts-expect-error a field with a required argument has no bare read
120
+ type _NoBareRead = (typeof accountQuery.byId)["pipe"];