graphql-react
Advanced tools
Comparing version 4.1.0 to 4.2.0
# graphql-react changelog | ||
## 4.2.0 | ||
### Minor | ||
- Added a new `GraphQL` constructor option `logErrors` (default `true`) and instance property, controlling if GraphQL request errors should be console logged for easy debugging. | ||
### Patch | ||
- Updated dependencies. | ||
- Refactored `GraphQL` static methods to separate modules. | ||
- Moved JSDoc type definitions into the index file. | ||
- Manually composed package exports instead of relying on `*`. | ||
- More consistent object snapshots in tests. | ||
## 4.1.0 | ||
@@ -4,0 +18,0 @@ |
@@ -14,10 +14,14 @@ 'use strict' | ||
var _react = _interopRequireDefault(require('react')) | ||
var _fastDeepEqual = _interopRequireDefault(require('fast-deep-equal')) | ||
var _propTypes = _interopRequireDefault(require('prop-types')) | ||
var _fastDeepEqual = _interopRequireDefault(require('fast-deep-equal')) | ||
var _react = _interopRequireDefault(require('react')) | ||
var _graphql = require('./graphql') | ||
var _graphqlFetchOptions = require('./graphqlFetchOptions') | ||
var _hashObject = require('./hashObject') | ||
var GraphQLContext = _react.default.createContext() | ||
@@ -90,7 +94,7 @@ | ||
if (props.loadOnMount) { | ||
var fetchOptions = props.graphql.constructor.fetchOptions(props.operation) | ||
var fetchOptions = (0, _graphqlFetchOptions.graphqlFetchOptions)( | ||
props.operation | ||
) | ||
if (props.fetchOptionsOverride) props.fetchOptionsOverride(fetchOptions) | ||
_this.state.fetchOptionsHash = props.graphql.constructor.hashFetchOptions( | ||
fetchOptions | ||
) | ||
_this.state.fetchOptionsHash = (0, _hashObject.hashObject)(fetchOptions) | ||
_this.state.requestCache = | ||
@@ -97,0 +101,0 @@ props.graphql.cache[_this.state.fetchOptionsHash] |
@@ -12,172 +12,171 @@ 'use strict' | ||
var _extractFiles = require('extract-files') | ||
var _mitt2 = _interopRequireDefault(require('mitt')) | ||
var _fnv1a = _interopRequireDefault(require('fnv1a')) | ||
var _graphqlFetchOptions = require('./graphqlFetchOptions') | ||
var _mitt2 = _interopRequireDefault(require('mitt')) | ||
var _hashObject = require('./hashObject') | ||
var GraphQL = (function() { | ||
GraphQL.requestBody = function requestBody(operation) { | ||
var files = (0, _extractFiles.extractFiles)(operation) | ||
var GraphQL = function GraphQL(_temp) { | ||
var _this = this | ||
if (files.length) { | ||
var form = new FormData() | ||
form.append('operations', JSON.stringify(operation)) | ||
form.append( | ||
'map', | ||
JSON.stringify( | ||
files.reduce(function(map, _ref, index) { | ||
var path = _ref.path | ||
map['' + index] = [path] | ||
return map | ||
}, {}) | ||
) | ||
) | ||
files.forEach(function(_ref2, index) { | ||
var file = _ref2.file | ||
return form.append(index, file, file.name) | ||
var _ref = _temp === void 0 ? {} : _temp, | ||
_ref$cache = _ref.cache, | ||
cache = _ref$cache === void 0 ? {} : _ref$cache, | ||
_ref$logErrors = _ref.logErrors, | ||
logErrors = _ref$logErrors === void 0 ? true : _ref$logErrors | ||
this.reset = function(exceptFetchOptionsHash) { | ||
var fetchOptionsHashes = Object.keys(_this.cache) | ||
if (exceptFetchOptionsHash) | ||
fetchOptionsHashes = fetchOptionsHashes.filter(function(hash) { | ||
return hash !== exceptFetchOptionsHash | ||
}) | ||
return form | ||
} else return JSON.stringify(operation) | ||
} | ||
fetchOptionsHashes.forEach(function(fetchOptionsHash) { | ||
return delete _this.cache[fetchOptionsHash] | ||
}) | ||
GraphQL.fetchOptions = function fetchOptions(operation) { | ||
var fetchOptions = { | ||
url: '/graphql', | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json' | ||
} | ||
} | ||
fetchOptions.body = this.requestBody(operation) | ||
if (typeof fetchOptions.body === 'string') | ||
fetchOptions.headers['Content-Type'] = 'application/json' | ||
return fetchOptions | ||
_this.emit('reset', { | ||
exceptFetchOptionsHash: exceptFetchOptionsHash | ||
}) | ||
} | ||
function GraphQL(_temp) { | ||
var _this = this | ||
this.request = function(_ref2, fetchOptionsHash) { | ||
var url = _ref2.url, | ||
options = (0, _objectWithoutPropertiesLoose2.default)(_ref2, ['url']) | ||
var requestCache = {} | ||
var fetcher = | ||
typeof fetch === 'function' | ||
? fetch | ||
: function() { | ||
return Promise.reject( | ||
new Error('Global fetch API or polyfill unavailable.') | ||
) | ||
} | ||
var _ref3 = _temp === void 0 ? {} : _temp, | ||
_ref3$cache = _ref3.cache, | ||
cache = _ref3$cache === void 0 ? {} : _ref3$cache | ||
_this.emit('fetch', { | ||
fetchOptionsHash: fetchOptionsHash | ||
}) | ||
this.reset = function(exceptFetchOptionsHash) { | ||
var fetchOptionsHashes = Object.keys(_this.cache) | ||
if (exceptFetchOptionsHash) | ||
fetchOptionsHashes = fetchOptionsHashes.filter(function(hash) { | ||
return hash !== exceptFetchOptionsHash | ||
return (_this.requests[fetchOptionsHash] = fetcher(url, options)) | ||
.then( | ||
function(response) { | ||
if (!response.ok) | ||
requestCache.httpError = { | ||
status: response.status, | ||
statusText: response.statusText | ||
} | ||
return response.json().then( | ||
function(_ref3) { | ||
var errors = _ref3.errors, | ||
data = _ref3.data | ||
if (!errors && !data) | ||
requestCache.parseError = 'Malformed payload.' | ||
if (errors) requestCache.graphQLErrors = errors | ||
if (data) requestCache.data = data | ||
}, | ||
function(_ref4) { | ||
var message = _ref4.message | ||
requestCache.parseError = message | ||
} | ||
) | ||
}, | ||
function(_ref5) { | ||
var message = _ref5.message | ||
requestCache.fetchError = message | ||
} | ||
) | ||
.then(function() { | ||
_this.cache[fetchOptionsHash] = requestCache | ||
delete _this.requests[fetchOptionsHash] | ||
_this.emit('cache', { | ||
fetchOptionsHash: fetchOptionsHash | ||
}) | ||
fetchOptionsHashes.forEach(function(fetchOptionsHash) { | ||
return delete _this.cache[fetchOptionsHash] | ||
}) | ||
_this.emit('reset', { | ||
exceptFetchOptionsHash: exceptFetchOptionsHash | ||
}) | ||
} | ||
var fetchError = requestCache.fetchError, | ||
httpError = requestCache.httpError, | ||
parseError = requestCache.parseError, | ||
graphQLErrors = requestCache.graphQLErrors | ||
this.request = function(_ref4, fetchOptionsHash) { | ||
var url = _ref4.url, | ||
options = (0, _objectWithoutPropertiesLoose2.default)(_ref4, ['url']) | ||
var requestCache = {} | ||
var fetcher = | ||
typeof fetch === 'function' | ||
? fetch | ||
: function() { | ||
return Promise.reject( | ||
new Error('Global fetch API or polyfill unavailable.') | ||
) | ||
} | ||
if ( | ||
_this.logErrors && | ||
(fetchError || httpError || parseError || graphQLErrors) | ||
) { | ||
console.groupCollapsed( | ||
'GraphQL request (hash \u201C' + | ||
fetchOptionsHash + | ||
'\u201D) errors:' | ||
) | ||
_this.emit('fetch', { | ||
fetchOptionsHash: fetchOptionsHash | ||
}) | ||
if (fetchError) { | ||
console.groupCollapsed('Fetch:') | ||
console.log(fetchError) | ||
console.groupEnd() | ||
} | ||
return (_this.requests[fetchOptionsHash] = fetcher(url, options)) | ||
.then( | ||
function(response) { | ||
if (!response.ok) | ||
requestCache.httpError = { | ||
status: response.status, | ||
statusText: response.statusText | ||
} | ||
return response.json().then( | ||
function(_ref5) { | ||
var errors = _ref5.errors, | ||
data = _ref5.data | ||
if (!errors && !data) | ||
requestCache.parseError = 'Malformed payload.' | ||
if (errors) requestCache.graphQLErrors = errors | ||
if (data) requestCache.data = data | ||
}, | ||
function(_ref6) { | ||
var message = _ref6.message | ||
requestCache.parseError = message | ||
} | ||
) | ||
}, | ||
function(_ref7) { | ||
var message = _ref7.message | ||
requestCache.fetchError = message | ||
if (httpError) { | ||
console.groupCollapsed('HTTP:') | ||
console.log('Status: ' + httpError.status) | ||
console.log('Text: ' + httpError.statusText) | ||
console.groupEnd() | ||
} | ||
) | ||
.then(function() { | ||
_this.cache[fetchOptionsHash] = requestCache | ||
delete _this.requests[fetchOptionsHash] | ||
_this.emit('cache', { | ||
fetchOptionsHash: fetchOptionsHash | ||
}) | ||
if (parseError) { | ||
console.groupCollapsed('Parse:') | ||
console.log(parseError) | ||
console.groupEnd() | ||
} | ||
return requestCache | ||
}) | ||
} | ||
if (graphQLErrors) { | ||
console.groupCollapsed('GraphQL:') | ||
graphQLErrors.forEach(function(_ref6) { | ||
var message = _ref6.message | ||
return console.log(message) | ||
}) | ||
console.groupEnd() | ||
} | ||
this.query = function(_ref8) { | ||
var operation = _ref8.operation, | ||
fetchOptionsOverride = _ref8.fetchOptionsOverride, | ||
resetOnLoad = _ref8.resetOnLoad | ||
console.groupEnd() | ||
} | ||
var fetchOptions = _this.constructor.fetchOptions(operation) | ||
return requestCache | ||
}) | ||
} | ||
if (fetchOptionsOverride) fetchOptionsOverride(fetchOptions) | ||
this.query = function(_ref7) { | ||
var operation = _ref7.operation, | ||
fetchOptionsOverride = _ref7.fetchOptionsOverride, | ||
resetOnLoad = _ref7.resetOnLoad | ||
var fetchOptions = (0, _graphqlFetchOptions.graphqlFetchOptions)(operation) | ||
if (fetchOptionsOverride) fetchOptionsOverride(fetchOptions) | ||
var fetchOptionsHash = (0, _hashObject.hashObject)(fetchOptions) | ||
var fetchOptionsHash = _this.constructor.hashFetchOptions(fetchOptions) | ||
var request = | ||
_this.requests[fetchOptionsHash] || | ||
_this.request(fetchOptions, fetchOptionsHash) | ||
var request = | ||
_this.requests[fetchOptionsHash] || | ||
_this.request(fetchOptions, fetchOptionsHash) | ||
if (resetOnLoad) | ||
request.then(function() { | ||
return _this.reset(fetchOptionsHash) | ||
}) | ||
return { | ||
fetchOptionsHash: fetchOptionsHash, | ||
cache: _this.cache[fetchOptionsHash], | ||
request: request | ||
} | ||
if (resetOnLoad) | ||
request.then(function() { | ||
return _this.reset(fetchOptionsHash) | ||
}) | ||
return { | ||
fetchOptionsHash: fetchOptionsHash, | ||
cache: _this.cache[fetchOptionsHash], | ||
request: request | ||
} | ||
} | ||
this.cache = cache | ||
this.requests = {} | ||
var _mitt = (0, _mitt2.default)(), | ||
on = _mitt.on, | ||
off = _mitt.off, | ||
emit = _mitt.emit | ||
var _mitt = (0, _mitt2.default)(), | ||
on = _mitt.on, | ||
off = _mitt.off, | ||
emit = _mitt.emit | ||
this.on = on | ||
this.off = off | ||
this.emit = emit | ||
this.cache = cache | ||
this.requests = {} | ||
this.logErrors = logErrors | ||
} | ||
this.on = on | ||
this.off = off | ||
this.emit = emit | ||
} | ||
return GraphQL | ||
})() | ||
exports.GraphQL = GraphQL | ||
GraphQL.hashFetchOptions = function(fetchOptions) { | ||
return (0, _fnv1a.default)(JSON.stringify(fetchOptions)).toString(36) | ||
} |
'use strict' | ||
exports.__esModule = true | ||
exports.preload = exports.Query = exports.Consumer = exports.Provider = exports.GraphQL = void 0 | ||
var _graphql = require('./graphql') | ||
Object.keys(_graphql).forEach(function(key) { | ||
if (key === 'default' || key === '__esModule') return | ||
exports[key] = _graphql[key] | ||
}) | ||
exports.GraphQL = _graphql.GraphQL | ||
var _components = require('./components') | ||
Object.keys(_components).forEach(function(key) { | ||
if (key === 'default' || key === '__esModule') return | ||
exports[key] = _components[key] | ||
}) | ||
exports.Provider = _components.Provider | ||
exports.Consumer = _components.Consumer | ||
exports.Query = _components.Query | ||
var _preload = require('./preload') | ||
Object.keys(_preload).forEach(function(key) { | ||
if (key === 'default' || key === '__esModule') return | ||
exports[key] = _preload[key] | ||
}) | ||
exports.preload = _preload.preload |
{ | ||
"name": "graphql-react", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "A lightweight GraphQL client for React.", | ||
@@ -40,3 +40,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"@babel/runtime": "^7.1.2", | ||
"@babel/runtime": "^7.1.5", | ||
"extract-files": "^4.1.0", | ||
@@ -50,16 +50,18 @@ "fast-deep-equal": "^2.0.1", | ||
"devDependencies": { | ||
"@babel/cli": "^7.1.2", | ||
"@babel/core": "^7.1.2", | ||
"@babel/cli": "^7.1.5", | ||
"@babel/core": "^7.1.6", | ||
"@babel/plugin-proposal-class-properties": "^7.1.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.0.0", | ||
"@babel/plugin-transform-runtime": "^7.1.0", | ||
"@babel/preset-env": "^7.1.0", | ||
"@babel/preset-env": "^7.1.6", | ||
"@babel/preset-react": "^7.0.0", | ||
"babel-eslint": "^10.0.1", | ||
"babel-plugin-transform-replace-object-assign": "^2.0.0", | ||
"capture-stdout": "^1.0.0", | ||
"cross-fetch": "^2.2.3", | ||
"eslint": "^5.8.0", | ||
"eslint-config-env": "^1.2.1", | ||
"eslint-config-prettier": "^3.1.0", | ||
"eslint": "^5.9.0", | ||
"eslint-config-env": "^2.0.0", | ||
"eslint-config-prettier": "^3.3.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-import-order-alphabetical": "^0.0.1", | ||
"eslint-plugin-node": "^8.0.0", | ||
@@ -70,12 +72,12 @@ "eslint-plugin-prettier": "^3.0.0", | ||
"graphql-api-koa": "^2.0.0", | ||
"husky": "^1.1.2", | ||
"jsdoc-md": "^1.6.0", | ||
"koa": "^2.6.1", | ||
"husky": "^1.2.0", | ||
"jsdoc-md": "^1.7.0", | ||
"koa": "^2.6.2", | ||
"koa-bodyparser": "^4.2.1", | ||
"lint-staged": "^8.0.4", | ||
"prettier": "^1.14.3", | ||
"react": "^16.6.0", | ||
"react-dom": "^16.6.0", | ||
"lint-staged": "^8.1.0", | ||
"prettier": "^1.15.3", | ||
"react": "^16.6.3", | ||
"react-dom": "^16.6.3", | ||
"size-limit": "^0.21.0", | ||
"tap": "^12.0.1", | ||
"tap": "^12.1.0", | ||
"watch": "^1.0.2" | ||
@@ -127,3 +129,3 @@ }, | ||
"path": "lib/index.mjs", | ||
"limit": "3 KB", | ||
"limit": "3.5 KB", | ||
"ignore": [ | ||
@@ -136,3 +138,3 @@ "object-assign" | ||
"path": "lib/index.js", | ||
"limit": "3 KB", | ||
"limit": "3.5 KB", | ||
"ignore": [ | ||
@@ -139,0 +141,0 @@ "object-assign" |
@@ -129,2 +129,3 @@ ![graphql-react logo](https://cdn.jsdelivr.net/gh/jaydenseric/graphql-react@0.1.0/graphql-react-logo.svg) | ||
- [Examples](#examples-2) | ||
- [GraphQL instance property logErrors](#graphql-instance-property-logerrors) | ||
- [function Consumer](#function-consumer) | ||
@@ -154,6 +155,7 @@ - [Examples](#examples-3) | ||
| Parameter | Type | Description | | ||
| :-------------- | :-------------------------------------- | :-------------------------------------------------- | | ||
| `options` | [Object](https://mdn.io/object)? = `{}` | Options. | | ||
| `options.cache` | [Object](https://mdn.io/object)? = `{}` | Cache to import; usually from a server side render. | | ||
| Parameter | Type | Description | | ||
| :------------------ | :------------------------------------------ | :------------------------------------------------------------------ | | ||
| `options` | [Object](https://mdn.io/object)? = `{}` | Options. | | ||
| `options.cache` | [Object](https://mdn.io/object)? = `{}` | Cache to import; usually from a server side render. | | ||
| `options.logErrors` | [boolean](https://mdn.io/boolean)? = `true` | Should GraphQL request errors be console logged for easy debugging. | | ||
@@ -211,2 +213,6 @@ #### Examples | ||
#### GraphQL instance property logErrors | ||
Should GraphQL request errors be logged. May be toggled at runtime. | ||
### function Consumer | ||
@@ -213,0 +219,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67432
15
863
552
32
Updated@babel/runtime@^7.1.5