convex 1.35.0 → 1.35.1
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/dist/browser.bundle.js +2 -8
- package/dist/browser.bundle.js.map +3 -3
- package/dist/cjs/browser/index-node.js +1 -3
- package/dist/cjs/browser/index.js +1 -3
- package/dist/cjs/browser/index.js.map +2 -2
- package/dist/cjs/browser/query_options.js.map +2 -2
- package/dist/cjs/cli/codegen_templates/readme.js +1 -14
- package/dist/cjs/cli/codegen_templates/readme.js.map +2 -2
- package/dist/cjs/cli/deploy.js +1 -1
- package/dist/cjs/cli/deploy.js.map +1 -1
- package/dist/cjs/cli/lib/envvars.js +8 -1
- package/dist/cjs/cli/lib/envvars.js.map +2 -2
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/react/client.js +6 -43
- package/dist/cjs/react/client.js.map +2 -2
- package/dist/cjs/react/index.js +0 -2
- package/dist/cjs/react/index.js.map +2 -2
- package/dist/cjs/server/api.js.map +2 -2
- package/dist/cjs-types/browser/index.d.ts +0 -1
- package/dist/cjs-types/browser/index.d.ts.map +1 -1
- package/dist/cjs-types/browser/query_options.d.ts +9 -12
- package/dist/cjs-types/browser/query_options.d.ts.map +1 -1
- package/dist/cjs-types/cli/codegen_templates/readme.d.ts.map +1 -1
- package/dist/cjs-types/cli/lib/deployApi/definitionConfig.d.ts +24 -24
- package/dist/cjs-types/cli/lib/deployApi/modules.d.ts +2 -2
- package/dist/cjs-types/cli/lib/deployApi/startPush.d.ts +36 -36
- package/dist/cjs-types/cli/lib/envvars.d.ts.map +1 -1
- package/dist/cjs-types/cli/lib/mcp/tools/insights.d.ts +2 -2
- package/dist/cjs-types/index.d.ts +1 -1
- package/dist/cjs-types/react/client.d.ts +2 -54
- package/dist/cjs-types/react/client.d.ts.map +1 -1
- package/dist/cjs-types/react/index.d.ts +2 -7
- package/dist/cjs-types/react/index.d.ts.map +1 -1
- package/dist/cjs-types/server/api.d.ts +1 -5
- package/dist/cjs-types/server/api.d.ts.map +1 -1
- package/dist/cli.bundle.cjs +11 -17
- package/dist/cli.bundle.cjs.map +2 -2
- package/dist/esm/browser/index-node.js +0 -1
- package/dist/esm/browser/index.js +0 -1
- package/dist/esm/browser/index.js.map +2 -2
- package/dist/esm/browser/query_options.js.map +2 -2
- package/dist/esm/cli/codegen_templates/readme.js +1 -14
- package/dist/esm/cli/codegen_templates/readme.js.map +2 -2
- package/dist/esm/cli/deploy.js +1 -1
- package/dist/esm/cli/deploy.js.map +1 -1
- package/dist/esm/cli/lib/envvars.js +8 -1
- package/dist/esm/cli/lib/envvars.js.map +2 -2
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/react/client.js +6 -43
- package/dist/esm/react/client.js.map +2 -2
- package/dist/esm/react/index.js +0 -1
- package/dist/esm/react/index.js.map +2 -2
- package/dist/esm/server/api.js.map +2 -2
- package/dist/esm-types/browser/index.d.ts +0 -1
- package/dist/esm-types/browser/index.d.ts.map +1 -1
- package/dist/esm-types/browser/query_options.d.ts +9 -12
- package/dist/esm-types/browser/query_options.d.ts.map +1 -1
- package/dist/esm-types/cli/codegen_templates/readme.d.ts.map +1 -1
- package/dist/esm-types/cli/lib/deployApi/definitionConfig.d.ts +24 -24
- package/dist/esm-types/cli/lib/deployApi/modules.d.ts +2 -2
- package/dist/esm-types/cli/lib/deployApi/startPush.d.ts +36 -36
- package/dist/esm-types/cli/lib/envvars.d.ts.map +1 -1
- package/dist/esm-types/cli/lib/mcp/tools/insights.d.ts +2 -2
- package/dist/esm-types/index.d.ts +1 -1
- package/dist/esm-types/react/client.d.ts +2 -54
- package/dist/esm-types/react/client.d.ts.map +1 -1
- package/dist/esm-types/react/index.d.ts +2 -7
- package/dist/esm-types/react/index.d.ts.map +1 -1
- package/dist/esm-types/server/api.d.ts +1 -5
- package/dist/esm-types/server/api.d.ts.map +1 -1
- package/dist/react.bundle.js +7 -50
- package/dist/react.bundle.js.map +3 -3
- package/package.json +1 -1
- package/src/browser/index.ts +0 -3
- package/src/browser/query_options.test.ts +9 -0
- package/src/browser/query_options.ts +15 -36
- package/src/cli/codegen_templates/readme.ts +1 -14
- package/src/cli/deploy.ts +1 -1
- package/src/cli/lib/envvars.ts +16 -1
- package/src/index.ts +1 -1
- package/src/react/client.test.tsx +0 -65
- package/src/react/client.ts +13 -129
- package/src/react/index.ts +1 -9
- package/src/server/api.ts +1 -5
- package/dist/cjs-types/react/use_query_object_options.test.d.ts +0 -5
- package/dist/cjs-types/react/use_query_object_options.test.d.ts.map +0 -1
- package/dist/cjs-types/react/use_query_result.test.d.ts +0 -5
- package/dist/cjs-types/react/use_query_result.test.d.ts.map +0 -1
- package/dist/esm-types/react/use_query_object_options.test.d.ts +0 -5
- package/dist/esm-types/react/use_query_object_options.test.d.ts.map +0 -1
- package/dist/esm-types/react/use_query_result.test.d.ts +0 -5
- package/dist/esm-types/react/use_query_result.test.d.ts.map +0 -1
- package/src/react/use_query_object_options.test.ts +0 -50
- package/src/react/use_query_result.test.ts +0 -41
package/package.json
CHANGED
package/src/browser/index.ts
CHANGED
|
@@ -40,6 +40,3 @@ export type { QueryJournal } from "./sync/protocol.js";
|
|
|
40
40
|
/** @internal */
|
|
41
41
|
export type { UserIdentityAttributes } from "./sync/protocol.js";
|
|
42
42
|
export type { FunctionResult } from "./sync/function_result.js";
|
|
43
|
-
/** @internal */
|
|
44
|
-
export { convexQueryOptions } from "./query_options.js";
|
|
45
|
-
export type { QueryOptions } from "./query_options.js";
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { test } from "vitest";
|
|
2
2
|
import { makeFunctionReference } from "../server/index.js";
|
|
3
3
|
import { EmptyObject } from "../server/registration.js";
|
|
4
|
+
import { ConvexReactClient } from "../react/client.js";
|
|
4
5
|
import { convexQueryOptions } from "./query_options.js";
|
|
5
6
|
|
|
6
7
|
const apiQueryFuncWithArgs = makeFunctionReference<
|
|
@@ -41,3 +42,11 @@ test("convexQueryOptions", async () => {
|
|
|
41
42
|
args: { name: "hey" },
|
|
42
43
|
});
|
|
43
44
|
});
|
|
45
|
+
|
|
46
|
+
test("prewarmQuery types", async () => {
|
|
47
|
+
const client = {
|
|
48
|
+
prewarmQuery: () => {},
|
|
49
|
+
} as unknown as ConvexReactClient;
|
|
50
|
+
|
|
51
|
+
client.prewarmQuery({ query: apiQueryFuncWithArgs, args: { name: "hi" } });
|
|
52
|
+
});
|
|
@@ -1,47 +1,26 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Query options are a potential new API for a variety of functions, but in particular a new overload of the React hook for queries.
|
|
3
|
+
*
|
|
4
|
+
* Inspired by https://tanstack.com/query/v5/docs/framework/react/guides/query-options
|
|
5
|
+
*/
|
|
2
6
|
import type { FunctionArgs, FunctionReference } from "../server/api.js";
|
|
3
7
|
|
|
8
|
+
// TODO if this type can encompass all use cases we can add not requiring args for queries
|
|
9
|
+
// that don't take arguments. Goal would be that queryOptions allows leaving out args,
|
|
10
|
+
// but queryOptions returns an object that always contains args. Helpers, "middleware,"
|
|
11
|
+
// anything that intercepts these arguments
|
|
4
12
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* Used with the object-form overload of {@link useQuery}.
|
|
8
|
-
*
|
|
9
|
-
* @public
|
|
13
|
+
* Query options.
|
|
10
14
|
*/
|
|
11
|
-
export type
|
|
12
|
-
/**
|
|
13
|
-
* The query function to run.
|
|
14
|
-
*/
|
|
15
|
+
export type ConvexQueryOptions<Query extends FunctionReference<"query">> = {
|
|
15
16
|
query: Query;
|
|
16
|
-
/**
|
|
17
|
-
* The arguments to the query function.
|
|
18
|
-
*/
|
|
19
17
|
args: FunctionArgs<Query>;
|
|
18
|
+
extendSubscriptionFor?: number;
|
|
20
19
|
};
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
* Creates a type-safe {@link QueryOptions} object for a Convex query.
|
|
24
|
-
*
|
|
25
|
-
* This is an identity function that exists to provide type inference — passing
|
|
26
|
-
* your query and args through this helper ensures TypeScript infers the correct
|
|
27
|
-
* `Query` type parameter, which enables precise return types on hooks like
|
|
28
|
-
* {@link useQuery}.
|
|
29
|
-
*
|
|
30
|
-
* ```typescript
|
|
31
|
-
* const opts = convexQueryOptions({
|
|
32
|
-
* query: api.users.getById,
|
|
33
|
-
* args: { id: userId },
|
|
34
|
-
* });
|
|
35
|
-
* // opts is typed as QueryOptions<typeof api.users.getById>
|
|
36
|
-
* client.prewarmQuery(opts);
|
|
37
|
-
* ```
|
|
38
|
-
*
|
|
39
|
-
* @param options - The query and its arguments.
|
|
40
|
-
* @returns The same object, typed as `QueryOptions<Query>`.
|
|
41
|
-
* @internal
|
|
42
|
-
*/
|
|
21
|
+
// This helper helps more once we have more inference happening.
|
|
43
22
|
export function convexQueryOptions<Query extends FunctionReference<"query">>(
|
|
44
|
-
options:
|
|
45
|
-
):
|
|
23
|
+
options: ConvexQueryOptions<Query>,
|
|
24
|
+
): ConvexQueryOptions<Query> {
|
|
46
25
|
return options;
|
|
47
26
|
}
|
|
@@ -37,20 +37,7 @@ export const myQueryFunction = query({
|
|
|
37
37
|
Using this query function in a React component looks like:
|
|
38
38
|
|
|
39
39
|
\`\`\`ts
|
|
40
|
-
const
|
|
41
|
-
query: api.myFunctions.myQueryFunction,
|
|
42
|
-
args: { first: 10, second: "hello" },
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
if (state.status === "pending") {
|
|
46
|
-
return <div>Loading...</div>;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (state.status === "error") {
|
|
50
|
-
return <div>Error: {state.error.message}</div>;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const data = state.data;
|
|
40
|
+
const data = useQuery(api.myFunctions.myQueryFunction, { first: 10, second: "hello" });
|
|
54
41
|
\`\`\`
|
|
55
42
|
|
|
56
43
|
|
package/src/cli/deploy.ts
CHANGED
|
@@ -49,7 +49,7 @@ export const deploy = new Command("deploy")
|
|
|
49
49
|
.addOption(
|
|
50
50
|
new Option(
|
|
51
51
|
"--preview-create <name>",
|
|
52
|
-
"The name to associate with this deployment if deploying to a newly created preview deployment. Defaults to the current Git branch name in Vercel, Netlify and GitHub CI. This parameter can only be used with a preview deploy key (when used with another type of key, the command will return an error).",
|
|
52
|
+
"The name to associate with this deployment if deploying to a newly created preview deployment. Defaults to the current Git branch name in Vercel, Netlify, Cloudflare Pages and GitHub CI. This parameter can only be used with a preview deploy key (when used with another type of key, the command will return an error).",
|
|
53
53
|
).conflicts("preview-name"),
|
|
54
54
|
)
|
|
55
55
|
.addOption(
|
package/src/cli/lib/envvars.ts
CHANGED
|
@@ -433,7 +433,9 @@ export function getBuildEnvironment(): string | false {
|
|
|
433
433
|
? "Vercel"
|
|
434
434
|
: process.env.NETLIFY
|
|
435
435
|
? "Netlify"
|
|
436
|
-
:
|
|
436
|
+
: process.env.CF_PAGES || process.env.WORKERS_CI
|
|
437
|
+
? "Cloudflare"
|
|
438
|
+
: false;
|
|
437
439
|
}
|
|
438
440
|
|
|
439
441
|
export function gitBranchFromEnvironment(): string | null {
|
|
@@ -445,6 +447,11 @@ export function gitBranchFromEnvironment(): string | null {
|
|
|
445
447
|
// https://docs.netlify.com/configure-builds/environment-variables/
|
|
446
448
|
return process.env.HEAD ?? null;
|
|
447
449
|
}
|
|
450
|
+
if (process.env.CF_PAGES || process.env.WORKERS_CI) {
|
|
451
|
+
// https://developers.cloudflare.com/pages/configuration/build-configuration/#environment-variables
|
|
452
|
+
// https://developers.cloudflare.com/workers/ci-cd/builds/configuration/#environment-variables
|
|
453
|
+
return process.env.CF_PAGES_BRANCH ?? process.env.WORKERS_CI_BRANCH ?? null;
|
|
454
|
+
}
|
|
448
455
|
|
|
449
456
|
if (process.env.CI) {
|
|
450
457
|
// https://docs.github.com/en/actions/learn-github-actions/variables
|
|
@@ -466,5 +473,13 @@ export function isNonProdBuildEnvironment(): boolean {
|
|
|
466
473
|
// https://docs.netlify.com/configure-builds/environment-variables/
|
|
467
474
|
return process.env.CONTEXT !== "production";
|
|
468
475
|
}
|
|
476
|
+
if (process.env.CF_PAGES || process.env.WORKERS_CI) {
|
|
477
|
+
// https://developers.cloudflare.com/pages/configuration/build-configuration/#environment-variables
|
|
478
|
+
// https://developers.cloudflare.com/workers/ci-cd/builds/configuration/#environment-variables
|
|
479
|
+
// Branch !== "main" is the closest heuristic; Cloudflare Pages
|
|
480
|
+
// does not expose a dedicated production/preview flag.
|
|
481
|
+
const branch = process.env.CF_PAGES_BRANCH ?? process.env.WORKERS_CI_BRANCH;
|
|
482
|
+
return branch !== "main";
|
|
483
|
+
}
|
|
469
484
|
return false;
|
|
470
485
|
}
|
package/src/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "1.35.
|
|
1
|
+
export const version = "1.35.1";
|
|
@@ -5,7 +5,6 @@ import { test, expect, describe, vi } from "vitest";
|
|
|
5
5
|
import ws from "ws";
|
|
6
6
|
|
|
7
7
|
import { ConvexReactClient, createMutation, useQuery } from "./client.js";
|
|
8
|
-
import { convexQueryOptions } from "../browser/query_options.js";
|
|
9
8
|
import { ConvexProvider } from "./index.js";
|
|
10
9
|
import React from "react";
|
|
11
10
|
import { renderHook } from "@testing-library/react";
|
|
@@ -114,46 +113,6 @@ describe("useQuery", () => {
|
|
|
114
113
|
expect(result.current).toStrictEqual(undefined);
|
|
115
114
|
});
|
|
116
115
|
|
|
117
|
-
test("object form returns success result", () => {
|
|
118
|
-
const client = createClientWithQuery();
|
|
119
|
-
const wrapper = ({ children }: any) => (
|
|
120
|
-
<ConvexProvider client={client}>{children}</ConvexProvider>
|
|
121
|
-
);
|
|
122
|
-
const { result } = renderHook(
|
|
123
|
-
() =>
|
|
124
|
-
useQuery({
|
|
125
|
-
query: anyApi.myQuery.default,
|
|
126
|
-
args: {},
|
|
127
|
-
}),
|
|
128
|
-
{ wrapper },
|
|
129
|
-
);
|
|
130
|
-
expect(result.current).toStrictEqual({
|
|
131
|
-
data: "queryResult",
|
|
132
|
-
error: undefined,
|
|
133
|
-
status: "success",
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
test("object form returns pending when skipped", () => {
|
|
138
|
-
const client = createClientWithQuery();
|
|
139
|
-
const wrapper = ({ children }: any) => (
|
|
140
|
-
<ConvexProvider client={client}>{children}</ConvexProvider>
|
|
141
|
-
);
|
|
142
|
-
const { result } = renderHook(
|
|
143
|
-
() =>
|
|
144
|
-
useQuery({
|
|
145
|
-
query: anyApi.myQuery.default,
|
|
146
|
-
args: "skip",
|
|
147
|
-
}),
|
|
148
|
-
{ wrapper },
|
|
149
|
-
);
|
|
150
|
-
expect(result.current).toStrictEqual({
|
|
151
|
-
data: undefined,
|
|
152
|
-
error: undefined,
|
|
153
|
-
status: "pending",
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
|
|
157
116
|
test("Optimistic update handlers can’t be async", () => {
|
|
158
117
|
const client = testConvexReactClient();
|
|
159
118
|
const mutation = createMutation(
|
|
@@ -233,27 +192,3 @@ describe("async query fetch", () => {
|
|
|
233
192
|
expect(await queryResult).toStrictEqual("queryResult");
|
|
234
193
|
});
|
|
235
194
|
});
|
|
236
|
-
|
|
237
|
-
describe("prewarmQuery types", () => {
|
|
238
|
-
test("accepts QueryOptions shape", () => {
|
|
239
|
-
const client = testConvexReactClient();
|
|
240
|
-
const opts = convexQueryOptions({
|
|
241
|
-
query: makeFunctionReference<"query", { name: string }, string>(
|
|
242
|
-
"myQuery",
|
|
243
|
-
),
|
|
244
|
-
args: { name: "hi" },
|
|
245
|
-
});
|
|
246
|
-
client.prewarmQuery(opts);
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
test("accepts extendSubscriptionFor on prewarmQuery", () => {
|
|
250
|
-
const client = testConvexReactClient();
|
|
251
|
-
client.prewarmQuery({
|
|
252
|
-
query: makeFunctionReference<"query", { name: string }, string>(
|
|
253
|
-
"myQuery",
|
|
254
|
-
),
|
|
255
|
-
args: { name: "hi" },
|
|
256
|
-
extendSubscriptionFor: 10_000,
|
|
257
|
-
});
|
|
258
|
-
});
|
|
259
|
-
});
|
package/src/react/client.ts
CHANGED
|
@@ -32,7 +32,7 @@ import {
|
|
|
32
32
|
instantiateNoopLogger,
|
|
33
33
|
Logger,
|
|
34
34
|
} from "../browser/logging.js";
|
|
35
|
-
import
|
|
35
|
+
import { ConvexQueryOptions } from "../browser/query_options.js";
|
|
36
36
|
import { LoadMoreOfPaginatedQuery } from "../browser/sync/pagination.js";
|
|
37
37
|
import {
|
|
38
38
|
PaginatedQueryClient,
|
|
@@ -537,7 +537,9 @@ export class ConvexReactClient {
|
|
|
537
537
|
* an optional extendSubscriptionFor for how long to subscribe to the query.
|
|
538
538
|
*/
|
|
539
539
|
prewarmQuery<Query extends FunctionReference<"query">>(
|
|
540
|
-
queryOptions:
|
|
540
|
+
queryOptions: ConvexQueryOptions<Query> & {
|
|
541
|
+
extendSubscriptionFor?: number;
|
|
542
|
+
},
|
|
541
543
|
) {
|
|
542
544
|
const extendSubscriptionFor =
|
|
543
545
|
queryOptions.extendSubscriptionFor ?? DEFAULT_EXTEND_SUBSCRIPTION_FOR;
|
|
@@ -799,34 +801,6 @@ export type OptionalRestArgsOrSkip<FuncRef extends FunctionReference<any>> =
|
|
|
799
801
|
? [args?: EmptyObject | "skip"]
|
|
800
802
|
: [args: FuncRef["_args"] | "skip"];
|
|
801
803
|
|
|
802
|
-
/**
|
|
803
|
-
* Result returned by object-form {@link useQuery}.
|
|
804
|
-
*
|
|
805
|
-
* @public
|
|
806
|
-
*/
|
|
807
|
-
export type UseQueryResult<QueryResult> =
|
|
808
|
-
| {
|
|
809
|
-
data: QueryResult;
|
|
810
|
-
error: undefined;
|
|
811
|
-
status: "success";
|
|
812
|
-
}
|
|
813
|
-
| {
|
|
814
|
-
data: undefined;
|
|
815
|
-
error: Error;
|
|
816
|
-
status: "error";
|
|
817
|
-
}
|
|
818
|
-
| {
|
|
819
|
-
data: undefined;
|
|
820
|
-
error: undefined;
|
|
821
|
-
status: "pending";
|
|
822
|
-
};
|
|
823
|
-
|
|
824
|
-
type UseQueryOptions<Query extends FunctionReference<"query">> = {
|
|
825
|
-
query: Query;
|
|
826
|
-
args: FunctionArgs<Query> | "skip";
|
|
827
|
-
throwOnError?: boolean;
|
|
828
|
-
};
|
|
829
|
-
|
|
830
804
|
/**
|
|
831
805
|
* Load a reactive query within a React component.
|
|
832
806
|
*
|
|
@@ -873,82 +847,20 @@ type UseQueryOptions<Query extends FunctionReference<"query">> = {
|
|
|
873
847
|
export function useQuery<Query extends FunctionReference<"query">>(
|
|
874
848
|
query: Query,
|
|
875
849
|
...args: OptionalRestArgsOrSkip<Query>
|
|
876
|
-
): Query["_returnType"] | undefined
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
* Load a reactive query within a React component using an options object.
|
|
880
|
-
*
|
|
881
|
-
* This is an alternative form of {@link useQuery} that accepts a single
|
|
882
|
-
* {@link UseQueryOptions} object instead of positional arguments.
|
|
883
|
-
* Errors are returned in the result object unless `throwOnError` is set.
|
|
884
|
-
*
|
|
885
|
-
* @example
|
|
886
|
-
* ```tsx
|
|
887
|
-
* import { useQuery } from "convex/react";
|
|
888
|
-
* import { api } from "../convex/_generated/api";
|
|
889
|
-
*
|
|
890
|
-
* function TaskList() {
|
|
891
|
-
* const state = useQuery({ query: api.tasks.list, args: { completed: false } });
|
|
892
|
-
*
|
|
893
|
-
* if (state.status === "pending") return <div>Loading...</div>;
|
|
894
|
-
* if (state.status === "error") return <div>Error: {state.error.message}</div>;
|
|
895
|
-
* return state.data.map((task) => <div key={task._id}>{task.text}</div>);
|
|
896
|
-
* }
|
|
897
|
-
* ```
|
|
898
|
-
*
|
|
899
|
-
* @param options - Query options. Pass `args: "skip"` to disable the query.
|
|
900
|
-
* @returns the current query state as a {@link UseQueryResult} object.
|
|
901
|
-
*
|
|
902
|
-
* @see https://docs.convex.dev/client/react#fetching-data
|
|
903
|
-
* @public
|
|
904
|
-
*/
|
|
905
|
-
export function useQuery<Query extends FunctionReference<"query">>(
|
|
906
|
-
options: UseQueryOptions<Query>,
|
|
907
|
-
): UseQueryResult<Query["_returnType"]>;
|
|
850
|
+
): Query["_returnType"] | undefined {
|
|
851
|
+
const skip = args[0] === "skip";
|
|
852
|
+
const argsObject = args[0] === "skip" ? {} : parseArgs(args[0]);
|
|
908
853
|
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
const isObjectOptions =
|
|
914
|
-
typeof queryOrOptions === "object" &&
|
|
915
|
-
queryOrOptions !== null &&
|
|
916
|
-
"query" in queryOrOptions;
|
|
917
|
-
const throwOnError = isObjectOptions
|
|
918
|
-
? (queryOrOptions.throwOnError ?? false)
|
|
919
|
-
: true;
|
|
920
|
-
|
|
921
|
-
let queryReference: Query | undefined;
|
|
922
|
-
let argsObject: Record<string, Value> = {};
|
|
923
|
-
|
|
924
|
-
if (isObjectOptions) {
|
|
925
|
-
const query = queryOrOptions.query;
|
|
926
|
-
queryReference =
|
|
927
|
-
typeof query === "string"
|
|
928
|
-
? (makeFunctionReference<"query", any, any>(query) as Query)
|
|
929
|
-
: query;
|
|
930
|
-
if (queryOrOptions.args !== "skip") {
|
|
931
|
-
argsObject = parseArgs(queryOrOptions.args as Record<string, Value>);
|
|
932
|
-
}
|
|
933
|
-
} else {
|
|
934
|
-
const query = queryOrOptions;
|
|
935
|
-
queryReference =
|
|
936
|
-
typeof query === "string"
|
|
937
|
-
? (makeFunctionReference<"query", any, any>(query) as Query)
|
|
938
|
-
: query;
|
|
939
|
-
argsObject = args[0] === "skip" ? {} : parseArgs(args[0] as Query["_args"]);
|
|
940
|
-
}
|
|
854
|
+
const queryReference =
|
|
855
|
+
typeof query === "string"
|
|
856
|
+
? makeFunctionReference<"query", any, any>(query)
|
|
857
|
+
: query;
|
|
941
858
|
|
|
942
|
-
const queryName = queryReference
|
|
943
|
-
? getFunctionName(queryReference)
|
|
944
|
-
: undefined;
|
|
945
|
-
const skip =
|
|
946
|
-
(isObjectOptions && queryOrOptions.args === "skip") ||
|
|
947
|
-
(!isObjectOptions && args[0] === "skip");
|
|
859
|
+
const queryName = getFunctionName(queryReference);
|
|
948
860
|
|
|
949
861
|
const queries = useMemo(
|
|
950
862
|
() =>
|
|
951
|
-
skip
|
|
863
|
+
skip
|
|
952
864
|
? ({} as RequestForQueries)
|
|
953
865
|
: { query: { query: queryReference, args: argsObject } },
|
|
954
866
|
// Stringify args so args that are semantically the same don't trigger a
|
|
@@ -959,34 +871,6 @@ export function useQuery<Query extends FunctionReference<"query">>(
|
|
|
959
871
|
|
|
960
872
|
const results = useQueries(queries);
|
|
961
873
|
const result = results["query"];
|
|
962
|
-
|
|
963
|
-
if (isObjectOptions) {
|
|
964
|
-
if (result instanceof Error) {
|
|
965
|
-
if (throwOnError) {
|
|
966
|
-
throw result;
|
|
967
|
-
}
|
|
968
|
-
return {
|
|
969
|
-
data: undefined,
|
|
970
|
-
error: result,
|
|
971
|
-
status: "error",
|
|
972
|
-
};
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
if (result === undefined) {
|
|
976
|
-
return {
|
|
977
|
-
data: undefined,
|
|
978
|
-
error: undefined,
|
|
979
|
-
status: "pending",
|
|
980
|
-
};
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
return {
|
|
984
|
-
data: result,
|
|
985
|
-
error: undefined,
|
|
986
|
-
status: "success",
|
|
987
|
-
};
|
|
988
|
-
}
|
|
989
|
-
|
|
990
874
|
if (result instanceof Error) {
|
|
991
875
|
throw result;
|
|
992
876
|
}
|
package/src/react/index.ts
CHANGED
|
@@ -53,11 +53,7 @@
|
|
|
53
53
|
* import { api } from "../convex/_generated/api";
|
|
54
54
|
*
|
|
55
55
|
* function App() {
|
|
56
|
-
* const counter = useQuery(
|
|
57
|
-
* query: api.getCounter.default,
|
|
58
|
-
* args: {},
|
|
59
|
-
* throwOnError: true,
|
|
60
|
-
* }).data;
|
|
56
|
+
* const counter = useQuery(api.getCounter.default);
|
|
61
57
|
* const increment = useMutation(api.incrementCounter.default);
|
|
62
58
|
* // Your component here!
|
|
63
59
|
* }
|
|
@@ -82,7 +78,6 @@ export {
|
|
|
82
78
|
type MutationOptions,
|
|
83
79
|
type ConvexReactClientOptions,
|
|
84
80
|
type OptionalRestArgsOrSkip,
|
|
85
|
-
type UseQueryResult,
|
|
86
81
|
ConvexReactClient,
|
|
87
82
|
useConvex,
|
|
88
83
|
ConvexProvider,
|
|
@@ -91,6 +86,3 @@ export {
|
|
|
91
86
|
useAction,
|
|
92
87
|
useConvexConnectionState,
|
|
93
88
|
} from "./client.js";
|
|
94
|
-
/** @internal */
|
|
95
|
-
export { convexQueryOptions } from "../browser/query_options.js";
|
|
96
|
-
export type { QueryOptions } from "../browser/query_options.js";
|
package/src/server/api.ts
CHANGED
|
@@ -39,11 +39,7 @@ export type FunctionType = "query" | "mutation" | "action";
|
|
|
39
39
|
* Function references can be used to invoke functions from the client. For
|
|
40
40
|
* example, in React you can pass references to the {@link react.useQuery} hook:
|
|
41
41
|
* ```js
|
|
42
|
-
* const result = useQuery(
|
|
43
|
-
* query: api.myModule.myFunction,
|
|
44
|
-
* args: {},
|
|
45
|
-
* throwOnError: true,
|
|
46
|
-
* }).data;
|
|
42
|
+
* const result = useQuery(api.myModule.myFunction);
|
|
47
43
|
* ```
|
|
48
44
|
*
|
|
49
45
|
* @typeParam Type - The type of the function ("query", "mutation", or "action").
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"use_query_object_options.test.d.ts","sourceRoot":"","sources":["../../../src/react/use_query_object_options.test.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"use_query_result.test.d.ts","sourceRoot":"","sources":["../../../src/react/use_query_result.test.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"use_query_object_options.test.d.ts","sourceRoot":"","sources":["../../../src/react/use_query_object_options.test.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"use_query_result.test.d.ts","sourceRoot":"","sources":["../../../src/react/use_query_result.test.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @vitest-environment custom-vitest-environment.ts
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
6
|
-
import { test, describe } from "vitest";
|
|
7
|
-
import { anyApi } from "../server/api.js";
|
|
8
|
-
|
|
9
|
-
import type { ApiFromModules, QueryBuilder } from "../server/index.js";
|
|
10
|
-
import { useQuery as useQueryReal } from "./client.js";
|
|
11
|
-
|
|
12
|
-
const useQuery = (() => {}) as unknown as typeof useQueryReal;
|
|
13
|
-
const query: QueryBuilder<any, "public"> = (() => {}) as any;
|
|
14
|
-
|
|
15
|
-
const module = {
|
|
16
|
-
noArgs: query(() => "result"),
|
|
17
|
-
args: query((_ctx, { _arg }: { _arg: string }) => "result"),
|
|
18
|
-
};
|
|
19
|
-
type API = ApiFromModules<{ module: typeof module }>;
|
|
20
|
-
const api = anyApi as unknown as API;
|
|
21
|
-
|
|
22
|
-
describe("useQuery object options", () => {
|
|
23
|
-
test("supports object options and skip sentinel", () => {
|
|
24
|
-
useQuery({
|
|
25
|
-
query: api.module.noArgs,
|
|
26
|
-
args: {},
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
useQuery({
|
|
30
|
-
query: api.module.args,
|
|
31
|
-
args: { _arg: "asdf" },
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
const _arg: string | undefined = undefined;
|
|
35
|
-
useQuery({
|
|
36
|
-
query: api.module.args,
|
|
37
|
-
args: _arg ? { _arg } : "skip",
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
useQuery({
|
|
41
|
-
query: api.module.args,
|
|
42
|
-
args: { _arg: "asdf" },
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
useQuery({
|
|
46
|
-
query: api.module.noArgs,
|
|
47
|
-
args: "skip",
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
});
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @vitest-environment custom-vitest-environment.ts
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
6
|
-
import { test, describe } from "vitest";
|
|
7
|
-
import { anyApi } from "../server/api.js";
|
|
8
|
-
|
|
9
|
-
import type { ApiFromModules, QueryBuilder } from "../server/index.js";
|
|
10
|
-
import { useQuery as useQueryReal } from "./client.js";
|
|
11
|
-
|
|
12
|
-
const useQuery = (() => {}) as unknown as typeof useQueryReal;
|
|
13
|
-
const query: QueryBuilder<any, "public"> = (() => {}) as any;
|
|
14
|
-
|
|
15
|
-
const module = {
|
|
16
|
-
noArgs: query(() => "result"),
|
|
17
|
-
args: query((_ctx, { _arg }: { _arg: string }) => "result"),
|
|
18
|
-
};
|
|
19
|
-
type API = ApiFromModules<{ module: typeof module }>;
|
|
20
|
-
const api = anyApi as unknown as API;
|
|
21
|
-
|
|
22
|
-
describe("useQuery object-form result types", () => {
|
|
23
|
-
test("supports object-form result usage", () => {
|
|
24
|
-
useQuery({
|
|
25
|
-
query: api.module.args,
|
|
26
|
-
args: { _arg: "asdf" },
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
useQuery({
|
|
30
|
-
query: api.module.args,
|
|
31
|
-
args: { _arg: "asdf" },
|
|
32
|
-
throwOnError: true,
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
const _arg: string | undefined = undefined;
|
|
36
|
-
useQuery({
|
|
37
|
-
query: api.module.args,
|
|
38
|
-
args: _arg ? { _arg } : "skip",
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
});
|