import gql from 'graphql-tag' import { graphql, compose } from 'react-apollo' const PostAccount = gql` mutation PostAccount($accountid: Int, $ownerid: String, $totalAmount: Float, $sold: Float, $debit: Float, $currency: String) { postAccount(accountid: $accountid, ownerid: $ownerid, totalAmount: $totalAmount, sold: $sold, debit: $debit, currency: $currency) { accountid ownerid total_amount currency sold debit } }` const PutAccount = gql` mutation PutAccount($accountid: Int, $ownerid: String, $totalAmount: Float, $currency: String, $sold: Float, $debit: Float) { putAccount(accountid: $accountid, ownerid: $ownerid, totalAmount: $totalAmount, currency: $currency, sold: $sold, debit: $debit) { accountid ownerid total_amount currency sold debit } }` const DeleteAccount = gql` mutation DeleteAccount($accountid: Int) { deleteAccount(accountid: $accountid) { } }` const GetAccount = gql` query GetAccount($accountid: Int, $ownerid: String, $totalAmount: Float, $sold: Float, $debit: Float, $currency: String) { getAccount(accountid: $accountid, ownerid: $ownerid, totalAmount: $totalAmount, sold: $sold, debit: $debit, currency: $currency) { accountid ownerid total_amount currency sold debit } }` const GetAccount = gql` query GetAccount($accountid: Int, $ownerid: String, $totalAmount: Float, $currency: String, $sold: Float, $debit: Float) { getAccount(accountid: $accountid, ownerid: $ownerid, totalAmount: $totalAmount, currency: $currency, sold: $sold, debit: $debit) { } }` const withAppSyncAccount = compose( graphql(GetAccount, { name: 'getAccount', options: (ownProps) => ({ fetchPolicy: 'cache-and-network', variables: { accountid: ownProps.accountid, ownerid: ownProps.ownerid, totalAmount: ownProps.totalAmount, sold: ownProps.sold, debit: ownProps.debit, currency: ownProps.currency } }), props: (props) => { if (props.getAccount.error) console.log(props.getAccount.error) return props } }), graphql(PutAccount, { props: (props) => ({ putAccount: ownProps => props.mutate({ variables: { accountid: ownProps.accountid, ownerid: ownProps.ownerid, totalAmount: ownProps.totalAmount, currency: ownProps.currency, sold: ownProps.sold, debit: ownProps.debit }, optimisticResponse: () => ({ putAccount: { ...ownProps.account, __typename: 'Account', version: 1 } }) }) }) }), graphql(DeleteAccount, { props: (props) => ({ deleteAccount: ownProps => props.mutate({ variables: { accountid: ownProps.accountid }, optimisticResponse: () => ({ deleteAccount: { ...ownProps, __typename: 'Account', version: 1 } }) }) }) }) ) export { PostAccount, PutAccount, DeleteAccount, GetAccount, GetAccount, withAppSyncAccount }