@m1212e/rumble 0.12.13 → 0.12.14

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
@@ -129,10 +129,10 @@ rumble({
129
129
  ## Helpers
130
130
  Rumble offers various helpers to make it easy and fast to implement your api. Ofcourse you can write your api by hand using the provided `schemaBuilder` from the rumble initiator, but since this might get repetitive, the provided helpers automate a lot of this work for you while also automatically applying the concepts of rumble directly into your api.
131
131
 
132
- ### arg
133
- `arg` is a helper to implement query arguments for filtering the results of a query for certain results. In many cases you would implement arguments for a query with something as `matchUsername: t.arg.string()` which is supposed to restrict the query to users which have that username. The arg helper implements such a filter tailored to the specific entity which you then can directly pass on to the database query.
132
+ ### whereArg
133
+ `whereArg` is a helper to implement query arguments for filtering the results of a query for certain results. In many cases you would implement arguments for a query with something as `matchUsername: t.arg.string()` which is supposed to restrict the query to users which have that username. The whereArg helper implements such a filter tailored to the specific entity which you then can directly pass on to the database query.
134
134
  ```ts
135
- const WhereArgs = arg({
135
+ const WhereArgs = whereArg({
136
136
  table: "posts",
137
137
  });
138
138
 
@@ -210,6 +210,9 @@ const enumRef = enum_({
210
210
  ```
211
211
  > The enum parameter allows other fields to be used to reference an enum. This is largely due to how this is used internally. Because of the way how drizzle handles enums, we are not able to provide type safety with enums. In case you actually need to use it, the above way is the recommended approach.
212
212
 
213
+ ### and more...
214
+ See [the example file](./example/src/main.ts) or clone it and let intellisense guide you. Rumble offers various other helpers which might become handy!
215
+
213
216
  ## Running the server
214
217
  In case you directly want to run a server from your rumble instance, you can do so by using the `createYoga` function. It returns a graphql `yoga` instance which can be used to provide a graphql api in [a multitude of ways](https://the-guild.dev/graphql/yoga-server/docs/integrations/z-other-environments).
215
218
  ```ts
@@ -242,7 +245,9 @@ await clientCreator({
242
245
  // to point to your custom client
243
246
  });
244
247
  ```
245
- > The client uses [urql](https://nearform.com/open-source/urql/docs/basics/core/) under the hood. If you would like more info on how the internals work, please see the docs.
248
+ > The client uses [urql](https://nearform.com/open-source/urql/docs/basics/core/) under the hood. If you would like more info on how the internals work, please see their docs.
249
+
250
+ This way of generating code is especially helpful in monorepos, where it is convenient to output client code when running the server during development. If you do not use a monorepo and want to decouple the generation process, see below.
246
251
 
247
252
  An example usage might look like this:
248
253
  ```ts
@@ -272,7 +277,7 @@ users.subscribe((s) => s?.at(0));
272
277
  // you can directly access the values of an awaited result
273
278
  console.log(users.firstName)
274
279
  ```
275
- ### Alternative
280
+ ### Alternative decoupled client generation
276
281
  As an alternative to use the client generator with a fully instanciated rumble instance, you can also import the `generateFromSchema` function from rumble and pass it a standard `GraphQLSchema` object to generate the client:
277
282
  ```ts
278
283
  import { generateFromSchema } from "@m1212e/rumble";
package/out/index.cjs CHANGED
@@ -244,25 +244,21 @@ function makeGraphQLQuery({ queryName, input, client, enableSubscription = false
244
244
  const argsString = stringifyArgumentObjectToGraphqlList(input[argsKey] ?? {});
245
245
  const operationString = (operationVerb) => `${operationVerb} ${otwQueryName} { ${queryName}${argsString} { ${stringifySelection(input)} }}`;
246
246
  let awaitedReturnValueReference;
247
- const response = (0, wonka.pipe)((0, wonka.merge)([client.query(operationString("query"), {}), enableSubscription ? client.subscription(operationString("subscription"), {}) : wonka.empty]), (0, wonka.map)((v) => {
247
+ const observable = (0, wonka.pipe)((0, wonka.merge)([client.query(operationString("query"), {}), enableSubscription ? client.subscription(operationString("subscription"), {}) : wonka.empty]), (0, wonka.map)((v) => {
248
248
  const data = v.data?.[queryName];
249
249
  if (!data && v.error) throw v.error;
250
250
  return data;
251
251
  }), (0, wonka.onPush)((data) => {
252
252
  if (awaitedReturnValueReference) Object.assign(awaitedReturnValueReference, data);
253
- }));
253
+ }), wonka.toObservable);
254
254
  const promise = new Promise((resolve) => {
255
- const unsub = (0, wonka.toObservable)(response).subscribe((v) => {
255
+ const unsub = observable.subscribe((v) => {
256
256
  unsub();
257
- const newObservableWithAwaitedValue = (0, wonka.pipe)((0, wonka.merge)([response, (0, wonka.fromValue)(v)]), wonka.toObservable);
258
- awaitedReturnValueReference = {
259
- ...v,
260
- ...newObservableWithAwaitedValue
261
- };
257
+ awaitedReturnValueReference = Object.assign(v, observable);
262
258
  resolve(awaitedReturnValueReference);
263
259
  }).unsubscribe;
264
260
  });
265
- Object.assign(promise, (0, wonka.toObservable)(response));
261
+ Object.assign(promise, observable);
266
262
  return promise;
267
263
  }
268
264
  function makeGraphQLMutation({ mutationName, input, client }) {
@@ -1091,6 +1087,16 @@ const createContextFunction = ({ context: makeUserContext, abilityBuilder }) =>
1091
1087
  };
1092
1088
  };
1093
1089
 
1090
+ //#endregion
1091
+ //#region lib/helpers/protoMapper.ts
1092
+ function deepSetProto(obj, proto = Object.prototype, seen = /* @__PURE__ */ new WeakSet()) {
1093
+ if (obj === null || typeof obj !== "object") return;
1094
+ if (seen.has(obj)) return;
1095
+ seen.add(obj);
1096
+ Object.setPrototypeOf(obj, proto);
1097
+ for (const key of Object.keys(obj)) deepSetProto(obj[key], proto, seen);
1098
+ }
1099
+
1094
1100
  //#endregion
1095
1101
  //#region lib/countQuery.ts
1096
1102
  const createCountQueryImplementer = ({ db, schemaBuilder, whereArgImplementer, makePubSubInstance }) => {
@@ -1123,7 +1129,7 @@ const createCountQueryImplementer = ({ db, schemaBuilder, whereArgImplementer, m
1123
1129
  }) },
1124
1130
  resolve: async (root, args, ctx, info) => {
1125
1131
  if (isAllowed && !await isAllowed(ctx)) throw new RumbleErrorSafe("Not allowed to perform this action");
1126
- Object.setPrototypeOf(args, Object.prototype);
1132
+ deepSetProto(args);
1127
1133
  return db.select({ count: (0, drizzle_orm.count)() }).from(tableSchema.fullSchema).where(ctx.abilities[table].filter(listAction).merge(mapNullFieldsToUndefined(args)).sql.where).then(assertFirstEntryExists).then((r) => r.count);
1128
1134
  }
1129
1135
  }) };
@@ -1579,7 +1585,7 @@ const createQueryImplementer = ({ db, schemaBuilder, search, whereArgImplementer
1579
1585
  },
1580
1586
  args: manyArgs,
1581
1587
  resolve: (query, _root, args, ctx, _info) => {
1582
- Object.setPrototypeOf(args, Object.prototype);
1588
+ deepSetProto(args);
1583
1589
  adjustQueryArgsForSearch({
1584
1590
  search,
1585
1591
  args,
@@ -1602,7 +1608,7 @@ const createQueryImplementer = ({ db, schemaBuilder, search, whereArgImplementer
1602
1608
  description: `Get a single ${pluralize.default.singular(table.toString())} by ID`,
1603
1609
  args: { id: t.arg.id({ required: true }) },
1604
1610
  resolve: (query, _root, args, ctx, _info) => {
1605
- Object.setPrototypeOf(args, Object.prototype);
1611
+ deepSetProto(args);
1606
1612
  const filter = ctx.abilities[table].filter(readAction).merge({ where: { [primaryKeyField.name]: args.id } }).query.single;
1607
1613
  const q = query(filter);
1608
1614
  if (filter.columns) q.columns = filter.columns;