graphql-hooks
Advanced tools
Comparing version 4.2.0 to 4.3.0
@@ -6,2 +6,14 @@ # Change Log | ||
# [4.3.0](https://github.com/nearform/graphql-hooks/compare/graphql-hooks@4.2.0...graphql-hooks@4.3.0) (2019-12-12) | ||
### Features | ||
* separate onError and logErrors config options ([#419](https://github.com/nearform/graphql-hooks/issues/419)) ([b125275](https://github.com/nearform/graphql-hooks/commit/b125275de6a5804b73907c53152d20d97207a94e)) | ||
* support file upload in NodeJS ([#422](https://github.com/nearform/graphql-hooks/issues/422)) ([5f1174e](https://github.com/nearform/graphql-hooks/commit/5f1174e08526ca10d4d3acaff20c73674e7c4a2d)) | ||
# [4.2.0](https://github.com/nearform/graphql-hooks/compare/graphql-hooks@4.1.2...graphql-hooks@4.2.0) (2019-12-02) | ||
@@ -8,0 +20,0 @@ |
@@ -70,3 +70,11 @@ (function (global, factory) { | ||
function extractFiles(value, path) { | ||
var isExtractableFile = function isExtractableFile(value) { | ||
return ( | ||
(typeof File !== 'undefined' && value instanceof File) || | ||
(typeof Blob !== 'undefined' && value instanceof Blob) || | ||
value instanceof ReactNativeFile | ||
) | ||
}; | ||
function extractFiles(value, path, isExtractableFile$1) { | ||
if (path === void 0) { | ||
@@ -76,2 +84,6 @@ path = ''; | ||
if (isExtractableFile$1 === void 0) { | ||
isExtractableFile$1 = isExtractableFile; | ||
} | ||
var clone; | ||
@@ -86,7 +98,3 @@ var files = new Map(); | ||
if ( | ||
(typeof File !== 'undefined' && value instanceof File) || | ||
(typeof Blob !== 'undefined' && value instanceof Blob) || | ||
value instanceof ReactNativeFile | ||
) { | ||
if (isExtractableFile$1(value)) { | ||
clone = null; | ||
@@ -103,3 +111,3 @@ addFile([path], value); | ||
clone = value.map(function(child, i) { | ||
var result = extractFiles(child, '' + prefix + i); | ||
var result = extractFiles(child, '' + prefix + i, isExtractableFile$1); | ||
result.files.forEach(addFile); | ||
@@ -112,3 +120,3 @@ return result.clone | ||
for (var i in value) { | ||
var result = extractFiles(value[i], '' + prefix + i); | ||
var result = extractFiles(value[i], '' + prefix + i, isExtractableFile$1); | ||
result.files.forEach(addFile); | ||
@@ -126,2 +134,8 @@ clone[i] = result.clone; | ||
var isExtractableFileEnhanced = function isExtractableFileEnhanced(value) { | ||
return isExtractableFile(value) || // Check if stream | ||
// https://github.com/sindresorhus/is-stream/blob/3750505b0727f6df54324784fe369365ef78841e/index.js#L3 | ||
value !== null && typeof value === 'object' && typeof value.pipe === 'function'; | ||
}; | ||
var GraphQLClient = | ||
@@ -159,2 +173,3 @@ /*#__PURE__*/ | ||
this.fetchOptions = config.fetchOptions || {}; | ||
this.FormData = config.FormData || (typeof FormData !== 'undefined' ? FormData : undefined); | ||
this.logErrors = config.logErrors !== undefined ? config.logErrors : true; | ||
@@ -188,10 +203,2 @@ this.onError = config.onError; | ||
operation = _ref.operation; | ||
if (this.onError) { | ||
return this.onError({ | ||
result: result, | ||
operation: operation | ||
}); | ||
} | ||
console.error('GraphQL Hooks Error'); | ||
@@ -204,20 +211,22 @@ console.groupCollapsed('---> Full Error Details'); | ||
if (error.fetchError) { | ||
console.groupCollapsed('FETCH ERROR:'); | ||
console.log(error.fetchError); | ||
console.groupEnd(); | ||
} | ||
if (error) { | ||
if (error.fetchError) { | ||
console.groupCollapsed('FETCH ERROR:'); | ||
console.log(error.fetchError); | ||
console.groupEnd(); | ||
} | ||
if (error.httpError) { | ||
console.groupCollapsed('HTTP ERROR:'); | ||
console.log(error.httpError); | ||
console.groupEnd(); | ||
} | ||
if (error.httpError) { | ||
console.groupCollapsed('HTTP ERROR:'); | ||
console.log(error.httpError); | ||
console.groupEnd(); | ||
} | ||
if (error.graphQLErrors && error.graphQLErrors.length > 0) { | ||
console.groupCollapsed('GRAPHQL ERROR:'); | ||
error.graphQLErrors.forEach(function (err) { | ||
return console.log(err); | ||
}); | ||
console.groupEnd(); | ||
if (error.graphQLErrors && error.graphQLErrors.length > 0) { | ||
console.groupCollapsed('GRAPHQL ERROR:'); | ||
error.graphQLErrors.forEach(function (err) { | ||
return console.log(err); | ||
}); | ||
console.groupEnd(); | ||
} | ||
} | ||
@@ -291,3 +300,3 @@ | ||
var _extractFiles = extractFiles(operation), | ||
var _extractFiles = extractFiles(operation, '', isExtractableFileEnhanced), | ||
clone = _extractFiles.clone, | ||
@@ -301,3 +310,7 @@ files = _extractFiles.files; | ||
// https://github.com/jaydenseric/graphql-multipart-request-spec | ||
var form = new FormData(); | ||
if (!this.FormData) { | ||
throw new Error('GraphQLClient: FormData must be polyfilled or passed in new GraphQLClient({ FormData })'); | ||
} | ||
var form = new this.FormData(); | ||
form.append('operations', operationJSON); | ||
@@ -378,7 +391,16 @@ var map = {}; | ||
}).then(function (result) { | ||
if (result.error && _this.logErrors) { | ||
_this.logErrorResult({ | ||
result: result, | ||
operation: operation | ||
}); | ||
if (result.error) { | ||
if (_this.logErrors) { | ||
_this.logErrorResult({ | ||
result: result, | ||
operation: operation | ||
}); | ||
} | ||
if (_this.onError) { | ||
_this.onError({ | ||
result: result, | ||
operation: operation | ||
}); | ||
} | ||
} | ||
@@ -385,0 +407,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],r):r((e=e||self).GraphQLHooks={},e.React)}(this,(function(e,r){"use strict";var t="default"in r?r.default:r;function n(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function o(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function i(e){for(var r=1;arguments.length>r;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?o(Object(t),!0).forEach((function(r){n(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):o(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var a=t.createContext();a.displayName="ClientContext";var s=function(e){var r=e.name,t=e.type;this.uri=e.uri,this.name=r,this.type=t};var u=function(){function e(e){if(void 0===e&&(e={}),!e.url)throw Error("GraphQLClient: config.url is required");if(e.fetch&&"function"!=typeof e.fetch)throw Error("GraphQLClient: config.fetch must be a function");if(!e.fetch&&!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");this.cache=e.cache,this.headers=e.headers||{},this.ssrMode=e.ssrMode,this.ssrPromises=[],this.url=e.url,this.fetch=e.fetch||fetch.bind(),this.fetchOptions=e.fetchOptions||{},this.logErrors=void 0===e.logErrors||e.logErrors,this.onError=e.onError,this.useGETForQueries=!0===e.useGETForQueries,this.subscriptionClient=e.subscriptionClient}var r=e.prototype;return r.setHeader=function(e,r){return this.headers[e]=r,this},r.setHeaders=function(e){return this.headers=e,this},r.removeHeader=function(e){return delete this.headers[e],this},r.logErrorResult=function(e){var r=e.result,t=e.operation;if(this.onError)return this.onError({result:r,operation:t});console.error("GraphQL Hooks Error"),console.groupCollapsed("---\x3e Full Error Details"),console.groupCollapsed("Operation:"),console.log(t),console.groupEnd();var n=r.error;n.fetchError&&(console.groupCollapsed("FETCH ERROR:"),console.log(n.fetchError),console.groupEnd()),n.httpError&&(console.groupCollapsed("HTTP ERROR:"),console.log(n.httpError),console.groupEnd()),n.graphQLErrors&&n.graphQLErrors.length>0&&(console.groupCollapsed("GRAPHQL ERROR:"),n.graphQLErrors.forEach((function(e){return console.log(e)})),console.groupEnd()),console.groupEnd()},r.generateResult=function(e){var r=e.fetchError,t=e.httpError,n=e.graphQLErrors,o=e.data;return!!(n&&n.length>0||r||t)?{data:o,error:{fetchError:r,httpError:t,graphQLErrors:n}}:{data:o}},r.getCacheKey=function(e,r){return void 0===r&&(r={}),{operation:e,fetchOptions:i({},this.fetchOptions,{},r.fetchOptionsOverrides)}},r.getCache=function(e){var r=this.cache?this.cache.get(e):null;if(r)return r},r.saveCache=function(e,r){this.cache&&this.cache.set(e,r)},r.getFetchOptions=function(e,r){void 0===r&&(r={});var t=i({method:"POST",headers:i({},this.headers)},this.fetchOptions,{},r);if("GET"===t.method)return t;var n=function e(r,t){var n;void 0===t&&(t="");var o=new Map;function i(e,r){var t=o.get(r);t?t.push.apply(t,e):o.set(r,e)}if("undefined"!=typeof File&&r instanceof File||"undefined"!=typeof Blob&&r instanceof Blob||r instanceof s)n=null,i([t],r);else{var a=t?t+".":"";if("undefined"!=typeof FileList&&r instanceof FileList)n=Array.prototype.map.call(r,(function(e,r){return i([""+a+r],e),null}));else if(Array.isArray(r))n=r.map((function(r,t){var n=e(r,""+a+t);return n.files.forEach(i),n.clone}));else if(r&&r.constructor===Object)for(var u in n={},r){var c=e(r[u],""+a+u);c.files.forEach(i),n[u]=c.clone}else n=r}return{clone:n,files:o}}(e),o=n.files,a=JSON.stringify(n.clone);if(o.size){var u=new FormData;u.append("operations",a);var c={},f=0;o.forEach((function(e){c[++f]=e})),u.append("map",JSON.stringify(c)),f=0,o.forEach((function(e,r){u.append(""+ ++f,r,r.name)})),t.body=u}else t.headers["Content-Type"]="application/json",t.body=a;return t},r.request=function(e,r){var t=this;void 0===r&&(r={});var n=this.url;"GET"===this.getFetchOptions(e,r.fetchOptionsOverrides).method&&(n=n+"?"+Object.entries(e).filter((function(e){return!!e[1]})).map((function(e){var r=e[0],t=e[1];return"variables"===r&&(t=JSON.stringify(t)),r+"="+encodeURIComponent(t)})).join("&"));return this.fetch(n,this.getFetchOptions(e,r.fetchOptionsOverrides)).then((function(e){return e.ok?e.json().then((function(e){return t.generateResult({graphQLErrors:e.errors,data:e.data})})):e.text().then((function(r){return t.generateResult({httpError:{status:e.status,statusText:e.statusText,body:r}})}))})).catch((function(e){return t.generateResult({fetchError:e})})).then((function(r){return r.error&&t.logErrors&&t.logErrorResult({result:r,operation:e}),r}))},r.createSubscription=function(e){return this.subscriptionClient.request(e)},e}();var c={RESET_STATE:"RESET_STATE",LOADING:"LOADING",CACHE_HIT:"CACHE_HIT",REQUEST_RESULT:"REQUEST_RESULT"};function f(e,r){switch(r.type){case c.RESET_STATE:return r.initialState;case c.LOADING:return e.loading?e:i({},e,{loading:!0});case c.CACHE_HIT:return e.cacheHit&&!r.resetState?e:i({},r.result,{cacheHit:!0,loading:!1});case c.REQUEST_RESULT:return i({},r.result,{data:e.data&&r.result.data&&r.updateData?r.updateData(e.data,r.result.data):r.result.data,cacheHit:!1,loading:!1});default:return e}}function l(e,r){var n=t.useRef();return function e(r,t){var n,o;if(r===t)return!0;if(r&&t&&(n=r.constructor)===t.constructor){if(n===Date)return r.getTime()===t.getTime();if(n===RegExp)return""+r==""+t;if(n===Array&&(o=r.length)===t.length){for(;o--&&e(r[o],t[o]););return-1===o}if(n===Object){if(Object.keys(r).length!==Object.keys(t).length)return!1;for(o in r)if(!(o in t&&e(r[o],t[o])))return!1;return!0}}return r!=r&&t!=t}(r,n.current)||(n.current=r),t.useCallback(e,n.current)}function h(e,r){if(void 0===r&&(r={}),"string"!=typeof e)throw Error("Your query must be a string. If you are using the `gql` template literal from graphql-tag, remove it from your query.");var n=t.useContext(a),o=t.useRef(!0),s=t.useRef(null),u={query:e,variables:r.variables,operationName:r.operationName};n.useGETForQueries&&!r.isMutation&&(r.fetchOptionsOverrides=i({},r.fetchOptionsOverrides,{method:"GET"}));var h=n.getCacheKey(u,r),p=r.isMutation||r.isManual,d=r.skipCache||!n.cache?null:n.cache.get(h),E=i({},d,{cacheHit:!!d,loading:!p&&!d}),g=t.useReducer(f,E),v=g[0],y=g[1],b=JSON.stringify(h);t.useEffect((function(){r.updateData||y({type:c.RESET_STATE,initialState:E})}),[b]),t.useEffect((function(){return o.current=!0,function(){o.current=!1}}),[]);var O=l((function(e){if(!o.current)return Promise.resolve();var t=i({},r,{},e),a=i({},u,{variables:t.variables,operationName:t.operationName}),f=n.getCacheKey(a,t);s.current=f;var l=t.skipCache?null:n.getCache(f);return l?(y({type:c.CACHE_HIT,result:l,resetState:b!==JSON.stringify(v.cacheKey)}),Promise.resolve(l)):(y({type:c.LOADING}),n.request(a,t).then((function(e){if(t.updateData&&"function"!=typeof t.updateData)throw Error("options.updateData must be a function");var r=i({},e);if(t.useCache&&(r.useCache=!0,r.cacheKey=f,n.ssrMode)){var a={data:t.updateData?t.updateData(v.data,r.data):r.data};n.saveCache(f,a)}return o.current&&f===s.current&&y({type:c.REQUEST_RESULT,updateData:t.updateData,result:r}),e})))}),[n,r,u]);return t.useEffect((function(){v.useCache&&n.saveCache(v.cacheKey,v)}),[n,v]),[O,v]}var p={useCache:!0};e.ClientContext=a,e.GraphQLClient=u,e.useClientRequest=h,e.useManualQuery=function(e,r){return h(e,i({useCache:!0,isManual:!0},r))},e.useMutation=function(e,r){return h(e,i({isMutation:!0},r))},e.useQuery=function(e,r){void 0===r&&(r={});var n=i({},p,{},r),o=t.useContext(a),s=t.useState(!1),u=s[0],c=s[1],f=h(e,n),l=f[0],d=f[1];if(o.ssrMode&&!1!==r.ssr&&!u&&!r.skipCache){if(!d.data&&!d.error){var E=l();o.ssrPromises.push(E)}c(!0)}var g=JSON.stringify(n);return t.useEffect((function(){l()}),[e,g]),i({},d,{refetch:t.useCallback((function(e){return void 0===e&&(e={}),l(i({skipCache:!0,updateData:function(e,r){return r}},e))}),[l])})},e.useSubscription=function(e,t){var n=r.useRef(t);n.current=t;var o=r.useContext(a),i={query:e.query,variables:e.variables};r.useEffect((function(){var e=o.createSubscription(i).subscribe({next:function(e){n.current(e)},error:function(){e.unsubscribe()},complete:function(){e.unsubscribe()}});return function(){e.unsubscribe()}}),[])},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],r):r((e=e||self).GraphQLHooks={},e.React)}(this,(function(e,r){"use strict";var t="default"in r?r.default:r;function n(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function o(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function i(e){for(var r=1;arguments.length>r;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?o(Object(t),!0).forEach((function(r){n(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):o(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}var a=t.createContext();a.displayName="ClientContext";var s=function(e){var r=e.name,t=e.type;this.uri=e.uri,this.name=r,this.type=t},u=function(e){return"undefined"!=typeof File&&e instanceof File||"undefined"!=typeof Blob&&e instanceof Blob||e instanceof s};var c=function(e){return u(e)||null!==e&&"object"==typeof e&&"function"==typeof e.pipe},f=function(){function e(e){if(void 0===e&&(e={}),!e.url)throw Error("GraphQLClient: config.url is required");if(e.fetch&&"function"!=typeof e.fetch)throw Error("GraphQLClient: config.fetch must be a function");if(!e.fetch&&!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");this.cache=e.cache,this.headers=e.headers||{},this.ssrMode=e.ssrMode,this.ssrPromises=[],this.url=e.url,this.fetch=e.fetch||fetch.bind(),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.subscriptionClient=e.subscriptionClient}var r=e.prototype;return r.setHeader=function(e,r){return this.headers[e]=r,this},r.setHeaders=function(e){return this.headers=e,this},r.removeHeader=function(e){return delete this.headers[e],this},r.logErrorResult=function(e){var r=e.result,t=e.operation;console.error("GraphQL Hooks Error"),console.groupCollapsed("---\x3e Full Error Details"),console.groupCollapsed("Operation:"),console.log(t),console.groupEnd();var n=r.error;n&&(n.fetchError&&(console.groupCollapsed("FETCH ERROR:"),console.log(n.fetchError),console.groupEnd()),n.httpError&&(console.groupCollapsed("HTTP ERROR:"),console.log(n.httpError),console.groupEnd()),n.graphQLErrors&&n.graphQLErrors.length>0&&(console.groupCollapsed("GRAPHQL ERROR:"),n.graphQLErrors.forEach((function(e){return console.log(e)})),console.groupEnd())),console.groupEnd()},r.generateResult=function(e){var r=e.fetchError,t=e.httpError,n=e.graphQLErrors,o=e.data;return!!(n&&n.length>0||r||t)?{data:o,error:{fetchError:r,httpError:t,graphQLErrors:n}}:{data:o}},r.getCacheKey=function(e,r){return void 0===r&&(r={}),{operation:e,fetchOptions:i({},this.fetchOptions,{},r.fetchOptionsOverrides)}},r.getCache=function(e){var r=this.cache?this.cache.get(e):null;if(r)return r},r.saveCache=function(e,r){this.cache&&this.cache.set(e,r)},r.getFetchOptions=function(e,r){void 0===r&&(r={});var t=i({method:"POST",headers:i({},this.headers)},this.fetchOptions,{},r);if("GET"===t.method)return t;var n=function e(r,t,n){var o;void 0===t&&(t=""),void 0===n&&(n=u);var i=new Map;function a(e,r){var t=i.get(r);t?t.push.apply(t,e):i.set(r,e)}if(n(r))o=null,a([t],r);else{var s=t?t+".":"";if("undefined"!=typeof FileList&&r instanceof FileList)o=Array.prototype.map.call(r,(function(e,r){return a([""+s+r],e),null}));else if(Array.isArray(r))o=r.map((function(r,t){var o=e(r,""+s+t,n);return o.files.forEach(a),o.clone}));else if(r&&r.constructor===Object)for(var c in o={},r){var f=e(r[c],""+s+c,n);f.files.forEach(a),o[c]=f.clone}else o=r}return{clone:o,files:i}}(e,"",c),o=n.files,a=JSON.stringify(n.clone);if(o.size){if(!this.FormData)throw Error("GraphQLClient: FormData must be polyfilled or passed in new GraphQLClient({ FormData })");var s=new this.FormData;s.append("operations",a);var f={},l=0;o.forEach((function(e){f[++l]=e})),s.append("map",JSON.stringify(f)),l=0,o.forEach((function(e,r){s.append(""+ ++l,r,r.name)})),t.body=s}else t.headers["Content-Type"]="application/json",t.body=a;return t},r.request=function(e,r){var t=this;void 0===r&&(r={});var n=this.url;"GET"===this.getFetchOptions(e,r.fetchOptionsOverrides).method&&(n=n+"?"+Object.entries(e).filter((function(e){return!!e[1]})).map((function(e){var r=e[0],t=e[1];return"variables"===r&&(t=JSON.stringify(t)),r+"="+encodeURIComponent(t)})).join("&"));return this.fetch(n,this.getFetchOptions(e,r.fetchOptionsOverrides)).then((function(e){return e.ok?e.json().then((function(e){return t.generateResult({graphQLErrors:e.errors,data:e.data})})):e.text().then((function(r){return t.generateResult({httpError:{status:e.status,statusText:e.statusText,body:r}})}))})).catch((function(e){return t.generateResult({fetchError:e})})).then((function(r){return r.error&&(t.logErrors&&t.logErrorResult({result:r,operation:e}),t.onError&&t.onError({result:r,operation:e})),r}))},r.createSubscription=function(e){return this.subscriptionClient.request(e)},e}();var l={RESET_STATE:"RESET_STATE",LOADING:"LOADING",CACHE_HIT:"CACHE_HIT",REQUEST_RESULT:"REQUEST_RESULT"};function h(e,r){switch(r.type){case l.RESET_STATE:return r.initialState;case l.LOADING:return e.loading?e:i({},e,{loading:!0});case l.CACHE_HIT:return e.cacheHit&&!r.resetState?e:i({},r.result,{cacheHit:!0,loading:!1});case l.REQUEST_RESULT:return i({},r.result,{data:e.data&&r.result.data&&r.updateData?r.updateData(e.data,r.result.data):r.result.data,cacheHit:!1,loading:!1});default:return e}}function p(e,r){var n=t.useRef();return function e(r,t){var n,o;if(r===t)return!0;if(r&&t&&(n=r.constructor)===t.constructor){if(n===Date)return r.getTime()===t.getTime();if(n===RegExp)return""+r==""+t;if(n===Array&&(o=r.length)===t.length){for(;o--&&e(r[o],t[o]););return-1===o}if(n===Object){if(Object.keys(r).length!==Object.keys(t).length)return!1;for(o in r)if(!(o in t&&e(r[o],t[o])))return!1;return!0}}return r!=r&&t!=t}(r,n.current)||(n.current=r),t.useCallback(e,n.current)}function d(e,r){if(void 0===r&&(r={}),"string"!=typeof e)throw Error("Your query must be a string. If you are using the `gql` template literal from graphql-tag, remove it from your query.");var n=t.useContext(a),o=t.useRef(!0),s=t.useRef(null),u={query:e,variables:r.variables,operationName:r.operationName};n.useGETForQueries&&!r.isMutation&&(r.fetchOptionsOverrides=i({},r.fetchOptionsOverrides,{method:"GET"}));var c=n.getCacheKey(u,r),f=r.isMutation||r.isManual,d=r.skipCache||!n.cache?null:n.cache.get(c),E=i({},d,{cacheHit:!!d,loading:!f&&!d}),g=t.useReducer(h,E),v=g[0],y=g[1],b=JSON.stringify(c);t.useEffect((function(){r.updateData||y({type:l.RESET_STATE,initialState:E})}),[b]),t.useEffect((function(){return o.current=!0,function(){o.current=!1}}),[]);var O=p((function(e){if(!o.current)return Promise.resolve();var t=i({},r,{},e),a=i({},u,{variables:t.variables,operationName:t.operationName}),c=n.getCacheKey(a,t);s.current=c;var f=t.skipCache?null:n.getCache(c);return f?(y({type:l.CACHE_HIT,result:f,resetState:b!==JSON.stringify(v.cacheKey)}),Promise.resolve(f)):(y({type:l.LOADING}),n.request(a,t).then((function(e){if(t.updateData&&"function"!=typeof t.updateData)throw Error("options.updateData must be a function");var r=i({},e);if(t.useCache&&(r.useCache=!0,r.cacheKey=c,n.ssrMode)){var a={data:t.updateData?t.updateData(v.data,r.data):r.data};n.saveCache(c,a)}return o.current&&c===s.current&&y({type:l.REQUEST_RESULT,updateData:t.updateData,result:r}),e})))}),[n,r,u]);return t.useEffect((function(){v.useCache&&n.saveCache(v.cacheKey,v)}),[n,v]),[O,v]}var E={useCache:!0};e.ClientContext=a,e.GraphQLClient=f,e.useClientRequest=d,e.useManualQuery=function(e,r){return d(e,i({useCache:!0,isManual:!0},r))},e.useMutation=function(e,r){return d(e,i({isMutation:!0},r))},e.useQuery=function(e,r){void 0===r&&(r={});var n=i({},E,{},r),o=t.useContext(a),s=t.useState(!1),u=s[0],c=s[1],f=d(e,n),l=f[0],h=f[1];if(o.ssrMode&&!1!==r.ssr&&!u&&!r.skipCache){if(!h.data&&!h.error){var p=l();o.ssrPromises.push(p)}c(!0)}var g=JSON.stringify(n);return t.useEffect((function(){l()}),[e,g]),i({},h,{refetch:t.useCallback((function(e){return void 0===e&&(e={}),l(i({skipCache:!0,updateData:function(e,r){return r}},e))}),[l])})},e.useSubscription=function(e,t){var n=r.useRef(t);n.current=t;var o=r.useContext(a),i={query:e.query,variables:e.variables};r.useEffect((function(){var e=o.createSubscription(i).subscribe({next:function(e){n.current(e)},error:function(){e.unsubscribe()},complete:function(){e.unsubscribe()}});return function(){e.unsubscribe()}}),[])},Object.defineProperty(e,"__esModule",{value:!0})})); |
import React, { useRef, useContext, useEffect } from 'react'; | ||
import { extractFiles } from 'extract-files'; | ||
import { isExtractableFile, extractFiles } from 'extract-files'; | ||
import deepEqual from 'dequal'; | ||
@@ -57,2 +57,8 @@ | ||
var isExtractableFileEnhanced = function isExtractableFileEnhanced(value) { | ||
return isExtractableFile(value) || // Check if stream | ||
// https://github.com/sindresorhus/is-stream/blob/3750505b0727f6df54324784fe369365ef78841e/index.js#L3 | ||
value !== null && typeof value === 'object' && typeof value.pipe === 'function'; | ||
}; | ||
var GraphQLClient = | ||
@@ -90,2 +96,3 @@ /*#__PURE__*/ | ||
this.fetchOptions = config.fetchOptions || {}; | ||
this.FormData = config.FormData || (typeof FormData !== 'undefined' ? FormData : undefined); | ||
this.logErrors = config.logErrors !== undefined ? config.logErrors : true; | ||
@@ -119,10 +126,2 @@ this.onError = config.onError; | ||
operation = _ref.operation; | ||
if (this.onError) { | ||
return this.onError({ | ||
result: result, | ||
operation: operation | ||
}); | ||
} | ||
console.error('GraphQL Hooks Error'); | ||
@@ -135,20 +134,22 @@ console.groupCollapsed('---> Full Error Details'); | ||
if (error.fetchError) { | ||
console.groupCollapsed('FETCH ERROR:'); | ||
console.log(error.fetchError); | ||
console.groupEnd(); | ||
} | ||
if (error) { | ||
if (error.fetchError) { | ||
console.groupCollapsed('FETCH ERROR:'); | ||
console.log(error.fetchError); | ||
console.groupEnd(); | ||
} | ||
if (error.httpError) { | ||
console.groupCollapsed('HTTP ERROR:'); | ||
console.log(error.httpError); | ||
console.groupEnd(); | ||
} | ||
if (error.httpError) { | ||
console.groupCollapsed('HTTP ERROR:'); | ||
console.log(error.httpError); | ||
console.groupEnd(); | ||
} | ||
if (error.graphQLErrors && error.graphQLErrors.length > 0) { | ||
console.groupCollapsed('GRAPHQL ERROR:'); | ||
error.graphQLErrors.forEach(function (err) { | ||
return console.log(err); | ||
}); | ||
console.groupEnd(); | ||
if (error.graphQLErrors && error.graphQLErrors.length > 0) { | ||
console.groupCollapsed('GRAPHQL ERROR:'); | ||
error.graphQLErrors.forEach(function (err) { | ||
return console.log(err); | ||
}); | ||
console.groupEnd(); | ||
} | ||
} | ||
@@ -222,3 +223,3 @@ | ||
var _extractFiles = extractFiles(operation), | ||
var _extractFiles = extractFiles(operation, '', isExtractableFileEnhanced), | ||
clone = _extractFiles.clone, | ||
@@ -232,3 +233,7 @@ files = _extractFiles.files; | ||
// https://github.com/jaydenseric/graphql-multipart-request-spec | ||
var form = new FormData(); | ||
if (!this.FormData) { | ||
throw new Error('GraphQLClient: FormData must be polyfilled or passed in new GraphQLClient({ FormData })'); | ||
} | ||
var form = new this.FormData(); | ||
form.append('operations', operationJSON); | ||
@@ -309,7 +314,16 @@ var map = {}; | ||
}).then(function (result) { | ||
if (result.error && _this.logErrors) { | ||
_this.logErrorResult({ | ||
result: result, | ||
operation: operation | ||
}); | ||
if (result.error) { | ||
if (_this.logErrors) { | ||
_this.logErrorResult({ | ||
result: result, | ||
operation: operation | ||
}); | ||
} | ||
if (_this.onError) { | ||
_this.onError({ | ||
result: result, | ||
operation: operation | ||
}); | ||
} | ||
} | ||
@@ -316,0 +330,0 @@ |
@@ -12,2 +12,3 @@ import * as React from 'react' | ||
fetchOptions: object | ||
FormData?: any | ||
logErrors: boolean | ||
@@ -79,2 +80,3 @@ useGETForQueries: boolean | ||
fetchOptions?: object | ||
FormData?: any | ||
logErrors?: boolean | ||
@@ -81,0 +83,0 @@ onError?({ |
@@ -64,2 +64,8 @@ 'use strict'; | ||
var isExtractableFileEnhanced = function isExtractableFileEnhanced(value) { | ||
return extractFiles.isExtractableFile(value) || // Check if stream | ||
// https://github.com/sindresorhus/is-stream/blob/3750505b0727f6df54324784fe369365ef78841e/index.js#L3 | ||
value !== null && typeof value === 'object' && typeof value.pipe === 'function'; | ||
}; | ||
var GraphQLClient = | ||
@@ -97,2 +103,3 @@ /*#__PURE__*/ | ||
this.fetchOptions = config.fetchOptions || {}; | ||
this.FormData = config.FormData || (typeof FormData !== 'undefined' ? FormData : undefined); | ||
this.logErrors = config.logErrors !== undefined ? config.logErrors : true; | ||
@@ -126,10 +133,2 @@ this.onError = config.onError; | ||
operation = _ref.operation; | ||
if (this.onError) { | ||
return this.onError({ | ||
result: result, | ||
operation: operation | ||
}); | ||
} | ||
console.error('GraphQL Hooks Error'); | ||
@@ -142,20 +141,22 @@ console.groupCollapsed('---> Full Error Details'); | ||
if (error.fetchError) { | ||
console.groupCollapsed('FETCH ERROR:'); | ||
console.log(error.fetchError); | ||
console.groupEnd(); | ||
} | ||
if (error) { | ||
if (error.fetchError) { | ||
console.groupCollapsed('FETCH ERROR:'); | ||
console.log(error.fetchError); | ||
console.groupEnd(); | ||
} | ||
if (error.httpError) { | ||
console.groupCollapsed('HTTP ERROR:'); | ||
console.log(error.httpError); | ||
console.groupEnd(); | ||
} | ||
if (error.httpError) { | ||
console.groupCollapsed('HTTP ERROR:'); | ||
console.log(error.httpError); | ||
console.groupEnd(); | ||
} | ||
if (error.graphQLErrors && error.graphQLErrors.length > 0) { | ||
console.groupCollapsed('GRAPHQL ERROR:'); | ||
error.graphQLErrors.forEach(function (err) { | ||
return console.log(err); | ||
}); | ||
console.groupEnd(); | ||
if (error.graphQLErrors && error.graphQLErrors.length > 0) { | ||
console.groupCollapsed('GRAPHQL ERROR:'); | ||
error.graphQLErrors.forEach(function (err) { | ||
return console.log(err); | ||
}); | ||
console.groupEnd(); | ||
} | ||
} | ||
@@ -229,3 +230,3 @@ | ||
var _extractFiles = extractFiles.extractFiles(operation), | ||
var _extractFiles = extractFiles.extractFiles(operation, '', isExtractableFileEnhanced), | ||
clone = _extractFiles.clone, | ||
@@ -239,3 +240,7 @@ files = _extractFiles.files; | ||
// https://github.com/jaydenseric/graphql-multipart-request-spec | ||
var form = new FormData(); | ||
if (!this.FormData) { | ||
throw new Error('GraphQLClient: FormData must be polyfilled or passed in new GraphQLClient({ FormData })'); | ||
} | ||
var form = new this.FormData(); | ||
form.append('operations', operationJSON); | ||
@@ -316,7 +321,16 @@ var map = {}; | ||
}).then(function (result) { | ||
if (result.error && _this.logErrors) { | ||
_this.logErrorResult({ | ||
result: result, | ||
operation: operation | ||
}); | ||
if (result.error) { | ||
if (_this.logErrors) { | ||
_this.logErrorResult({ | ||
result: result, | ||
operation: operation | ||
}); | ||
} | ||
if (_this.onError) { | ||
_this.onError({ | ||
result: result, | ||
operation: operation | ||
}); | ||
} | ||
} | ||
@@ -323,0 +337,0 @@ |
{ | ||
"name": "graphql-hooks", | ||
"version": "4.2.0", | ||
"version": "4.3.0", | ||
"description": "Graphql Hooks", | ||
@@ -38,3 +38,3 @@ "main": "lib/graphql-hooks.js", | ||
"dequal": "^1.0.0", | ||
"extract-files": "^5.0.1" | ||
"extract-files": "^6.0.0" | ||
}, | ||
@@ -44,3 +44,3 @@ "devDependencies": { | ||
"@testing-library/react-hooks": "^3.2.1", | ||
"formdata-node": "^1.6.0", | ||
"formdata-node": "^2.0.0", | ||
"graphql-hooks-memcache": "^1.3.0", | ||
@@ -62,3 +62,3 @@ "graphql-hooks-ssr": "^1.1.5", | ||
"homepage": "https://github.com/nearform/graphql-hooks#readme", | ||
"gitHead": "ddcc5675e9863c3f0b4c9bfbec0afc3c0be4e97c" | ||
"gitHead": "b562a414d019cda4c7acd98554e99004bc52c582" | ||
} |
@@ -143,2 +143,3 @@ # graphql-hooks | ||
- `fetch(url, options)`: Fetch implementation - defaults to the global `fetch` API. Check [Request interceptors](#request-interceptors) for more details how to manage `fetch`. | ||
- `FormData`: FormData implementation - defaults to the global `FormData` API. Polyfill this in a node.js environment. See [file-uploads-nodejs](#file-uploads-nodejs) for more info. | ||
- `fetchOptions`: See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch) for info on what options can be passed | ||
@@ -552,3 +553,3 @@ - `headers`: Object, e.g. `{ 'My-Header': 'hello' }` | ||
const uploadPostPictureMutation = /* GraphQL */ ` | ||
const uploadPostPictureMutation = ` | ||
mutation UploadPostPicture($picture: Upload!) { | ||
@@ -588,2 +589,26 @@ uploadPostPicture(picture: $picture) { | ||
### File uploads Node.js | ||
```js | ||
const client = new GraphQLClient({ | ||
url: 'https://domain.com/graphql', | ||
fetch: require('node-fetch'), | ||
FormData: require('formdata-node') | ||
}) | ||
const uploadPostPictureMutation = ` | ||
mutation UploadPostPicture($picture: Upload!) { | ||
uploadPostPicture(picture: $picture) { | ||
id | ||
pictureUrl | ||
} | ||
} | ||
` | ||
const { data, error } = await client.request({ | ||
query: uploadPostPictureMutation, | ||
variables: { picture: createReadStream('some-file.txt') } | ||
}) | ||
``` | ||
## HTTP Get support | ||
@@ -835,38 +860,43 @@ | ||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> | ||
<!-- prettier-ignore --> | ||
<!-- prettier-ignore-start --> | ||
<!-- markdownlint-disable --> | ||
<table> | ||
<tr> | ||
<td align="center"><a href="https://twitter.com/bmullan91"><img src="https://avatars1.githubusercontent.com/u/1939483?v=4" width="100px;" alt="Brian Mullan"/><br /><sub><b>Brian Mullan</b></sub></a><br /><a href="#question-bmullan91" title="Answering Questions">๐ฌ</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Abmullan91" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=bmullan91" title="Code">๐ป</a> <a href="#content-bmullan91" title="Content">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=bmullan91" title="Documentation">๐</a> <a href="#example-bmullan91" title="Examples">๐ก</a> <a href="#ideas-bmullan91" title="Ideas, Planning, & Feedback">๐ค</a> <a href="#maintenance-bmullan91" title="Maintenance">๐ง</a> <a href="#review-bmullan91" title="Reviewed Pull Requests">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=bmullan91" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://jackdc.com"><img src="https://avatars0.githubusercontent.com/u/1485654?v=4" width="100px;" alt="Jack Clark"/><br /><sub><b>Jack Clark</b></sub></a><br /><a href="#question-jackdclark" title="Answering Questions">๐ฌ</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Ajackdclark" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=jackdclark" title="Code">๐ป</a> <a href="#content-jackdclark" title="Content">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=jackdclark" title="Documentation">๐</a> <a href="#example-jackdclark" title="Examples">๐ก</a> <a href="#ideas-jackdclark" title="Ideas, Planning, & Feedback">๐ค</a> <a href="#maintenance-jackdclark" title="Maintenance">๐ง</a> <a href="#review-jackdclark" title="Reviewed Pull Requests">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=jackdclark" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="http://twitter.com/joezo"><img src="https://avatars1.githubusercontent.com/u/2870255?v=4" width="100px;" alt="Joe Warren"/><br /><sub><b>Joe Warren</b></sub></a><br /><a href="#question-Joezo" title="Answering Questions">๐ฌ</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3AJoezo" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=Joezo" title="Code">๐ป</a> <a href="#content-Joezo" title="Content">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=Joezo" title="Documentation">๐</a> <a href="#example-Joezo" title="Examples">๐ก</a> <a href="#ideas-Joezo" title="Ideas, Planning, & Feedback">๐ค</a> <a href="#maintenance-Joezo" title="Maintenance">๐ง</a> <a href="#review-Joezo" title="Reviewed Pull Requests">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=Joezo" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="http://simoneb.github.io"><img src="https://avatars1.githubusercontent.com/u/20181?v=4" width="100px;" alt="Simone Busoli"/><br /><sub><b>Simone Busoli</b></sub></a><br /><a href="#question-simoneb" title="Answering Questions">๐ฌ</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Asimoneb" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=simoneb" title="Documentation">๐</a></td> | ||
<td align="center"><a href="https://jheytompkins.com"><img src="https://avatars1.githubusercontent.com/u/842246?v=4" width="100px;" alt="jhey tompkins"/><br /><sub><b>jhey tompkins</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=jh3y" title="Tests">โ ๏ธ</a> <a href="#question-jh3y" title="Answering Questions">๐ฌ</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Ajh3y" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=jh3y" title="Code">๐ป</a> <a href="#content-jh3y" title="Content">๐</a> <a href="#review-jh3y" title="Reviewed Pull Requests">๐</a></td> | ||
<td align="center"><a href="https://haroen.me"><img src="https://avatars3.githubusercontent.com/u/6270048?v=4" width="100px;" alt="Haroen Viaene"/><br /><sub><b>Haroen Viaene</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/issues?q=author%3AHaroenv" title="Bug reports">๐</a></td> | ||
<td align="center"><a href="https://github.com/aribouius"><img src="https://avatars2.githubusercontent.com/u/10748727?v=4" width="100px;" alt="Ari Bouius"/><br /><sub><b>Ari Bouius</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=aribouius" title="Documentation">๐</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Aaribouius" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=aribouius" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=aribouius" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://twitter.com/bmullan91"><img src="https://avatars1.githubusercontent.com/u/1939483?v=4" width="100px;" alt=""/><br /><sub><b>Brian Mullan</b></sub></a><br /><a href="#question-bmullan91" title="Answering Questions">๐ฌ</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Abmullan91" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=bmullan91" title="Code">๐ป</a> <a href="#content-bmullan91" title="Content">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=bmullan91" title="Documentation">๐</a> <a href="#example-bmullan91" title="Examples">๐ก</a> <a href="#ideas-bmullan91" title="Ideas, Planning, & Feedback">๐ค</a> <a href="#maintenance-bmullan91" title="Maintenance">๐ง</a> <a href="https://github.com/nearform/graphql-hooks/pulls?q=is%3Apr+reviewed-by%3Abmullan91" title="Reviewed Pull Requests">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=bmullan91" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://jackdc.com"><img src="https://avatars0.githubusercontent.com/u/1485654?v=4" width="100px;" alt=""/><br /><sub><b>Jack Clark</b></sub></a><br /><a href="#question-jackdclark" title="Answering Questions">๐ฌ</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Ajackdclark" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=jackdclark" title="Code">๐ป</a> <a href="#content-jackdclark" title="Content">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=jackdclark" title="Documentation">๐</a> <a href="#example-jackdclark" title="Examples">๐ก</a> <a href="#ideas-jackdclark" title="Ideas, Planning, & Feedback">๐ค</a> <a href="#maintenance-jackdclark" title="Maintenance">๐ง</a> <a href="https://github.com/nearform/graphql-hooks/pulls?q=is%3Apr+reviewed-by%3Ajackdclark" title="Reviewed Pull Requests">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=jackdclark" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="http://twitter.com/joezo"><img src="https://avatars1.githubusercontent.com/u/2870255?v=4" width="100px;" alt=""/><br /><sub><b>Joe Warren</b></sub></a><br /><a href="#question-Joezo" title="Answering Questions">๐ฌ</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3AJoezo" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=Joezo" title="Code">๐ป</a> <a href="#content-Joezo" title="Content">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=Joezo" title="Documentation">๐</a> <a href="#example-Joezo" title="Examples">๐ก</a> <a href="#ideas-Joezo" title="Ideas, Planning, & Feedback">๐ค</a> <a href="#maintenance-Joezo" title="Maintenance">๐ง</a> <a href="https://github.com/nearform/graphql-hooks/pulls?q=is%3Apr+reviewed-by%3AJoezo" title="Reviewed Pull Requests">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=Joezo" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="http://simoneb.github.io"><img src="https://avatars1.githubusercontent.com/u/20181?v=4" width="100px;" alt=""/><br /><sub><b>Simone Busoli</b></sub></a><br /><a href="#question-simoneb" title="Answering Questions">๐ฌ</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Asimoneb" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=simoneb" title="Documentation">๐</a></td> | ||
<td align="center"><a href="https://jheytompkins.com"><img src="https://avatars1.githubusercontent.com/u/842246?v=4" width="100px;" alt=""/><br /><sub><b>jhey tompkins</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=jh3y" title="Tests">โ ๏ธ</a> <a href="#question-jh3y" title="Answering Questions">๐ฌ</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Ajh3y" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=jh3y" title="Code">๐ป</a> <a href="#content-jh3y" title="Content">๐</a> <a href="https://github.com/nearform/graphql-hooks/pulls?q=is%3Apr+reviewed-by%3Ajh3y" title="Reviewed Pull Requests">๐</a></td> | ||
<td align="center"><a href="https://haroen.me"><img src="https://avatars3.githubusercontent.com/u/6270048?v=4" width="100px;" alt=""/><br /><sub><b>Haroen Viaene</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/issues?q=author%3AHaroenv" title="Bug reports">๐</a></td> | ||
<td align="center"><a href="https://github.com/aribouius"><img src="https://avatars2.githubusercontent.com/u/10748727?v=4" width="100px;" alt=""/><br /><sub><b>Ari Bouius</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=aribouius" title="Documentation">๐</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Aaribouius" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=aribouius" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=aribouius" title="Tests">โ ๏ธ</a></td> | ||
</tr> | ||
<tr> | ||
<td align="center"><a href="https://github.com/kkogovsek"><img src="https://avatars1.githubusercontent.com/u/8089644?v=4" width="100px;" alt="Klemen Kogovลกek"/><br /><sub><b>Klemen Kogovลกek</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Akkogovsek" title="Bug reports">๐</a> <a href="#ideas-kkogovsek" title="Ideas, Planning, & Feedback">๐ค</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=kkogovsek" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=kkogovsek" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://github.com/wescoder"><img src="https://avatars0.githubusercontent.com/u/22945955?v=4" width="100px;" alt="Wรฉsley Queiroz"/><br /><sub><b>Wรฉsley Queiroz</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Awescoder" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=wescoder" title="Code">๐ป</a></td> | ||
<td align="center"><a href="https://www.good-idea.studio"><img src="https://avatars3.githubusercontent.com/u/11514928?v=4" width="100px;" alt="Joseph Thomas"/><br /><sub><b>Joseph Thomas</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Agood-idea" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=good-idea" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=good-idea" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="http://edvinasbartkus.lt"><img src="https://avatars0.githubusercontent.com/u/202988?v=4" width="100px;" alt="Edvinas Bartkus"/><br /><sub><b>Edvinas Bartkus</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=edvinasbartkus" title="Code">๐ป</a> <a href="#question-edvinasbartkus" title="Answering Questions">๐ฌ</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Aedvinasbartkus" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=edvinasbartkus" title="Documentation">๐</a> <a href="#example-edvinasbartkus" title="Examples">๐ก</a> <a href="#ideas-edvinasbartkus" title="Ideas, Planning, & Feedback">๐ค</a> <a href="#maintenance-edvinasbartkus" title="Maintenance">๐ง</a> <a href="#review-edvinasbartkus" title="Reviewed Pull Requests">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=edvinasbartkus" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://github.com/olistic"><img src="https://avatars1.githubusercontent.com/u/5600126?v=4" width="100px;" alt="Matรญas Olivera"/><br /><sub><b>Matรญas Olivera</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Aolistic" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=olistic" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=olistic" title="Tests">โ ๏ธ</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=olistic" title="Documentation">๐</a></td> | ||
<td align="center"><a href="https://github.com/tcudok-jg"><img src="https://avatars3.githubusercontent.com/u/50208575?v=4" width="100px;" alt="tcudok-jg"/><br /><sub><b>tcudok-jg</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=tcudok-jg" title="Code">๐ป</a></td> | ||
<td align="center"><a href="https://github.com/heymartinadams"><img src="https://avatars2.githubusercontent.com/u/11673745?v=4" width="100px;" alt="Martin Adams"/><br /><sub><b>Martin Adams</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=heymartinadams" title="Documentation">๐</a></td> | ||
<td align="center"><a href="https://github.com/kkogovsek"><img src="https://avatars1.githubusercontent.com/u/8089644?v=4" width="100px;" alt=""/><br /><sub><b>Klemen Kogovลกek</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Akkogovsek" title="Bug reports">๐</a> <a href="#ideas-kkogovsek" title="Ideas, Planning, & Feedback">๐ค</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=kkogovsek" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=kkogovsek" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://github.com/wescoder"><img src="https://avatars0.githubusercontent.com/u/22945955?v=4" width="100px;" alt=""/><br /><sub><b>Wรฉsley Queiroz</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Awescoder" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=wescoder" title="Code">๐ป</a></td> | ||
<td align="center"><a href="https://www.good-idea.studio"><img src="https://avatars3.githubusercontent.com/u/11514928?v=4" width="100px;" alt=""/><br /><sub><b>Joseph Thomas</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Agood-idea" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=good-idea" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=good-idea" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="http://edvinasbartkus.lt"><img src="https://avatars0.githubusercontent.com/u/202988?v=4" width="100px;" alt=""/><br /><sub><b>Edvinas Bartkus</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=edvinasbartkus" title="Code">๐ป</a> <a href="#question-edvinasbartkus" title="Answering Questions">๐ฌ</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Aedvinasbartkus" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=edvinasbartkus" title="Documentation">๐</a> <a href="#example-edvinasbartkus" title="Examples">๐ก</a> <a href="#ideas-edvinasbartkus" title="Ideas, Planning, & Feedback">๐ค</a> <a href="#maintenance-edvinasbartkus" title="Maintenance">๐ง</a> <a href="https://github.com/nearform/graphql-hooks/pulls?q=is%3Apr+reviewed-by%3Aedvinasbartkus" title="Reviewed Pull Requests">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=edvinasbartkus" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://github.com/olistic"><img src="https://avatars1.githubusercontent.com/u/5600126?v=4" width="100px;" alt=""/><br /><sub><b>Matรญas Olivera</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Aolistic" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=olistic" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=olistic" title="Tests">โ ๏ธ</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=olistic" title="Documentation">๐</a></td> | ||
<td align="center"><a href="https://github.com/tcudok-jg"><img src="https://avatars3.githubusercontent.com/u/50208575?v=4" width="100px;" alt=""/><br /><sub><b>tcudok-jg</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=tcudok-jg" title="Code">๐ป</a></td> | ||
<td align="center"><a href="https://github.com/heymartinadams"><img src="https://avatars2.githubusercontent.com/u/11673745?v=4" width="100px;" alt=""/><br /><sub><b>Martin Adams</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=heymartinadams" title="Documentation">๐</a></td> | ||
</tr> | ||
<tr> | ||
<td align="center"><a href="http://gal.js.org"><img src="https://avatars3.githubusercontent.com/u/191608?v=4" width="100px;" alt="Gal Dubitski"/><br /><sub><b>Gal Dubitski</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=galmail" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Agalmail" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=galmail" title="Documentation">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=galmail" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://twitter.com/osdevisnot"><img src="https://avatars1.githubusercontent.com/u/802242?v=4" width="100px;" alt="Abhishek Shende"/><br /><sub><b>Abhishek Shende</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=osdevisnot" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Aosdevisnot" title="Bug reports">๐</a></td> | ||
<td align="center"><a href="http://fabien.cool"><img src="https://avatars1.githubusercontent.com/u/1702255?v=4" width="100px;" alt="fabienheureux"/><br /><sub><b>fabienheureux</b></sub></a><br /><a href="#review-fabienheureux" title="Reviewed Pull Requests">๐</a></td> | ||
<td align="center"><a href="https://hughboylan.com"><img src="https://avatars2.githubusercontent.com/u/2158740?v=4" width="100px;" alt="Hugh Boylan"/><br /><sub><b>Hugh Boylan</b></sub></a><br /><a href="#review-hboylan" title="Reviewed Pull Requests">๐</a></td> | ||
<td align="center"><a href="https://github.com/bmamouri"><img src="https://avatars2.githubusercontent.com/u/6419173?v=4" width="100px;" alt="Baqer Mamouri"/><br /><sub><b>Baqer Mamouri</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=bmamouri" title="Code">๐ป</a></td> | ||
<td align="center"><a href="http://www.netnotion.com"><img src="https://avatars0.githubusercontent.com/u/17315?v=4" width="100px;" alt="Guillermo Gonzalez "/><br /><sub><b>Guillermo Gonzalez </b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=helloguille" title="Code">๐ป</a></td> | ||
<td align="center"><a href="https://github.com/brookback"><img src="https://avatars0.githubusercontent.com/u/30257156?v=4" width="100px;" alt="Johan Brook"/><br /><sub><b>Johan Brook</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=brookback" title="Code">๐ป</a></td> | ||
<td align="center"><a href="http://gal.js.org"><img src="https://avatars3.githubusercontent.com/u/191608?v=4" width="100px;" alt=""/><br /><sub><b>Gal Dubitski</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=galmail" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Agalmail" title="Bug reports">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=galmail" title="Documentation">๐</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=galmail" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://twitter.com/osdevisnot"><img src="https://avatars1.githubusercontent.com/u/802242?v=4" width="100px;" alt=""/><br /><sub><b>Abhishek Shende</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=osdevisnot" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Aosdevisnot" title="Bug reports">๐</a></td> | ||
<td align="center"><a href="http://fabien.cool"><img src="https://avatars1.githubusercontent.com/u/1702255?v=4" width="100px;" alt=""/><br /><sub><b>fabienheureux</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/pulls?q=is%3Apr+reviewed-by%3Afabienheureux" title="Reviewed Pull Requests">๐</a></td> | ||
<td align="center"><a href="https://hughboylan.com"><img src="https://avatars2.githubusercontent.com/u/2158740?v=4" width="100px;" alt=""/><br /><sub><b>Hugh Boylan</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/pulls?q=is%3Apr+reviewed-by%3Ahboylan" title="Reviewed Pull Requests">๐</a></td> | ||
<td align="center"><a href="https://github.com/bmamouri"><img src="https://avatars2.githubusercontent.com/u/6419173?v=4" width="100px;" alt=""/><br /><sub><b>Baqer Mamouri</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=bmamouri" title="Code">๐ป</a></td> | ||
<td align="center"><a href="http://www.netnotion.com"><img src="https://avatars0.githubusercontent.com/u/17315?v=4" width="100px;" alt=""/><br /><sub><b>Guillermo Gonzalez </b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=helloguille" title="Code">๐ป</a></td> | ||
<td align="center"><a href="https://github.com/brookback"><img src="https://avatars0.githubusercontent.com/u/30257156?v=4" width="100px;" alt=""/><br /><sub><b>Johan Brook</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=brookback" title="Code">๐ป</a></td> | ||
</tr> | ||
<tr> | ||
<td align="center"><a href="https://github.com/frikille"><img src="https://avatars2.githubusercontent.com/u/569278?v=4" width="100px;" alt="Peter Balazs"/><br /><sub><b>Peter Balazs</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=frikille" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=frikille" title="Documentation">๐</a> <a href="#example-frikille" title="Examples">๐ก</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=frikille" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://github.com/frikille"><img src="https://avatars2.githubusercontent.com/u/569278?v=4" width="100px;" alt=""/><br /><sub><b>Peter Balazs</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=frikille" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=frikille" title="Documentation">๐</a> <a href="#example-frikille" title="Examples">๐ก</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=frikille" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="http://panz3r.dev"><img src="https://avatars3.githubusercontent.com/u/1754457?v=4" width="100px;" alt=""/><br /><sub><b>Mattia Panzeri</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=panz3r" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=panz3r" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://github.com/lynxtaa"><img src="https://avatars1.githubusercontent.com/u/22496946?v=4" width="100px;" alt=""/><br /><sub><b>Alex Kondratyuk</b></sub></a><br /><a href="https://github.com/nearform/graphql-hooks/commits?author=lynxtaa" title="Code">๐ป</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=lynxtaa" title="Tests">โ ๏ธ</a> <a href="https://github.com/nearform/graphql-hooks/commits?author=lynxtaa" title="Documentation">๐</a> <a href="https://github.com/nearform/graphql-hooks/issues?q=author%3Alynxtaa" title="Bug reports">๐</a></td> | ||
</tr> | ||
</table> | ||
<!-- markdownlint-enable --> | ||
<!-- prettier-ignore-end --> | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> | ||
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! |
import { extractFiles } from 'extract-files' | ||
import isExtractableFileEnhanced from './isExtractableFileEnhanced' | ||
class GraphQLClient { | ||
@@ -31,2 +33,5 @@ constructor(config = {}) { | ||
this.fetchOptions = config.fetchOptions || {} | ||
this.FormData = | ||
config.FormData || | ||
(typeof FormData !== 'undefined' ? FormData : undefined) | ||
this.logErrors = config.logErrors !== undefined ? config.logErrors : true | ||
@@ -54,6 +59,2 @@ this.onError = config.onError | ||
logErrorResult({ result, operation }) { | ||
if (this.onError) { | ||
return this.onError({ result, operation }) | ||
} | ||
console.error('GraphQL Hooks Error') | ||
@@ -67,18 +68,20 @@ console.groupCollapsed('---> Full Error Details') | ||
if (error.fetchError) { | ||
console.groupCollapsed('FETCH ERROR:') | ||
console.log(error.fetchError) | ||
console.groupEnd() | ||
} | ||
if (error) { | ||
if (error.fetchError) { | ||
console.groupCollapsed('FETCH ERROR:') | ||
console.log(error.fetchError) | ||
console.groupEnd() | ||
} | ||
if (error.httpError) { | ||
console.groupCollapsed('HTTP ERROR:') | ||
console.log(error.httpError) | ||
console.groupEnd() | ||
} | ||
if (error.httpError) { | ||
console.groupCollapsed('HTTP ERROR:') | ||
console.log(error.httpError) | ||
console.groupEnd() | ||
} | ||
if (error.graphQLErrors && error.graphQLErrors.length > 0) { | ||
console.groupCollapsed('GRAPHQL ERROR:') | ||
error.graphQLErrors.forEach(err => console.log(err)) | ||
console.groupEnd() | ||
if (error.graphQLErrors && error.graphQLErrors.length > 0) { | ||
console.groupCollapsed('GRAPHQL ERROR:') | ||
error.graphQLErrors.forEach(err => console.log(err)) | ||
console.groupEnd() | ||
} | ||
} | ||
@@ -89,2 +92,3 @@ | ||
/* eslint-enable no-console */ | ||
generateResult({ fetchError, httpError, graphQLErrors, data }) { | ||
@@ -142,3 +146,7 @@ const errorFound = !!( | ||
const { clone, files } = extractFiles(operation) | ||
const { clone, files } = extractFiles( | ||
operation, | ||
'', | ||
isExtractableFileEnhanced | ||
) | ||
const operationJSON = JSON.stringify(clone) | ||
@@ -150,4 +158,10 @@ | ||
const form = new FormData() | ||
if (!this.FormData) { | ||
throw new Error( | ||
'GraphQLClient: FormData must be polyfilled or passed in new GraphQLClient({ FormData })' | ||
) | ||
} | ||
const form = new this.FormData() | ||
form.append('operations', operationJSON) | ||
@@ -228,4 +242,10 @@ | ||
.then(result => { | ||
if (result.error && this.logErrors) { | ||
this.logErrorResult({ result, operation }) | ||
if (result.error) { | ||
if (this.logErrors) { | ||
this.logErrorResult({ result, operation }) | ||
} | ||
if (this.onError) { | ||
this.onError({ result, operation }) | ||
} | ||
} | ||
@@ -232,0 +252,0 @@ return result |
Sorry, the diff of this file is not supported yet
144140
18
2380
899
+ Addedextract-files@6.0.0(transitive)
- Removedextract-files@5.0.1(transitive)
Updatedextract-files@^6.0.0