UNPKG

graphql-hooks

Version:
2 lines (1 loc) 20.6 kB
import*as e from"react";import t,{useContext as r,useRef as n}from"react";import o from"events";const i=t.createContext(null);function s(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}i.displayName="ClientContext";const a=class{constructor({uri:e,name:t,type:r}){this.uri=e,this.name=t,this.type=r}};var c=function(e){return"undefined"!=typeof File&&e instanceof File||"undefined"!=typeof Blob&&e instanceof Blob||e instanceof a},u=s(c);const l=c;var f=function(e,t="",r=l){const n=new Map,o=new Map;return{clone:function e(t,i,s){let a=t;if(r(t)){a=null;const e=n.get(t);e?e.push(i):n.set(t,[i])}else{const r=Array.isArray(t)||"undefined"!=typeof FileList&&t instanceof FileList;if(r||t&&t.constructor===Object){const n=o.has(t);if(n?a=o.get(t):(a=r?[]:{},o.set(t,a)),!s.has(t)){const o=i?i+".":"",c=new Set(s).add(t);if(r){let r=0;for(const i of t){const t=e(i,o+r++,c);n||a.push(t)}}else for(const r in t){const i=e(t[r],o+r,c);n||(a[r]=i)}}}}return a}(e,t,new Set),files:n}},h=s(f);const p=e=>u(e)||null!==e&&"object"==typeof e&&"function"==typeof e.pipe||null!==e&&"object"==typeof e&&"function"==typeof e.stream;class d{constructor(e){0===e.length&&e.push(((e,t)=>t()));for(const t of e){if("function"!=typeof t)throw Error("GraphQLClient Middleware: middleware has to be of type `function`");this.run=(e=>(r,n)=>{e(r,(()=>{t.call(this,r,n.bind.call(n,null,r))}))})(this.run)}}run(e,t){t.apply(this,e)}}var y="OperationDefinition",b=function(e){return e[e.Const=1]="Const",e[e.Var=2]="Var",e[e.Int=3]="Int",e[e.Float=4]="Float",e[e.BlockString=5]="BlockString",e[e.String=6]="String",e[e.Enum=7]="Enum",e}(b||{}),m=function(e){return e[e.Spread=1]="Spread",e[e.Name=2]="Name",e}(m||{});function v(e,t,r){for(var n="",o=0;e.length>o;o++)o&&(n+=t),n+=r(e[o]);return n}var g="\n",E={OperationDefinition(e){var t=e.operation;return e.name&&(t+=" "+e.name.value),e.variableDefinitions&&e.variableDefinitions.length&&(e.name||(t+=" "),t+="("+v(e.variableDefinitions,", ",E.VariableDefinition)+")"),e.directives&&e.directives.length&&(t+=" "+v(e.directives," ",E.Directive)),"query"!==t?t+" "+E.SelectionSet(e.selectionSet):E.SelectionSet(e.selectionSet)},VariableDefinition(e){var t=E.Variable(e.variable)+": "+O(e.type);return e.defaultValue&&(t+=" = "+O(e.defaultValue)),e.directives&&e.directives.length&&(t+=" "+v(e.directives," ",E.Directive)),t},Field(e){var t=e.alias?e.alias.value+": "+e.name.value:e.name.value;if(e.arguments&&e.arguments.length){var r=v(e.arguments,", ",E.Argument);t+=t.length+r.length+2>80?"("+(g+=" ")+v(e.arguments,g,E.Argument)+(g=g.slice(0,-2))+")":"("+r+")"}return e.directives&&e.directives.length&&(t+=" "+v(e.directives," ",E.Directive)),e.selectionSet&&e.selectionSet.selections.length&&(t+=" "+E.SelectionSet(e.selectionSet)),t},StringValue:e=>e.block?function(e){return'"""\n'+e.replace(/"""/g,'\\"""')+'\n"""'}(e.value).replace(/\n/g,g):function(e){return JSON.stringify(e)}(e.value),BooleanValue:e=>""+e.value,NullValue:e=>"null",IntValue:e=>e.value,FloatValue:e=>e.value,EnumValue:e=>e.value,Name:e=>e.value,Variable:e=>"$"+e.name.value,ListValue:e=>"["+v(e.values,", ",O)+"]",ObjectValue:e=>"{"+v(e.fields,", ",E.ObjectField)+"}",ObjectField:e=>e.name.value+": "+O(e.value),Document:e=>e.definitions&&e.definitions.length?v(e.definitions,"\n\n",O):"",SelectionSet:e=>"{"+(g+=" ")+v(e.selections,g,O)+(g=g.slice(0,-2))+"}",Argument:e=>e.name.value+": "+O(e.value),FragmentSpread(e){var t="..."+e.name.value;return e.directives&&e.directives.length&&(t+=" "+v(e.directives," ",E.Directive)),t},InlineFragment(e){var t="...";return e.typeCondition&&(t+=" on "+e.typeCondition.name.value),e.directives&&e.directives.length&&(t+=" "+v(e.directives," ",E.Directive)),t+" "+E.SelectionSet(e.selectionSet)},FragmentDefinition(e){var t="fragment "+e.name.value;return t+=" on "+e.typeCondition.name.value,e.directives&&e.directives.length&&(t+=" "+v(e.directives," ",E.Directive)),t+" "+E.SelectionSet(e.selectionSet)},Directive(e){var t="@"+e.name.value;return e.arguments&&e.arguments.length&&(t+="("+v(e.arguments,", ",E.Argument)+")"),t},NamedType:e=>e.name.value,ListType:e=>"["+O(e.type)+"]",NonNullType:e=>O(e.type)+"!"},O=e=>E[e.kind](e);function D(e){return"string"==typeof e?e:function(e){return g="\n",E[e.kind]?E[e.kind](e):""}(e)}var w=(e=>(e.DATA_INVALIDATED="DATA_INVALIDATED",e.DATA_UPDATED="DATA_UPDATED",e))(w||{}),C=Object.defineProperty,S=Object.defineProperties,A=Object.getOwnPropertyDescriptors,j=Object.getOwnPropertySymbols,T=Object.prototype.hasOwnProperty,P=Object.prototype.propertyIsEnumerable,L=(e,t,r)=>t in e?C(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Q=(e,t)=>{for(var r in t||(t={}))T.call(t,r)&&L(e,r,t[r]);if(j)for(var r of j(t))P.call(t,r)&&L(e,r,t[r]);return e},q=(e,t,r)=>L(e,"symbol"!=typeof t?t+"":t,r);class R{constructor(e){if(q(this,"url"),q(this,"ssrPromises"),q(this,"FormData"),q(this,"fetch"),q(this,"fetchOptions"),q(this,"logErrors"),q(this,"useGETForQueries"),q(this,"middleware"),q(this,"mutationsEmitter"),q(this,"cache"),q(this,"headers"),q(this,"ssrMode"),q(this,"subscriptionClient"),q(this,"fullWsTransport"),q(this,"onError"),!e)throw Error("GraphQLClient: config is required as first parameter");this.fullWsTransport=e.fullWsTransport,this.subscriptionClient="function"==typeof e.subscriptionClient?e.subscriptionClient():e.subscriptionClient,this.verifyConfig(e),this.cache=e.cache,this.headers=e.headers||{},this.ssrMode=e.ssrMode,this.ssrPromises=[],this.url=e.url,this.fetch=e.fetch||("undefined"!=typeof fetch&&fetch?fetch.bind(void 0):void 0),this.fetchOptions=e.fetchOptions||{},this.FormData=e.FormData||("undefined"!=typeof FormData?FormData:void 0),this.logErrors=void 0===e.logErrors||e.logErrors,this.onError=e.onError,this.useGETForQueries=!0===e.useGETForQueries,this.middleware=new d(e.middleware||[]),this.mutationsEmitter=new o}verifyConfig(e){if(!e.url){if(!this.fullWsTransport)throw Error("GraphQLClient: config.url is required");if(!this.subscriptionClient)throw Error("GraphQLClient: subscriptionClient is required")}if(e.fetch&&"function"!=typeof e.fetch)throw Error("GraphQLClient: config.fetch must be a function");if(("undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement||e.ssrMode)&&!e.fetch&&"function"!=typeof fetch)throw Error("GraphQLClient: fetch must be polyfilled or passed in new GraphQLClient({ fetch })");if(e.ssrMode&&!e.cache)throw Error("GraphQLClient: config.cache is required when in ssrMode")}setHeader(e,t){return this.headers[e]=t,this}setHeaders(e){return this.headers=e,this}removeHeader(e){return delete this.headers[e],this}logErrorResult({result:e,operation:t}){console.error("GraphQL Hooks Error"),console.groupCollapsed("---\x3e Full Error Details"),console.groupCollapsed("Operation:"),console.log(t),console.groupEnd();const r=e.error;r&&(r.fetchError&&(console.groupCollapsed("FETCH ERROR:"),console.log(r.fetchError),console.groupEnd()),r.httpError&&(console.groupCollapsed("HTTP ERROR:"),console.log(r.httpError),console.groupEnd()),r.graphQLErrors&&r.graphQLErrors.length>0&&(console.groupCollapsed("GRAPHQL ERROR:"),r.graphQLErrors.forEach((e=>console.log(e))),console.groupEnd())),console.groupEnd()}generateResult({fetchError:e,httpError:t,graphQLErrors:r,data:n,headers:o}){return!!(r&&r.length>0||e||t)?{data:n,error:{fetchError:e,httpError:t,graphQLErrors:r},headers:o}:{data:n,headers:o}}getCacheKey(e,t={}){return{operation:e,fetchOptions:Q(Q({},this.fetchOptions),t.fetchOptionsOverrides)}}getCache(e){const t=this.cache?this.cache.get(e):null;if(t)return t}saveCache(e,t){this.cache&&this.cache.set(e,t)}removeCache(e){var t;null==(t=this.cache)||t.delete(e)}getFetchOptions(e,t={}){const r=Q(Q({method:"POST",headers:Q({},this.headers)},this.fetchOptions),t);if("GET"===r.method)return r;const{clone:n,files:o}=h(e,"",p),i=JSON.stringify(n);if(o.size){if(!this.FormData)throw Error("GraphQLClient: FormData must be polyfilled or passed in new GraphQLClient({ FormData })");const e=new this.FormData;e.append("operations",i);const t={};let n=0;o.forEach((e=>{t[++n]=e})),e.append("map",JSON.stringify(t)),n=0,o.forEach(((t,r)=>{e.append(""+ ++n,r,r.name)})),r.body=e}else r.headers["Content-Type"]="application/json",r.body=i;return r}request(e,t){const r=[],n=e=>r.push(e);return new Promise(((o,i)=>this.middleware.run({operation:e,client:this,addResponseHook:n,resolve:o,reject:i},(({operation:e})=>{const n=e=>{return r.length>0?(t=r,e=>t.reduce(((e,t)=>e.then(t)),Promise.resolve(e)))(e):e;var t};return this.fullWsTransport?this.requestViaWS(e).then(n).then(o).catch(i):this.url?this.requestViaHttp(e,t).then(n).then(o).catch(i):void i(Error("GraphQLClient: config.url is required"))}))))}requestViaHttp(e,t={}){let r=this.url;const n=this.getFetchOptions(e,t.fetchOptionsOverrides);if("GET"===n.method){r=r+"?"+Object.entries(e).filter((([,e])=>!!e)).map((([e,t])=>("variables"!==e&&"extensions"!==e||(t=JSON.stringify(t)),`${e}=${encodeURIComponent(t)}`))).join("&")}return this.fetch(r,n).then((e=>e.ok?e.json().then((({errors:r,data:n})=>this.generateResult({graphQLErrors:r,data:F(t.responseReducer,n,e),headers:e.headers}))):e.text().then((t=>{const{status:r,statusText:n}=e;return this.generateResult({httpError:{status:r,statusText:n,body:t},headers:e.headers})})))).catch((e=>this.generateResult({fetchError:e}))).then((t=>(t.error&&(this.logErrors&&this.logErrorResult({result:t,operation:e}),this.onError&&this.onError({result:t,operation:e})),t)))}requestViaWS(e){return new Promise(((t,r)=>{let n;try{const o=this.createSubscription(e).subscribe({next:e=>{n=e},error:r,complete:()=>{o.unsubscribe(),t(n)}})}catch(e){r(e)}}))}createSubscription(e){if(!this.subscriptionClient)throw Error("No SubscriptionClient! Please set in the constructor.");return"function"==typeof this.subscriptionClient.subscribe?{subscribe:t=>({unsubscribe:this.subscriptionClient.subscribe(e,t)})}:this.subscriptionClient.request(e)}invalidateQuery(e){const t="string"==typeof e?{query:e}:e,r=this.getCacheKey(t);this.cache&&r&&(this.removeCache(r),this.request(t).then((e=>{this.mutationsEmitter.emit(w.DATA_INVALIDATED,e)})).catch((e=>console.error(e))))}setQueryData(e,t){const r=this.getCacheKey("string"==typeof e?{query:e}:e);if(this.cache&&r){const e=this.cache.get(r),i=(n=Q({},e),o={data:t(e.data||null)},S(n,A(o)));this.saveCache(r,i),this.mutationsEmitter.emit(w.DATA_UPDATED,i)}var n,o}}function F(e,t,r){return"function"==typeof e?e(t,r):t}var M=Object.defineProperty,k=(e,t,r)=>((e,t,r)=>t in e?M(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class V{constructor(e){k(this,"fetchError"),k(this,"httpError"),k(this,"graphQLErrors"),this.fetchError=e.fetchError,this.httpError=e.httpError,this.graphQLErrors=e.graphQLErrors}}var N=Object.defineProperty,I=Object.getOwnPropertySymbols,G=Object.prototype.hasOwnProperty,x=Object.prototype.propertyIsEnumerable,H=(e,t,r)=>t in e?N(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,_=(e,t,r)=>H(e,"symbol"!=typeof t?t+"":t,r);class U extends R{constructor(e){if(super(((e,t)=>{for(var r in t||(t={}))G.call(t,r)&&H(e,r,t[r]);if(I)for(var r of I(t))x.call(t,r)&&H(e,r,t[r]);return e})({url:"http://localhost"},e)),_(this,"localQueries"),_(this,"requestDelayMs"),this.localQueries=e.localQueries,this.requestDelayMs=e.requestDelayMs||0,!this.localQueries)throw Error("LocalGraphQLClient: `localQueries` object required in the constructor options")}verifyConfig(){}requestViaHttp(e,t={}){return(r=this.requestDelayMs,new Promise((e=>{setTimeout(e,r)}))).then((()=>{if(!e.query||!this.localQueries[e.query])throw Error("LocalGraphQLClient: no query match for: "+e.query);const r=this.localQueries[e.query](e.variables,e.operationName);return F(t.responseReducer,r,new Response)}));var r}request(e,t){return super.request(e,t).then((e=>{if(e instanceof V)return{error:e};const{data:t,errors:r}=K(e);return r&&r.length>0?{data:t,error:new V({graphQLErrors:r})}:{data:t}}))}}function K(e){return e instanceof Error?{data:null,errors:[e]}:Array.isArray(e)?function(e){const t=Array(e.length),r=[];for(const[n,o]of e.entries()){const e=K(o);t[n]=e.data,null!=e.errors&&r.push(...e.errors)}return{data:t,errors:r}}(e):function(e){return e===Object(e)}(e)?function(e){const t={},r=[];for(const[n,o]of Object.entries(e)){const e=K(o);t[n]=e.data,null!=e.errors&&r.push(...e.errors)}return{data:t,errors:r}}(e):{data:e,errors:null}}var B=Object.prototype.hasOwnProperty;function J(e,t,r){for(r of e.keys())if(W(r,t))return r}function W(e,t){var r,n,o;if(e===t)return!0;if(e&&t&&(r=e.constructor)===t.constructor){if(r===Date)return e.getTime()===t.getTime();if(r===RegExp)return""+e==""+t;if(r===Array){if((n=e.length)===t.length)for(;n--&&W(e[n],t[n]););return-1===n}if(r===Set){if(e.size!==t.size)return!1;for(n of e){if((o=n)&&"object"==typeof o&&!(o=J(t,o)))return!1;if(!t.has(o))return!1}return!0}if(r===Map){if(e.size!==t.size)return!1;for(n of e){if((o=n[0])&&"object"==typeof o&&!(o=J(t,o)))return!1;if(!W(n[1],t.get(o)))return!1}return!0}if(r===ArrayBuffer)e=new Uint8Array(e),t=new Uint8Array(t);else if(r===DataView){if((n=e.byteLength)===t.byteLength)for(;n--&&e.getInt8(n)===t.getInt8(n););return-1===n}if(ArrayBuffer.isView(e)){if((n=e.byteLength)===t.byteLength)for(;n--&&e[n]===t[n];);return-1===n}if(!r||"object"==typeof e){for(r in n=0,e){if(B.call(e,r)&&++n&&!B.call(t,r))return!1;if(!(r in t)||!W(e[r],t[r]))return!1}return Object.keys(t).length===n}}return e!=e&&t!=t}function z(t){var r=e.useRef(t),n=e.useRef(0);return W(t,r.current)||(r.current=t,n.current+=1),e.useMemo((function(){return r.current}),[n.current])}var $=Object.defineProperty,X=Object.defineProperties,Y=Object.getOwnPropertyDescriptors,Z=Object.getOwnPropertySymbols,ee=Object.prototype.hasOwnProperty,te=Object.prototype.propertyIsEnumerable,re=(e,t,r)=>t in e?$(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,ne=(e,t)=>{for(var r in t||(t={}))ee.call(t,r)&&re(e,r,t[r]);if(Z)for(var r of Z(t))te.call(t,r)&&re(e,r,t[r]);return e},oe=(e,t)=>X(e,Y(t));const ie="RESET_STATE",se="LOADING",ae="CACHE_HIT",ce="REQUEST_RESULT",ue="DATA_UPDATED";function le(e,t){switch(t.type){case ie:return e.loading?e:t.initialState;case se:return e.error?oe(ne({},t.initialState),{data:e.data,loading:!0}):e.loading?e:oe(ne({},e),{loading:!0});case ue:return oe(ne({},e),{data:t.result.data});case ae:return e.cacheHit&&!t.resetState?e:oe(ne({},t.result),{cacheHit:!0,loading:!1});case ce:return oe(ne({},t.result),{data:e.data&&t.result.data&&t.updateData?t.updateData(e.data,t.result.data):t.result.data,cacheHit:!1,loading:!1});default:return e}}function fe(e,r={}){var n;const o=D(e),s=null!=(n=r.operationName)?n:function(e){var t,r;let n;if("string"!=typeof e){const o=e.definitions.filter((e=>e.kind===y));1===o.length&&(n=null==(r=null==(t=o[0])?void 0:t.name)?void 0:r.value)}return n}(e),a=t.useContext(i),c=r.client||a;if(null==c)throw Error("A client must be provided in order to use the useClientRequest hook.");const u=t.useRef(!0),l=t.useRef(null),f={query:o,variables:r.variables,operationName:s,persisted:r.persisted};(r.persisted||c.useGETForQueries&&!r.isMutation)&&(r.fetchOptionsOverrides=oe(ne({},r.fetchOptionsOverrides),{method:"GET"}));const h=c.getCacheKey(f,r),p=r.isMutation||r.isManual||r.skip,d=!r.skipCache&&c.cache&&h?c.cache.get(h):null,b=oe(ne({},d),{cacheHit:!!d,loading:!p&&!d}),[m,v]=t.useReducer(le,b),g=JSON.stringify(h);t.useEffect((()=>{r.updateData||v({type:ie,initialState:b})}),[g]),t.useEffect((()=>(u.current=!0,()=>{u.current=!1})),[]);const E=t.useCallback((e=>{const t=ne(ne({},r),e),n=oe(ne({},f),{variables:t.variables,operationName:t.operationName});if(!u.current)return Promise.resolve({error:{fetchError:Error("fetchData should not be called after hook unmounted")},loading:!1,cacheHit:!1});const i=c.getCacheKey(n,t);l.current=i;const s=t.skipCache?null:c.getCache(i);return s?(v({type:ae,result:s,resetState:g!==JSON.stringify(m.cacheKey)}),Promise.resolve(s)):(v({type:se,initialState:b}),c.request(n,t).then((e=>{if(t.updateData&&"function"!=typeof t.updateData)throw Error("options.updateData must be a function");const s=ne({},e);if(t.useCache&&(s.useCache=!0,s.cacheKey=i,c.ssrMode)){const e={error:s.error,data:t.updateData?t.updateData(m.data,s.data):s.data};c.saveCache(i,e)}if(u.current&&i===l.current&&v({type:ce,updateData:t.updateData,result:s}),r.isMutation&&c.mutationsEmitter.emit(o,oe(ne({},n),{mutation:o,result:s})),!(null==e?void 0:e.error)&&t.onSuccess){if("function"!=typeof t.onSuccess)throw Error("options.onSuccess must be a function");t.onSuccess(e,n.variables)}return e})))}),z([c,r,f]));t.useEffect((()=>{m.useCache&&!c.ssrMode&&c.saveCache(m.cacheKey,m)}),[c,m]);return t.useEffect((()=>{const e=(e,t)=>{v({type:t,result:e})},t=t=>e(t,ce),r=t=>e(t,ue),n=c.mutationsEmitter;return n.on(w.DATA_INVALIDATED,t),n.on(w.DATA_UPDATED,r),()=>{n&&(n.removeListener(w.DATA_INVALIDATED,t),n.removeListener(w.DATA_UPDATED,r))}}),[]),[E,m,(e={})=>v({type:ie,initialState:ne(ne({},b),e)})]}var he=Object.defineProperty,pe=Object.defineProperties,de=Object.getOwnPropertyDescriptors,ye=Object.getOwnPropertySymbols,be=Object.prototype.hasOwnProperty,me=Object.prototype.propertyIsEnumerable,ve=(e,t,r)=>t in e?he(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,ge=(e,t)=>{for(var r in t||(t={}))be.call(t,r)&&ve(e,r,t[r]);if(ye)for(var r of ye(t))me.call(t,r)&&ve(e,r,t[r]);return e};const Ee={useCache:!0,skip:!1,throwErrors:!1};function Oe(e,r={}){const n=ge(ge({},Ee),r),o=t.useContext(i),s=r.client||o,[a,c]=t.useState(!1),[u,l]=fe(e,n);if(!s)throw Error("useQuery() requires a client to be passed in the options or as a context value");if(s.ssrMode&&!1!==r.ssr&&!a&&!r.skipCache&&!r.skip){if(!l.data&&!l.error){const e=u();s.ssrPromises.push(e)}c(!0)}const f=((e,t)=>{var r={};for(var n in e)be.call(e,n)&&0>t.indexOf(n)&&(r[n]=e[n]);if(null!=e&&ye)for(var n of ye(e))0>t.indexOf(n)&&me.call(e,n)&&(r[n]=e[n]);return r})(n,["client"]),h=JSON.stringify(f);t.useEffect((()=>{n.skip||u()}),[e,h]),t.useEffect((()=>{if(l.error&&n.throwErrors)throw l.error}),[l.error,n.throwErrors]);const p=t.useCallback(((e={})=>u(ge({skipCache:!0,updateData:(e,t)=>t},e))),[u]);return t.useEffect((function(){const e=function(e){if(!e)return{};const t={};return(Array.isArray(e)?e:[e]).forEach((e=>{var r;if(null!=e)if("string"==typeof e)t[e]={};else if("object"==typeof(r=e)&&null!=r&&"mutation"in r){const{filter:r,mutation:n,refetchOnMutationError:o=!0}=e;t[n]={filter:r,refetchOnMutationError:o}}else(function(e){return"object"==typeof e&&null!=e&&"kind"in e})(e)&&(t[D(e)]={})})),t}(r.refetchAfterMutations),t=Object.keys(e),n=({mutation:t,variables:r,result:n})=>{const{filter:o,refetchOnMutationError:i}=e[t];(!o||r&&o(r))&&(i||!n.error)&&p()};return t.forEach((e=>{s.mutationsEmitter.on(e,n)})),()=>{t.forEach((e=>{s.mutationsEmitter.removeListener(e,n)}))}}),[r.refetchAfterMutations,p,s.mutationsEmitter]),d=ge({},l),pe(d,de({refetch:p}));var d}function De(){return r(i)}function we(t,o){const s=n(o);s.current=o;const a=r(i),c=t.client||a;if(!c)throw Error("useSubscription() requires a client to be passed in the options or as a context value");!function(t,r){e.useEffect(t,z(r))}((()=>{if(t.skip)return;const e=c.createSubscription({query:t.query,variables:t.variables}).subscribe({next:e=>{s.current(e)},error:e=>{s.current({errors:e})},complete:()=>{e.unsubscribe()}});return()=>{e.unsubscribe()}}),[t.query,t.variables,t.skip])}var Ce=Object.defineProperty,Se=Object.getOwnPropertySymbols,Ae=Object.prototype.hasOwnProperty,je=Object.prototype.propertyIsEnumerable,Te=(e,t,r)=>t in e?Ce(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;const Pe=(e,t={})=>fe(e,((e,t)=>{for(var r in t||(t={}))Ae.call(t,r)&&Te(e,r,t[r]);if(Se)for(var r of Se(t))je.call(t,r)&&Te(e,r,t[r]);return e})({isMutation:!0},t));var Le=Object.defineProperty,Qe=Object.getOwnPropertySymbols,qe=Object.prototype.hasOwnProperty,Re=Object.prototype.propertyIsEnumerable,Fe=(e,t,r)=>t in e?Le(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;const Me=(e,t={})=>fe(e,((e,t)=>{for(var r in t||(t={}))qe.call(t,r)&&Fe(e,r,t[r]);if(Qe)for(var r of Qe(t))Re.call(t,r)&&Fe(e,r,t[r]);return e})({useCache:!0,isManual:!0},t));export{i as ClientContext,R as GraphQLClient,U as LocalGraphQLClient,V as LocalGraphQLError,fe as useClientRequest,Me as useManualQuery,Pe as useMutation,Oe as useQuery,De as useQueryClient,we as useSubscription};