(
query: string | TypedDocumentNode<ResponseData, Variables>,
initialOpts: UseClientRequestOptions<ResponseData, Variables> = {}
)
| 86 | opts.skipCache: Boolean |
| 87 | */ |
| 88 | function useClientRequest< |
| 89 | ResponseData = any, |
| 90 | Variables = object, |
| 91 | TGraphQLError extends GraphQLResponseError = GraphQLResponseError |
| 92 | >( |
| 93 | query: string | TypedDocumentNode<ResponseData, Variables>, |
| 94 | initialOpts: UseClientRequestOptions<ResponseData, Variables> = {} |
| 95 | ): [ |
| 96 | FetchData<ResponseData, Variables, TGraphQLError>, |
| 97 | UseClientRequestResult<ResponseData, TGraphQLError>, |
| 98 | ResetFunction |
| 99 | ] { |
| 100 | const queryString = stringifyDocumentNode(query) |
| 101 | const operationName = initialOpts.operationName ?? extractOperationName(query) |
| 102 | |
| 103 | const contextClient = React.useContext(ClientContext) |
| 104 | const client = initialOpts.client || contextClient |
| 105 | |
| 106 | if (client === null || client === undefined) { |
| 107 | throw new Error( |
| 108 | 'A client must be provided in order to use the useClientRequest hook.' |
| 109 | ) |
| 110 | } |
| 111 | |
| 112 | const isMounted = React.useRef(true) |
| 113 | const activeCacheKey = React.useRef<CacheKeyObject | null>(null) |
| 114 | const operation = { |
| 115 | query: queryString, |
| 116 | variables: initialOpts.variables, |
| 117 | operationName, |
| 118 | persisted: initialOpts.persisted |
| 119 | } as any |
| 120 | |
| 121 | if ( |
| 122 | initialOpts.persisted || |
| 123 | (client.useGETForQueries && !initialOpts.isMutation) |
| 124 | ) { |
| 125 | initialOpts.fetchOptionsOverrides = { |
| 126 | ...initialOpts.fetchOptionsOverrides, |
| 127 | method: 'GET' |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | const cacheKey = client.getCacheKey(operation, initialOpts) |
| 132 | const isDeferred = |
| 133 | initialOpts.isMutation || initialOpts.isManual || initialOpts.skip |
| 134 | const initialCacheHit = |
| 135 | initialOpts.skipCache || !client.cache || !cacheKey |
| 136 | ? null |
| 137 | : client.cache.get(cacheKey) |
| 138 | const initialState = { |
| 139 | ...initialCacheHit, |
| 140 | cacheHit: !!initialCacheHit, |
| 141 | loading: isDeferred ? false : !initialCacheHit |
| 142 | } |
| 143 | const [state, dispatch] = React.useReducer(reducer, initialState) |
| 144 | |
| 145 | // NOTE: state from useReducer is only initialState on the first render |
searching dependent graphs…