naystack 1.5.41 → 1.6.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.
@@ -118,41 +118,42 @@ var tokenContext = (token) => {
118
118
  function useAuthQuery(query, variables) {
119
119
  const token = (0, import_client2.useToken)();
120
120
  const [fetch, result] = (0, import_client.useLazyQuery)(query);
121
- const [calledVars, setCalledVars] = (0, import_react.useState)();
121
+ const prevVarsRef = (0, import_react.useRef)(null);
122
122
  (0, import_react.useEffect)(() => {
123
- if (token && variables && calledVars !== JSON.stringify(variables)) {
124
- setCalledVars(JSON.stringify(variables));
123
+ const serialized = JSON.stringify(variables);
124
+ if (token && variables && prevVarsRef.current !== serialized) {
125
+ prevVarsRef.current = serialized;
125
126
  void fetch({
126
127
  // @ts-expect-error -- to allow dynamic props
127
- variables: { input },
128
+ variables: { input: variables },
128
129
  context: tokenContext(token),
129
130
  fetchPolicy: "no-cache"
130
131
  });
131
132
  }
132
- }, [fetch, token, variables, calledVars]);
133
+ }, [fetch, token, variables]);
133
134
  const reFetch = (0, import_react.useCallback)(
134
- (input2) => fetch({
135
+ (input) => fetch({
135
136
  // @ts-expect-error -- to allow dynamic props
136
- variables: { input: input2 },
137
+ variables: { input },
137
138
  context: tokenContext(token),
138
139
  fetchPolicy: "no-cache"
139
140
  }),
140
141
  [fetch, token]
141
142
  );
142
- return [reFetch, result];
143
+ return [reFetch, { ...result, hasAuth: !!token }];
143
144
  }
144
145
  function useAuthMutation(mutation, options) {
145
146
  const token = (0, import_client2.useToken)();
146
147
  const [mutate, result] = (0, import_client.useMutation)(mutation, options);
147
148
  const method = (0, import_react.useCallback)(
148
- (input2) => mutate({
149
+ (input) => mutate({
149
150
  // @ts-expect-error -- to allow dynamic props
150
- variables: { input: input2 },
151
+ variables: { input },
151
152
  context: tokenContext(token)
152
153
  }),
153
154
  [token]
154
155
  );
155
- return [method, result];
156
+ return [method, { ...result, hasAuth: !!token }];
156
157
  }
157
158
  // Annotate the CommonJS export names for ESM import in node:
158
159
  0 && (module.exports = {
@@ -1,5 +1,6 @@
1
+ import * as graphql from 'graphql';
1
2
  import * as _apollo_client from '@apollo/client';
2
- import { InMemoryCacheConfig, OperationVariables, MutationHookOptions } from '@apollo/client';
3
+ import { InMemoryCacheConfig, OperationVariables, ApolloClient, MutationHookOptions } from '@apollo/client';
3
4
  import { TypedDocumentNode } from '@graphql-typed-document-node/core';
4
5
  import React__default, { PropsWithChildren } from 'react';
5
6
 
@@ -58,7 +59,31 @@ declare const tokenContext: (token?: string | null) => {
58
59
  *
59
60
  * @category GraphQL
60
61
  */
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>];
62
+ declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocumentNode<T, V>, variables?: V["input"]): readonly [(input?: V["input"]) => Promise<_apollo_client.QueryResult<T, V>>, {
63
+ readonly hasAuth: false;
64
+ readonly client: ApolloClient<any>;
65
+ readonly observable: _apollo_client.ObservableQuery<T, V>;
66
+ readonly data: T | undefined;
67
+ readonly previousData?: T | undefined;
68
+ readonly error?: _apollo_client.ApolloError;
69
+ readonly errors?: ReadonlyArray<graphql.GraphQLFormattedError>;
70
+ readonly loading: boolean;
71
+ readonly networkStatus: _apollo_client.NetworkStatus;
72
+ readonly called: boolean;
73
+ readonly startPolling: (pollInterval: number) => void;
74
+ readonly stopPolling: () => void;
75
+ readonly subscribeToMore: _apollo_client.SubscribeToMoreFunction<T, V>;
76
+ readonly updateQuery: (mapFn: _apollo_client.UpdateQueryMapFn<T, V>) => void;
77
+ readonly refetch: (variables?: Partial<V> | undefined) => Promise<_apollo_client.ApolloQueryResult<T>>;
78
+ readonly reobserve: (newOptions?: Partial<_apollo_client.WatchQueryOptions<V, T>> | undefined, newNetworkStatus?: _apollo_client.NetworkStatus) => Promise<_apollo_client.ApolloQueryResult<T>>;
79
+ readonly variables: V | undefined;
80
+ readonly fetchMore: <TFetchData = T, TFetchVars extends OperationVariables = V>(fetchMoreOptions: _apollo_client.FetchMoreQueryOptions<TFetchVars, TFetchData> & {
81
+ updateQuery?: ((previousQueryResult: _apollo_client.Unmasked<T>, options: {
82
+ fetchMoreResult: _apollo_client.Unmasked<TFetchData>;
83
+ variables: TFetchVars;
84
+ }) => _apollo_client.Unmasked<T>) | undefined;
85
+ }) => Promise<_apollo_client.ApolloQueryResult<TFetchData>>;
86
+ }];
62
87
  /**
63
88
  * Hook to run a GraphQL mutation with the current user's token. Returns a function you call with the mutation input.
64
89
  *
@@ -94,6 +119,14 @@ declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocum
94
119
  *
95
120
  * @category GraphQL
96
121
  */
97
- declare function useAuthMutation<T, V extends OperationVariables>(mutation: TypedDocumentNode<T, V>, options?: MutationHookOptions<T, V>): readonly [(input?: V["input"]) => Promise<_apollo_client.FetchResult<T>>, _apollo_client.MutationResult<T>];
122
+ declare function useAuthMutation<T, V extends OperationVariables>(mutation: TypedDocumentNode<T, V>, options?: MutationHookOptions<T, V>): readonly [(input?: V["input"]) => Promise<_apollo_client.FetchResult<T>>, {
123
+ readonly hasAuth: false;
124
+ readonly data?: T | null | undefined;
125
+ readonly error?: _apollo_client.ApolloError;
126
+ readonly loading: boolean;
127
+ readonly called: boolean;
128
+ readonly client: ApolloClient<object>;
129
+ readonly reset: () => void;
130
+ }];
98
131
 
99
132
  export { ApolloWrapper, tokenContext, useAuthMutation, useAuthQuery };
@@ -1,5 +1,6 @@
1
+ import * as graphql from 'graphql';
1
2
  import * as _apollo_client from '@apollo/client';
2
- import { InMemoryCacheConfig, OperationVariables, MutationHookOptions } from '@apollo/client';
3
+ import { InMemoryCacheConfig, OperationVariables, ApolloClient, MutationHookOptions } from '@apollo/client';
3
4
  import { TypedDocumentNode } from '@graphql-typed-document-node/core';
4
5
  import React__default, { PropsWithChildren } from 'react';
5
6
 
@@ -58,7 +59,31 @@ declare const tokenContext: (token?: string | null) => {
58
59
  *
59
60
  * @category GraphQL
60
61
  */
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>];
62
+ declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocumentNode<T, V>, variables?: V["input"]): readonly [(input?: V["input"]) => Promise<_apollo_client.QueryResult<T, V>>, {
63
+ readonly hasAuth: false;
64
+ readonly client: ApolloClient<any>;
65
+ readonly observable: _apollo_client.ObservableQuery<T, V>;
66
+ readonly data: T | undefined;
67
+ readonly previousData?: T | undefined;
68
+ readonly error?: _apollo_client.ApolloError;
69
+ readonly errors?: ReadonlyArray<graphql.GraphQLFormattedError>;
70
+ readonly loading: boolean;
71
+ readonly networkStatus: _apollo_client.NetworkStatus;
72
+ readonly called: boolean;
73
+ readonly startPolling: (pollInterval: number) => void;
74
+ readonly stopPolling: () => void;
75
+ readonly subscribeToMore: _apollo_client.SubscribeToMoreFunction<T, V>;
76
+ readonly updateQuery: (mapFn: _apollo_client.UpdateQueryMapFn<T, V>) => void;
77
+ readonly refetch: (variables?: Partial<V> | undefined) => Promise<_apollo_client.ApolloQueryResult<T>>;
78
+ readonly reobserve: (newOptions?: Partial<_apollo_client.WatchQueryOptions<V, T>> | undefined, newNetworkStatus?: _apollo_client.NetworkStatus) => Promise<_apollo_client.ApolloQueryResult<T>>;
79
+ readonly variables: V | undefined;
80
+ readonly fetchMore: <TFetchData = T, TFetchVars extends OperationVariables = V>(fetchMoreOptions: _apollo_client.FetchMoreQueryOptions<TFetchVars, TFetchData> & {
81
+ updateQuery?: ((previousQueryResult: _apollo_client.Unmasked<T>, options: {
82
+ fetchMoreResult: _apollo_client.Unmasked<TFetchData>;
83
+ variables: TFetchVars;
84
+ }) => _apollo_client.Unmasked<T>) | undefined;
85
+ }) => Promise<_apollo_client.ApolloQueryResult<TFetchData>>;
86
+ }];
62
87
  /**
63
88
  * Hook to run a GraphQL mutation with the current user's token. Returns a function you call with the mutation input.
64
89
  *
@@ -94,6 +119,14 @@ declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocum
94
119
  *
95
120
  * @category GraphQL
96
121
  */
97
- declare function useAuthMutation<T, V extends OperationVariables>(mutation: TypedDocumentNode<T, V>, options?: MutationHookOptions<T, V>): readonly [(input?: V["input"]) => Promise<_apollo_client.FetchResult<T>>, _apollo_client.MutationResult<T>];
122
+ declare function useAuthMutation<T, V extends OperationVariables>(mutation: TypedDocumentNode<T, V>, options?: MutationHookOptions<T, V>): readonly [(input?: V["input"]) => Promise<_apollo_client.FetchResult<T>>, {
123
+ readonly hasAuth: false;
124
+ readonly data?: T | null | undefined;
125
+ readonly error?: _apollo_client.ApolloError;
126
+ readonly loading: boolean;
127
+ readonly called: boolean;
128
+ readonly client: ApolloClient<object>;
129
+ readonly reset: () => void;
130
+ }];
98
131
 
99
132
  export { ApolloWrapper, tokenContext, useAuthMutation, useAuthQuery };
@@ -13,7 +13,7 @@ import { useToken } from "naystack/auth/email/client";
13
13
  import React, {
14
14
  useCallback,
15
15
  useEffect,
16
- useState
16
+ useRef
17
17
  } from "react";
18
18
 
19
19
  // src/env.ts
@@ -93,41 +93,42 @@ var tokenContext = (token) => {
93
93
  function useAuthQuery(query, variables) {
94
94
  const token = useToken();
95
95
  const [fetch, result] = useLazyQuery(query);
96
- const [calledVars, setCalledVars] = useState();
96
+ const prevVarsRef = useRef(null);
97
97
  useEffect(() => {
98
- if (token && variables && calledVars !== JSON.stringify(variables)) {
99
- setCalledVars(JSON.stringify(variables));
98
+ const serialized = JSON.stringify(variables);
99
+ if (token && variables && prevVarsRef.current !== serialized) {
100
+ prevVarsRef.current = serialized;
100
101
  void fetch({
101
102
  // @ts-expect-error -- to allow dynamic props
102
- variables: { input },
103
+ variables: { input: variables },
103
104
  context: tokenContext(token),
104
105
  fetchPolicy: "no-cache"
105
106
  });
106
107
  }
107
- }, [fetch, token, variables, calledVars]);
108
+ }, [fetch, token, variables]);
108
109
  const reFetch = useCallback(
109
- (input2) => fetch({
110
+ (input) => fetch({
110
111
  // @ts-expect-error -- to allow dynamic props
111
- variables: { input: input2 },
112
+ variables: { input },
112
113
  context: tokenContext(token),
113
114
  fetchPolicy: "no-cache"
114
115
  }),
115
116
  [fetch, token]
116
117
  );
117
- return [reFetch, result];
118
+ return [reFetch, { ...result, hasAuth: !!token }];
118
119
  }
119
120
  function useAuthMutation(mutation, options) {
120
121
  const token = useToken();
121
122
  const [mutate, result] = useMutation(mutation, options);
122
123
  const method = useCallback(
123
- (input2) => mutate({
124
+ (input) => mutate({
124
125
  // @ts-expect-error -- to allow dynamic props
125
- variables: { input: input2 },
126
+ variables: { input },
126
127
  context: tokenContext(token)
127
128
  }),
128
129
  [token]
129
130
  );
130
- return [method, result];
131
+ return [method, { ...result, hasAuth: !!token }];
131
132
  }
132
133
  export {
133
134
  ApolloWrapper,
@@ -880,6 +880,7 @@ function FieldLibrary(type, queries) {
880
880
  if (!def) continue;
881
881
  Object.defineProperty(GeneratedResolver.prototype, key, {
882
882
  value: async function(root, ctx, input) {
883
+ if (root[key]) return root[key];
883
884
  return def.fn(root, ctx, input);
884
885
  },
885
886
  writable: false
@@ -1,7 +1,7 @@
1
1
  export { GQLError } from './errors.mjs';
2
2
  export { initGraphQLServer } from './init.mjs';
3
3
  export { AuthorizedContext, Context } from './types.mjs';
4
- export { FieldLibrary, FieldResponseType, QueryLibrary, QueryResponseType, field, query } from './utils.mjs';
4
+ export { FieldLibrary, FieldResolverDefinition, FieldResponseType, QueryDefinition, QueryLibrary, QueryResponseType, field, query } from './utils.mjs';
5
5
  import 'graphql/error';
6
6
  import '@apollo/server';
7
7
  import 'next/server';
@@ -1,7 +1,7 @@
1
1
  export { GQLError } from './errors.js';
2
2
  export { initGraphQLServer } from './init.js';
3
3
  export { AuthorizedContext, Context } from './types.js';
4
- export { FieldLibrary, FieldResponseType, QueryLibrary, QueryResponseType, field, query } from './utils.js';
4
+ export { FieldLibrary, FieldResolverDefinition, FieldResponseType, QueryDefinition, QueryLibrary, QueryResponseType, field, query } from './utils.js';
5
5
  import 'graphql/error';
6
6
  import '@apollo/server';
7
7
  import 'next/server';
@@ -876,6 +876,7 @@ function FieldLibrary(type, queries) {
876
876
  if (!def) continue;
877
877
  Object.defineProperty(GeneratedResolver.prototype, key, {
878
878
  value: async function(root, ctx, input) {
879
+ if (root[key]) return root[key];
879
880
  return def.fn(root, ctx, input);
880
881
  },
881
882
  writable: false
@@ -237,6 +237,7 @@ function FieldLibrary(type, queries) {
237
237
  if (!def) continue;
238
238
  Object.defineProperty(GeneratedResolver.prototype, key, {
239
239
  value: async function(root, ctx, input) {
240
+ if (root[key]) return root[key];
240
241
  return def.fn(root, ctx, input);
241
242
  },
242
243
  writable: false
@@ -264,4 +264,4 @@ type QueryResponseType<T extends QueryDefinition<any, any, any, any, any, any>>
264
264
  */
265
265
  type FieldResponseType<T extends FieldResolverDefinition<any, any, any, any, any, any, any>> = Awaited<ReturnType<T["call"]>>;
266
266
 
267
- export { FieldLibrary, type FieldResponseType, QueryLibrary, type QueryResponseType, field, query };
267
+ export { FieldLibrary, type FieldResolverDefinition, type FieldResponseType, type QueryDefinition, QueryLibrary, type QueryResponseType, field, query };
@@ -264,4 +264,4 @@ type QueryResponseType<T extends QueryDefinition<any, any, any, any, any, any>>
264
264
  */
265
265
  type FieldResponseType<T extends FieldResolverDefinition<any, any, any, any, any, any, any>> = Awaited<ReturnType<T["call"]>>;
266
266
 
267
- export { FieldLibrary, type FieldResponseType, QueryLibrary, type QueryResponseType, field, query };
267
+ export { FieldLibrary, type FieldResolverDefinition, type FieldResponseType, type QueryDefinition, QueryLibrary, type QueryResponseType, field, query };
@@ -222,6 +222,7 @@ function FieldLibrary(type, queries) {
222
222
  if (!def) continue;
223
223
  Object.defineProperty(GeneratedResolver.prototype, key, {
224
224
  value: async function(root, ctx, input) {
225
+ if (root[key]) return root[key];
225
226
  return def.fn(root, ctx, input);
226
227
  },
227
228
  writable: false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "naystack",
3
- "version": "1.5.41",
3
+ "version": "1.6.0",
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",