naystack 1.8.1 → 1.8.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.
@@ -115,9 +115,10 @@ var tokenContext = (token) => {
115
115
  credentials: `omit`
116
116
  };
117
117
  };
118
- function useAuthQuery(query, variables) {
118
+ function useAuthQuery(query, variables, options) {
119
119
  const [fetch, result] = (0, import_client.useLazyQuery)(query, {
120
- fetchPolicy: "no-cache"
120
+ fetchPolicy: "no-cache",
121
+ ...options
121
122
  });
122
123
  const prevVarsRef = (0, import_react.useRef)(null);
123
124
  (0, import_react.useEffect)(() => {
@@ -1,5 +1,5 @@
1
1
  import * as _apollo_client from '@apollo/client';
2
- import { InMemoryCacheConfig, OperationVariables, MutationHookOptions, ApolloClient } from '@apollo/client';
2
+ import { InMemoryCacheConfig, OperationVariables, LazyQueryHookOptions, MutationHookOptions, ApolloClient } from '@apollo/client';
3
3
  import { TypedDocumentNode } from '@graphql-typed-document-node/core';
4
4
  import React__default, { PropsWithChildren } from 'react';
5
5
 
@@ -25,10 +25,14 @@ declare const tokenContext: (token?: string | null) => {
25
25
  * Hook to run a GraphQL query with the current user's token. The query auto-fires when both the token
26
26
  * and variables are available. Returns a refetch function and the Apollo query result.
27
27
  *
28
- * Uses `fetchPolicy: "no-cache"` by default to always get fresh data.
28
+ * Uses Apollo's default `fetchPolicy` (`cache-first`) unless you override it via
29
+ * `options`. Pass `{ fetchPolicy: "no-cache" }` for always-fresh, viewer-keyed data
30
+ * (e.g. the current user), or `{ fetchPolicy: "cache-and-network" }` to show cached
31
+ * data instantly while refetching.
29
32
  *
30
33
  * @param query - A `TypedDocumentNode` for the query (e.g. from codegen or a `gql` template).
31
34
  * @param variables - Optional initial variables (the `input` value). Automatically wrapped as `{ input: variables }` before sending. Query fires automatically when this and token are set; change to refetch.
35
+ * @param options - Optional Apollo `LazyQueryHookOptions` (e.g. `fetchPolicy`, `notifyOnNetworkStatusChange`). Merged into the underlying `useLazyQuery`.
32
36
  * @returns Tuple: `[refetch, result]`.
33
37
  * - `refetch(input)` — runs the query again with the given input (wrapped as `variables.input`).
34
38
  * - `result` — `{ data, loading, error, hasAuth }` from Apollo. `hasAuth` is `true` when an auth token is available.
@@ -58,7 +62,7 @@ declare const tokenContext: (token?: string | null) => {
58
62
  *
59
63
  * @category GraphQL
60
64
  */
61
- declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocumentNode<T, V>, variables?: V["input"]): readonly [(input?: V["input"]) => Promise<_apollo_client.QueryResult<T, V>>, _apollo_client.QueryResult<T, V>];
65
+ declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocumentNode<T, V>, variables?: V["input"], options?: LazyQueryHookOptions<T, V>): readonly [(input?: V["input"]) => Promise<_apollo_client.QueryResult<T, V>>, _apollo_client.QueryResult<T, V>];
62
66
  /**
63
67
  * Hook to run a GraphQL mutation with the current user's token. Returns a function you call with the mutation input.
64
68
  *
@@ -1,5 +1,5 @@
1
1
  import * as _apollo_client from '@apollo/client';
2
- import { InMemoryCacheConfig, OperationVariables, MutationHookOptions, ApolloClient } from '@apollo/client';
2
+ import { InMemoryCacheConfig, OperationVariables, LazyQueryHookOptions, MutationHookOptions, ApolloClient } from '@apollo/client';
3
3
  import { TypedDocumentNode } from '@graphql-typed-document-node/core';
4
4
  import React__default, { PropsWithChildren } from 'react';
5
5
 
@@ -25,10 +25,14 @@ declare const tokenContext: (token?: string | null) => {
25
25
  * Hook to run a GraphQL query with the current user's token. The query auto-fires when both the token
26
26
  * and variables are available. Returns a refetch function and the Apollo query result.
27
27
  *
28
- * Uses `fetchPolicy: "no-cache"` by default to always get fresh data.
28
+ * Uses Apollo's default `fetchPolicy` (`cache-first`) unless you override it via
29
+ * `options`. Pass `{ fetchPolicy: "no-cache" }` for always-fresh, viewer-keyed data
30
+ * (e.g. the current user), or `{ fetchPolicy: "cache-and-network" }` to show cached
31
+ * data instantly while refetching.
29
32
  *
30
33
  * @param query - A `TypedDocumentNode` for the query (e.g. from codegen or a `gql` template).
31
34
  * @param variables - Optional initial variables (the `input` value). Automatically wrapped as `{ input: variables }` before sending. Query fires automatically when this and token are set; change to refetch.
35
+ * @param options - Optional Apollo `LazyQueryHookOptions` (e.g. `fetchPolicy`, `notifyOnNetworkStatusChange`). Merged into the underlying `useLazyQuery`.
32
36
  * @returns Tuple: `[refetch, result]`.
33
37
  * - `refetch(input)` — runs the query again with the given input (wrapped as `variables.input`).
34
38
  * - `result` — `{ data, loading, error, hasAuth }` from Apollo. `hasAuth` is `true` when an auth token is available.
@@ -58,7 +62,7 @@ declare const tokenContext: (token?: string | null) => {
58
62
  *
59
63
  * @category GraphQL
60
64
  */
61
- declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocumentNode<T, V>, variables?: V["input"]): readonly [(input?: V["input"]) => Promise<_apollo_client.QueryResult<T, V>>, _apollo_client.QueryResult<T, V>];
65
+ declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocumentNode<T, V>, variables?: V["input"], options?: LazyQueryHookOptions<T, V>): readonly [(input?: V["input"]) => Promise<_apollo_client.QueryResult<T, V>>, _apollo_client.QueryResult<T, V>];
62
66
  /**
63
67
  * Hook to run a GraphQL mutation with the current user's token. Returns a function you call with the mutation input.
64
68
  *
@@ -90,9 +90,10 @@ var tokenContext = (token) => {
90
90
  credentials: `omit`
91
91
  };
92
92
  };
93
- function useAuthQuery(query, variables) {
93
+ function useAuthQuery(query, variables, options) {
94
94
  const [fetch, result] = useLazyQuery(query, {
95
- fetchPolicy: "no-cache"
95
+ fetchPolicy: "no-cache",
96
+ ...options
96
97
  });
97
98
  const prevVarsRef = useRef(null);
98
99
  useEffect(() => {
@@ -34,8 +34,15 @@ type Promisify<T> = T | Promise<T>;
34
34
  * there's no refresh cookie the only way to keep that promise is to not call the
35
35
  * resolver at all — hence the added `null`, which surfaces the logged-out case in
36
36
  * the types instead of passing a null `userId` through the cast.
37
+ *
38
+ * `[IsAuth] extends [true]` (not `IsAuth extends true`) is deliberate: `field()`
39
+ * has no `= false` default and can't reverse-infer `IsAuth` from a `ctx: Context`
40
+ * signature, so a non-authorized field infers `IsAuth = boolean`. A naked
41
+ * conditional would distribute over `true | false` and union `null` into every
42
+ * public field's return. The tuple wrapper stops the distribution, so only a
43
+ * literal `true` adds `null`.
37
44
  */
38
- type AuthCallReturn<IsAuth extends boolean, R> = IsAuth extends true ? Awaited<R> | null : Awaited<R>;
45
+ type AuthCallReturn<IsAuth extends boolean, R> = [IsAuth] extends [true] ? Awaited<R> | null : Awaited<R>;
39
46
  /** Base for query/field definition (output, input, options). */
40
47
  interface BaseDefinition<T, U, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false> {
41
48
  output: T;
@@ -34,8 +34,15 @@ type Promisify<T> = T | Promise<T>;
34
34
  * there's no refresh cookie the only way to keep that promise is to not call the
35
35
  * resolver at all — hence the added `null`, which surfaces the logged-out case in
36
36
  * the types instead of passing a null `userId` through the cast.
37
+ *
38
+ * `[IsAuth] extends [true]` (not `IsAuth extends true`) is deliberate: `field()`
39
+ * has no `= false` default and can't reverse-infer `IsAuth` from a `ctx: Context`
40
+ * signature, so a non-authorized field infers `IsAuth = boolean`. A naked
41
+ * conditional would distribute over `true | false` and union `null` into every
42
+ * public field's return. The tuple wrapper stops the distribution, so only a
43
+ * literal `true` adds `null`.
37
44
  */
38
- type AuthCallReturn<IsAuth extends boolean, R> = IsAuth extends true ? Awaited<R> | null : Awaited<R>;
45
+ type AuthCallReturn<IsAuth extends boolean, R> = [IsAuth] extends [true] ? Awaited<R> | null : Awaited<R>;
39
46
  /** Base for query/field definition (output, input, options). */
40
47
  interface BaseDefinition<T, U, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false> {
41
48
  output: T;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "naystack",
3
- "version": "1.8.1",
3
+ "version": "1.8.2",
4
4
  "description": "A stack built with Next + GraphQL + S3 + Auth",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",