@duely/client
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1,52 +0,98 @@ | ||
// import { useMutation as useApolloMutation } from '@apollo/client'; | ||
// import { useCallback, useMemo } from 'react'; | ||
// const cache = new WeakMap(); | ||
// function wrapMutate(mutate, defaultOptions) { | ||
// return async (variables, options) => { | ||
// const { data } = await mutate({ | ||
// variables, | ||
// ...defaultOptions, | ||
// ...options | ||
// }); | ||
// const res = result(data); | ||
// if (after) { | ||
// await after(client.cache, res, variables); | ||
// } | ||
// cache.set(data, res); | ||
// return res; | ||
// }; | ||
// } | ||
// export function useMutation(mutationDef, variables, options) { | ||
// const [mutation, mergedVariables, mergedOptions] = useMemo(() => { | ||
// const { mutation, result, after, variables: defaultVariables, ...defaultOptions } = mutationDef; | ||
// const mergedVariables = { ...defaultVariables, ...variables }; | ||
// const mergedOptions = { variables: mergedVariables, ...defaultOptions, ...options }; | ||
// return [mutation, mergedVariables, mergedOptions]; | ||
// }, [mutationDef, variables, options]); | ||
// const [apolloMutate, { data: rawData, networkStatus, loading: initialLoading, ...rest }] = useApolloMutation(mutation, mergedOptions); | ||
// const mutate = useCallback((mutation, options) => { | ||
// }, [apolloMutate, ]); | ||
// const { data } = await client.mutate({ | ||
// variables, | ||
// ...defaultOptions, | ||
// ...options | ||
// }); | ||
// const res = result(data); | ||
// if (after) { | ||
// await after(client.cache, res, variables); | ||
// } | ||
// const { mutation, result, after, variables: defaultVariables, ...defaultOptions } = mutationDef; | ||
// variables = { ...defaultVariables, ...variables }; | ||
// // https://github.com/apollographql/apollo-client/blob/main/src/core/networkStatus.ts | ||
// const loading = networkStatus | ||
// ? networkStatus < 7 | ||
// : initialLoading; | ||
// let data = cache.get(rawData); | ||
// if (typeof data === 'undefined') { | ||
// const data = result(rawData); | ||
// } | ||
// return { data, loading, initialLoading, ...rest }; | ||
// const [apolloMutation, ] | ||
// } | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.useMutation = useMutation; | ||
var _react = require("react"); | ||
var _mutations = require("../mutations"); | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
var initialState = { | ||
loading: false, | ||
error: undefined, | ||
data: undefined | ||
}; | ||
var loadingState = { | ||
loading: true, | ||
error: undefined, | ||
data: undefined | ||
}; | ||
var createCompletedState = function createCompletedState(data) { | ||
return { | ||
loading: false, | ||
error: undefined, | ||
data: data | ||
}; | ||
}; | ||
var createErrorState = function createErrorState(error) { | ||
return { | ||
loading: false, | ||
error: error, | ||
data: undefined | ||
}; | ||
}; | ||
function useMutation(mutationDef) { | ||
var _useState = (0, _react.useState)(initialState), | ||
_useState2 = _slicedToArray(_useState, 2), | ||
state = _useState2[0], | ||
setState = _useState2[1]; | ||
var mutate = (0, _react.useCallback)( /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(variables, options) { | ||
var data; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
setState(loadingState); | ||
_context.prev = 1; | ||
_context.next = 4; | ||
return (0, _mutations.mutate)(mutationDef, variables, options); | ||
case 4: | ||
data = _context.sent; | ||
setState(createCompletedState(data)); | ||
_context.next = 11; | ||
break; | ||
case 8: | ||
_context.prev = 8; | ||
_context.t0 = _context["catch"](1); | ||
setState(createErrorState(_context.t0)); | ||
case 11: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, null, [[1, 8]]); | ||
})); | ||
return function (_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(), [mutationDef]); | ||
return [mutate, state]; | ||
} | ||
//# sourceMappingURL=useMutation.js.map |
@@ -167,5 +167,4 @@ "use strict"; | ||
}, | ||
onCompleted: function onCompleted(d) { | ||
after: function after(cache, result) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var result; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
@@ -175,6 +174,4 @@ while (1) { | ||
case 0: | ||
result = d['log_in']; | ||
if (result.success) { | ||
_context.next = 3; | ||
_context.next = 2; | ||
break; | ||
@@ -185,3 +182,3 @@ } | ||
case 3: | ||
case 2: | ||
if (typeof window !== 'undefined') { | ||
@@ -191,6 +188,6 @@ localStorage.setItem('user-jwt', result.jwt); | ||
_context.next = 6; | ||
_context.next = 5; | ||
return _client2.client.resetStore(); | ||
case 6: | ||
case 5: | ||
case "end": | ||
@@ -210,5 +207,4 @@ return _context.stop(); | ||
}, | ||
onCompleted: function onCompleted(d) { | ||
after: function after(cache, result) { | ||
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var result; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
@@ -218,6 +214,4 @@ while (1) { | ||
case 0: | ||
result = d['log_out']; | ||
if (result.success) { | ||
_context2.next = 3; | ||
_context2.next = 2; | ||
break; | ||
@@ -228,3 +222,3 @@ } | ||
case 3: | ||
case 2: | ||
if (typeof window !== 'undefined') { | ||
@@ -234,6 +228,6 @@ localStorage.removeItem('user-jwt'); | ||
_context2.next = 6; | ||
_context2.next = 5; | ||
return _client2.client.resetStore(); | ||
case 6: | ||
case 5: | ||
case "end": | ||
@@ -240,0 +234,0 @@ return _context2.stop(); |
{ | ||
"name": "@duely/client", | ||
"type": "module", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"repository": "github:uoleevi/duely", | ||
@@ -6,0 +6,0 @@ "description": "GraphQL client by Duely (wrapper for @apollo/client)", |
@@ -1,76 +0,42 @@ | ||
// import { useMutation as useApolloMutation } from '@apollo/client'; | ||
// import { useCallback, useMemo } from 'react'; | ||
import { useCallback, useState } from 'react'; | ||
import { mutate as _mutate } from '../mutations'; | ||
// const cache = new WeakMap(); | ||
const initialState = { | ||
loading: false, | ||
error: undefined, | ||
data: undefined | ||
}; | ||
// function wrapMutate(mutate, defaultOptions) { | ||
// return async (variables, options) => { | ||
// const { data } = await mutate({ | ||
// variables, | ||
// ...defaultOptions, | ||
// ...options | ||
// }); | ||
// const res = result(data); | ||
// if (after) { | ||
// await after(client.cache, res, variables); | ||
// } | ||
const loadingState = { | ||
loading: true, | ||
error: undefined, | ||
data: undefined | ||
}; | ||
// cache.set(data, res); | ||
const createCompletedState = data => ({ | ||
loading: false, | ||
error: undefined, | ||
data | ||
}); | ||
// return res; | ||
// }; | ||
// } | ||
const createErrorState = error => ({ | ||
loading: false, | ||
error, | ||
data: undefined | ||
}); | ||
// export function useMutation(mutationDef, variables, options) { | ||
// const [mutation, mergedVariables, mergedOptions] = useMemo(() => { | ||
// const { mutation, result, after, variables: defaultVariables, ...defaultOptions } = mutationDef; | ||
// const mergedVariables = { ...defaultVariables, ...variables }; | ||
// const mergedOptions = { variables: mergedVariables, ...defaultOptions, ...options }; | ||
// return [mutation, mergedVariables, mergedOptions]; | ||
// }, [mutationDef, variables, options]); | ||
export function useMutation(mutationDef) { | ||
const [state, setState] = useState(initialState); | ||
const mutate = useCallback(async (variables, options) => { | ||
setState(loadingState); | ||
// const [apolloMutate, { data: rawData, networkStatus, loading: initialLoading, ...rest }] = useApolloMutation(mutation, mergedOptions); | ||
try { | ||
const data = await _mutate(mutationDef, variables, options); | ||
setState(createCompletedState(data)); | ||
} catch (error) { | ||
setState(createErrorState(error)); | ||
} | ||
}, [mutationDef]); | ||
// const mutate = useCallback((mutation, options) => { | ||
// }, [apolloMutate, ]); | ||
// const { data } = await client.mutate({ | ||
// variables, | ||
// ...defaultOptions, | ||
// ...options | ||
// }); | ||
// const res = result(data); | ||
// if (after) { | ||
// await after(client.cache, res, variables); | ||
// } | ||
// const { mutation, result, after, variables: defaultVariables, ...defaultOptions } = mutationDef; | ||
// variables = { ...defaultVariables, ...variables }; | ||
// // https://github.com/apollographql/apollo-client/blob/main/src/core/networkStatus.ts | ||
// const loading = networkStatus | ||
// ? networkStatus < 7 | ||
// : initialLoading; | ||
// let data = cache.get(rawData); | ||
// if (typeof data === 'undefined') { | ||
// const data = result(rawData); | ||
// } | ||
// return { data, loading, initialLoading, ...rest }; | ||
// const [apolloMutation, ] | ||
// } | ||
return [mutate, state]; | ||
} |
@@ -36,5 +36,3 @@ import { gql } from '@apollo/client'; | ||
result: d => d['log_in'], | ||
async onCompleted(d) { | ||
const result = d['log_in'] | ||
async after(cache, result) { | ||
if (!result.success) return; | ||
@@ -60,5 +58,3 @@ | ||
result: d => d['log_out'], | ||
async onCompleted(d) { | ||
const result = d['log_out']; | ||
async after(cache, result) { | ||
if (!result.success) return; | ||
@@ -65,0 +61,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
116233
2187