|
| 1 | +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing |
| 2 | + |
| 3 | +exports[`Connection State Hook Generator generateConnectionStateHook generates useConnectionState hook 1`] = ` |
| 4 | +"/** |
| 5 | + * WebSocket connection state hook |
| 6 | + * @generated by @constructive-io/graphql-codegen |
| 7 | + * DO NOT EDIT - changes will be overwritten |
| 8 | + */ |
| 9 | +
|
| 10 | +import { useState, useEffect } from "react"; |
| 11 | +import { getClient } from "../client"; |
| 12 | +import type { ConnectionState } from "../../orm/client"; |
| 13 | +export type { ConnectionState } from "../../orm/client"; |
| 14 | +/** |
| 15 | + * Hook to observe the WebSocket connection state. |
| 16 | + * |
| 17 | + * Returns the current connection state of the realtime WebSocket. |
| 18 | + * Returns 'disconnected' if realtime is not configured. |
| 19 | + * |
| 20 | + * @example |
| 21 | + * \`\`\`tsx |
| 22 | + * const state = useConnectionState(); |
| 23 | + * // state: 'disconnected' | 'connecting' | 'connected' | 'reconnecting' |
| 24 | + * \`\`\` |
| 25 | + */ |
| 26 | +export function useConnectionState(): ConnectionState { |
| 27 | + const [state, setState] = useState<ConnectionState>(() => getClient().getConnectionState()); |
| 28 | + useEffect(() => { |
| 29 | + const client = getClient(); |
| 30 | + if (!client.isRealtimeEnabled) return; |
| 31 | + const unsubscribe = client.onConnectionStateChange(setState); |
| 32 | + return () => unsubscribe(); |
| 33 | + }, []); |
| 34 | + return state; |
| 35 | +} |
| 36 | +" |
| 37 | +`; |
| 38 | + |
| 39 | +exports[`Subscription Barrel Generator generates barrel with subscription hooks and connection state 1`] = ` |
| 40 | +"/** |
| 41 | + * Subscription hooks barrel export |
| 42 | + * @generated by @constructive-io/graphql-codegen |
| 43 | + * DO NOT EDIT - changes will be overwritten |
| 44 | + */ |
| 45 | +export * from "./useContactSubscription"; |
| 46 | +export * from "./useProjectSubscription"; |
| 47 | +export * from "./useConnectionState";" |
| 48 | +`; |
| 49 | + |
| 50 | +exports[`Subscription Hook Generator generateSubscriptionHook generates subscription hook for Contact table 1`] = ` |
| 51 | +"/** |
| 52 | + * Subscription hook for Contact |
| 53 | + * @generated by @constructive-io/graphql-codegen |
| 54 | + * DO NOT EDIT - changes will be overwritten |
| 55 | + */ |
| 56 | +
|
| 57 | +import { useEffect, useRef, useCallback } from "react"; |
| 58 | +import { useQueryClient } from "@tanstack/react-query"; |
| 59 | +import type { QueryClient } from "@tanstack/react-query"; |
| 60 | +import { getClient } from "../client"; |
| 61 | +import type { SubscriptionEvent, SubscriptionFieldMeta, Unsubscribe } from "../../orm/client"; |
| 62 | +import type { Contact } from "../../orm/input-types"; |
| 63 | +import { contactKeys } from "../query-keys"; |
| 64 | +export type { SubscriptionEvent, Unsubscribe } from "../../orm/client"; |
| 65 | +const SUBSCRIPTION_DOCUMENT = "subscription OnContactChanged {\\n onContactChanged {\\n event\\n contact { __typename }\\n timestamp\\n }\\n}"; |
| 66 | +const FIELD_META: SubscriptionFieldMeta = { |
| 67 | + fieldName: "onContactChanged", |
| 68 | + tableName: "contact", |
| 69 | + dataFieldName: "contact" |
| 70 | +}; |
| 71 | +export interface ContactSubscriptionOptions { |
| 72 | + onEvent: (event: SubscriptionEvent<Contact>) => void; |
| 73 | + onError?: (error: Error) => void; |
| 74 | + enabled?: boolean; |
| 75 | + invalidateQueries?: boolean; |
| 76 | +} |
| 77 | +/** |
| 78 | + * Subscription hook for Contact realtime events |
| 79 | + * |
| 80 | + * Subscribes to realtime changes on the server and automatically |
| 81 | + * invalidates React Query cache when events are received. |
| 82 | + * |
| 83 | + * @example |
| 84 | + * \`\`\`tsx |
| 85 | + * useContactSubscription({ |
| 86 | + * onEvent: (event) => { |
| 87 | + * console.log(event.operation, event.data); |
| 88 | + * }, |
| 89 | + * }); |
| 90 | + * \`\`\` |
| 91 | + */ |
| 92 | +export function useContactSubscription(options: ContactSubscriptionOptions): void { |
| 93 | + const queryClient = useQueryClient(); |
| 94 | + const optionsRef = useRef(options); |
| 95 | + optionsRef.current = options; |
| 96 | + useEffect(() => { |
| 97 | + if (options.enabled === false) return; |
| 98 | + const client = getClient(); |
| 99 | + if (!client.isRealtimeEnabled) return; |
| 100 | + const unsubscribe = client.subscribe(FIELD_META, SUBSCRIPTION_DOCUMENT, {}, { |
| 101 | + onEvent: event => { |
| 102 | + optionsRef.current.onEvent(event); |
| 103 | + if (optionsRef.current.invalidateQueries !== false) queryClient.invalidateQueries({ |
| 104 | + queryKey: contactKeys.all |
| 105 | + }); |
| 106 | + }, |
| 107 | + onError: err => { |
| 108 | + optionsRef.current?.onError(err); |
| 109 | + } |
| 110 | + }); |
| 111 | + return () => unsubscribe(); |
| 112 | + }, [options.enabled, queryClient]); |
| 113 | +} |
| 114 | +" |
| 115 | +`; |
| 116 | +
|
| 117 | +exports[`Subscription Hook Generator generateSubscriptionHook generates subscription hook for Project table 1`] = ` |
| 118 | +"/** |
| 119 | + * Subscription hook for Project |
| 120 | + * @generated by @constructive-io/graphql-codegen |
| 121 | + * DO NOT EDIT - changes will be overwritten |
| 122 | + */ |
| 123 | +
|
| 124 | +import { useEffect, useRef, useCallback } from "react"; |
| 125 | +import { useQueryClient } from "@tanstack/react-query"; |
| 126 | +import type { QueryClient } from "@tanstack/react-query"; |
| 127 | +import { getClient } from "../client"; |
| 128 | +import type { SubscriptionEvent, SubscriptionFieldMeta, Unsubscribe } from "../../orm/client"; |
| 129 | +import type { Project } from "../../orm/input-types"; |
| 130 | +import { projectKeys } from "../query-keys"; |
| 131 | +export type { SubscriptionEvent, Unsubscribe } from "../../orm/client"; |
| 132 | +const SUBSCRIPTION_DOCUMENT = "subscription OnProjectChanged {\\n onProjectChanged {\\n event\\n project { __typename }\\n timestamp\\n }\\n}"; |
| 133 | +const FIELD_META: SubscriptionFieldMeta = { |
| 134 | + fieldName: "onProjectChanged", |
| 135 | + tableName: "project", |
| 136 | + dataFieldName: "project" |
| 137 | +}; |
| 138 | +export interface ProjectSubscriptionOptions { |
| 139 | + onEvent: (event: SubscriptionEvent<Project>) => void; |
| 140 | + onError?: (error: Error) => void; |
| 141 | + enabled?: boolean; |
| 142 | + invalidateQueries?: boolean; |
| 143 | +} |
| 144 | +/** |
| 145 | + * Subscription hook for Project realtime events |
| 146 | + * |
| 147 | + * Subscribes to realtime changes on the server and automatically |
| 148 | + * invalidates React Query cache when events are received. |
| 149 | + * |
| 150 | + * @example |
| 151 | + * \`\`\`tsx |
| 152 | + * useProjectSubscription({ |
| 153 | + * onEvent: (event) => { |
| 154 | + * console.log(event.operation, event.data); |
| 155 | + * }, |
| 156 | + * }); |
| 157 | + * \`\`\` |
| 158 | + */ |
| 159 | +export function useProjectSubscription(options: ProjectSubscriptionOptions): void { |
| 160 | + const queryClient = useQueryClient(); |
| 161 | + const optionsRef = useRef(options); |
| 162 | + optionsRef.current = options; |
| 163 | + useEffect(() => { |
| 164 | + if (options.enabled === false) return; |
| 165 | + const client = getClient(); |
| 166 | + if (!client.isRealtimeEnabled) return; |
| 167 | + const unsubscribe = client.subscribe(FIELD_META, SUBSCRIPTION_DOCUMENT, {}, { |
| 168 | + onEvent: event => { |
| 169 | + optionsRef.current.onEvent(event); |
| 170 | + if (optionsRef.current.invalidateQueries !== false) queryClient.invalidateQueries({ |
| 171 | + queryKey: projectKeys.all |
| 172 | + }); |
| 173 | + }, |
| 174 | + onError: err => { |
| 175 | + optionsRef.current?.onError(err); |
| 176 | + } |
| 177 | + }); |
| 178 | + return () => unsubscribe(); |
| 179 | + }, [options.enabled, queryClient]); |
| 180 | +} |
| 181 | +" |
| 182 | +`; |
0 commit comments