MCPcopy
hub / github.com/nearform/graphql-hooks / useQuery

Function useQuery

packages/graphql-hooks/src/useQuery.ts:20–132  ·  view source on GitHub ↗
(
  query: string | TypedDocumentNode<ResponseData, Variables>,
  opts: UseQueryOptions<ResponseData, Variables, TRefetchData, TRefetchVariables> = {}
)

Source from the content-addressed store, hash-verified

18}
19
20function useQuery<
21 ResponseData = any,
22 Variables = object,
23 TGraphQLError extends GraphQLResponseError = GraphQLResponseError,
24 TRefetchData = any,
25 TRefetchVariables = object
26>(
27 query: string | TypedDocumentNode<ResponseData, Variables>,
28 opts: UseQueryOptions<ResponseData, Variables, TRefetchData, TRefetchVariables> = {}
29): UseQueryResult<ResponseData, Variables, TGraphQLError> {
30 const allOpts = {
31 ...defaultOpts,
32 ...opts
33 }
34 const contextClient = React.useContext(ClientContext)
35 const client = opts.client || contextClient
36 const [calledDuringSSR, setCalledDuringSSR] = React.useState(false)
37 const [queryReq, state] = useClientRequest<
38 ResponseData,
39 Variables,
40 TGraphQLError
41 >(query, allOpts)
42
43 if (!client) {
44 throw new Error(
45 'useQuery() requires a client to be passed in the options or as a context value'
46 )
47 }
48
49 if (
50 client.ssrMode &&
51 opts.ssr !== false &&
52 !calledDuringSSR &&
53 !opts.skipCache &&
54 !opts.skip
55 ) {
56 // result may already be in the cache from previous SSR iterations
57 if (!state.data && !state.error) {
58 const p = queryReq()
59 client.ssrPromises.push(p)
60 }
61 setCalledDuringSSR(true)
62 }
63
64 const { client: clientFromOpts, ...allOptsToStringify } = allOpts
65 const stringifiedAllOpts = JSON.stringify(allOptsToStringify)
66 React.useEffect(() => {
67 if (allOpts.skip) {
68 return
69 }
70
71 queryReq()
72 }, [query, stringifiedAllOpts]) // eslint-disable-line react-hooks/exhaustive-deps
73
74 React.useEffect(() => {
75 if (state.error && allOpts.throwErrors) {
76 throw state.error
77 }

Callers 15

TestComponentFunction · 0.90
ComponentFunction · 0.90
Component1Function · 0.90
Component2Function · 0.90
Component3Function · 0.90
TestComponentFunction · 0.90
useQuery.test.tsxFile · 0.90
executeHookFunction · 0.90
MyCompAFunction · 0.90
MyCompAFunction · 0.90
MyCompAFunction · 0.90
MyCompAFunction · 0.90

Calls 3

useClientRequestFunction · 0.85
keysMethod · 0.65

Tested by 7

TestComponentFunction · 0.72
ComponentFunction · 0.72
Component1Function · 0.72
Component2Function · 0.72
Component3Function · 0.72
TestComponentFunction · 0.72
executeHookFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…