Created
March 18, 2018 05:56
-
-
Save cauldyclark15/fe234b6afd5c245ea2840d443a636617 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gql from 'graphql-tag'; | |
import update from 'immutability-helper'; | |
import { isEmpty } from 'lodash'; | |
import { graphql } from 'react-apollo'; | |
import { | |
accountInfo, | |
locationInfo, | |
meta, | |
notificationBasicInfo, | |
settingsInfo, | |
} from './common/fragments'; | |
const NOTIFICATIONS_SUBSCRIPTION = gql` | |
subscription notificationsUpdate($userId: ID!) { | |
notificationsUpdate(userId: $userId) { | |
items { | |
...notificationBasicInfo | |
} | |
meta { | |
total | |
size | |
count | |
page | |
lastPage | |
from | |
to | |
query | |
sortBy | |
sortDirection | |
} | |
} | |
} | |
${notificationBasicInfo} | |
`; | |
export const getApp = graphql( | |
gql` | |
query getApp { | |
app { | |
clientId | |
googleAuthUrl | |
} | |
version(application: app) { | |
id | |
application | |
version | |
} | |
transactionTypes { | |
id | |
sortIndex | |
transactionNumber | |
description | |
} | |
measurementUnits { | |
id | |
name | |
abbreviation | |
unitType | |
} | |
viewer { | |
id | |
fullName | |
firstName | |
userName | |
phone | |
gmail | |
gmailWithOAuth | |
isAdmin | |
isSuperAdmin | |
settings { | |
id | |
defaultSignature | |
} | |
formattedPermissions { | |
resource | |
permissions | |
} | |
notificationCount | |
notifications(size: 100, status: "complete,pending") { | |
items { | |
...notificationBasicInfo | |
} | |
meta { | |
total | |
size | |
count | |
page | |
lastPage | |
from | |
to | |
query | |
sortBy | |
sortDirection | |
} | |
} | |
roles { | |
name | |
label | |
resources { | |
name | |
label | |
permissions | |
} | |
} | |
account { | |
id | |
...accountInfo | |
locations { | |
...locationInfo | |
} | |
} | |
createdAt | |
updatedAt | |
} | |
} | |
${accountInfo} | |
${locationInfo} | |
${notificationBasicInfo} | |
`, | |
{ | |
props: ({ | |
ownProps, | |
data, | |
data: { | |
app, | |
loading, | |
version, | |
viewer, | |
measurementUnits, | |
notifications, | |
refetch, | |
subscribeToMore, | |
}, | |
}) => ({ | |
data: { | |
shouldLogout: isEmpty(viewer), | |
loading, | |
viewer, | |
app, | |
version, | |
measurementUnits, | |
notifications, | |
refetch, | |
}, | |
subscribeToNewNotifications: params => | |
subscribeToMore({ | |
document: NOTIFICATIONS_SUBSCRIPTION, | |
variables: { | |
userId: params.userId, | |
}, | |
updateQuery: (previousResult, { subscriptionData }) => { | |
if (!subscriptionData.data) { | |
return previousResult; | |
} | |
const paginatedNotifications = | |
subscriptionData.data.notificationsUpdate; | |
return update(previousResult, { | |
viewer: { | |
notifications: { | |
$merge: paginatedNotifications, | |
}, | |
}, | |
}); | |
}, | |
}), | |
}), | |
options: ownProps => ({ | |
fetchPolicy: 'network-only', | |
variables: {}, | |
}), | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment