(
query: string | TypedDocumentNode<ResponseData, Variables>,
opts: UseQueryOptions<ResponseData, Variables, TRefetchData, TRefetchVariables> = {}
)
| 18 | } |
| 19 | |
| 20 | function 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 | } |
searching dependent graphs…