Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | 10x 10x 10x 10x 10x 10x 10x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 10x 10x 10x 2x 2x 6x 6x 4x 4x 18x | // @flow import pluralize from 'pluralize'; import {upperFirst, set, isEmpty} from 'lodash'; import type {Action, ActionType} from './types'; /** * change an action to mutation object which's used to generate graphql string */ export default function actionToMutation(action: Action<ActionType>) { const mutation = { mutation: { args: { }, fields: { } } }; const {type, payload: {key = '', id}} = action; let name = ''; let args = { $payload: 'any', $where: 'any' }; let actionArgs = { data: '$payload', where: '$where' }; let fields = {}; switch(type) { case 'UPDATE_OBJECT': args = { $payload: genUpdateInputType(action) } actionArgs = { data: '$payload' } name = `update${upperFirst(key)}`; fields = {__typename: null}; break; case 'UPDATE_ARRAY': args = { $payload: genUpdateInputType(action), $where: genWhereInputType(action) } actionArgs = { data: '$payload', where: '$where' } name = `update${transformKey(key)}`; fields = {id: null}; break; case 'CREATE_ARRAY': args = { $payload: genCreateInputType(action), } actionArgs = { data: '$payload', } name = `create${transformKey(key)}`; fields = {id: null}; break; case 'DELETE_ARRAY': args = { $where: genWhereInputType(action), } actionArgs = { where: '$where', } name = `delete${transformKey(key)}`; fields = {id: null}; break; case 'CONNECT': case 'DISCONNECT': if (id) { args = { $payload: genUpdateInputType(action), $where: genWhereInputType(action) } actionArgs = { data: '$payload', where: '$where' } name = `update${transformKey(key)}`; fields = {id: null}; } else { args = { $payload: genUpdateInputType(action) } actionArgs = { data: '$payload' } name = `update${upperFirst(key)}`; // $FlowFixMe fields = {[action.payload.path]: { fields: { id: null } }}; } break; default: name = `update${transformKey(key)}`; break; } set(mutation, `mutation.args`, args); set(mutation, `mutation.fields.${name}`, { args: actionArgs, fields: isEmpty(fields) ? null : fields }); return mutation; } function genCreateInputType(action) { const key = action.payload.key; return `${transformKey(key)}CreateInput!` } function genUpdateInputType(action) { const key = action.payload.key; return `${transformKey(key)}UpdateInput!`; } function genWhereInputType(action) { const key = action.payload.key; return `${transformKey(key)}WhereUniqueInput!`; } export function transformKey(key: string) { return upperFirst(pluralize.singular(key)); } |