MCPcopy Create free account
hub / github.com/artsy/force / useClientQuery

Function useClientQuery

src/Utils/Hooks/useClientQuery.ts:24–172  ·  view source on GitHub ↗
({
  environment,
  query,
  variables = {},
  cacheConfig = {},
  skip = false,
}: {
  environment?: Environment
  query: GraphQLTaggedNode
  variables?: T["variables"]
  cacheConfig?: {
    networkCacheConfig?: CacheConfig | null | undefined
    fetchPolicy?: FetchQueryFetchPolicy | null | undefined
  } | null
  skip?: boolean
})

Source from the content-addressed store, hash-verified

22}
23
24export const useClientQuery = <T extends OperationType>({
25 environment,
26 query,
27 variables = {},
28 cacheConfig = {},
29 skip = false,
30}: {
31 environment?: Environment
32 query: GraphQLTaggedNode
33 variables?: T["variables"]
34 cacheConfig?: {
35 networkCacheConfig?: CacheConfig | null | undefined
36 fetchPolicy?: FetchQueryFetchPolicy | null | undefined
37 } | null
38 skip?: boolean
39}) => {
40 const { relayEnvironment } = useSystemContext()
41
42 const [data, setData] = useState<T["response"] | null>(null)
43 const [error, setError] = useState<Error | null>(null)
44 const [loading, setLoading] = useState(true)
45
46 const key = useRef(JSON.stringify(variables))
47 const prevKey = useRef(key.current)
48 const retainedRef = useRef<Disposable | null>(null)
49
50 useUpdateEffect(() => {
51 key.current = JSON.stringify(variables)
52 }, [variables])
53
54 // Clean up retained operations on unmount
55 useEffect(() => {
56 return () => {
57 if (retainedRef.current) {
58 retainedRef.current.dispose()
59 retainedRef.current = null
60 }
61 }
62 }, [])
63
64 // refetch returns an object with a `promise` that resolves when the network
65 // response arrives and a `disposable` with a `dispose()` method that can be
66 // used to cancel the in-flight subscription where supported.
67 const refetch = (
68 newVariables = variables,
69 ): RefetchResult<T["response"] | null> => {
70 setLoading(true)
71
72 const env = (environment || relayEnvironment) as unknown as Environment
73
74 // Validate environment
75 if (!env) {
76 const error = new Error("No Relay environment available")
77 setError(error)
78 setLoading(false)
79 const rejectedPromise = Promise.reject(error).catch(() => null)
80 return { promise: rejectedPromise, disposable: { dispose: () => {} } }
81 }

Callers 9

useUserLocationFunction · 0.90
SearchBarInputFunction · 0.90
NotificationsPillsFunction · 0.90
useCountryCodeFunction · 0.90
useArtworkArticlesFunction · 0.90
SearchRouteFunction · 0.90

Calls 2

useSystemContextFunction · 0.90
refetchFunction · 0.70

Tested by

no test coverage detected