- This is an analysis of the code from Replacing Relay with Redux.
- code:
- Connect(CashayBook)
- -- CashayBook
- --- Connect(RecentPosts)
- ---- RecentPosts
- ----- SinglePost
- ------ Connect(CommentContainer)
- ------- CommentContainer
- -------- Comment
Here's a bottom-up analysis. Skip to end and reach backwards if your brain works top down...
- Presentation only.
- props: Comment object. Has karma, content.
- Smart container. Has GraphQL query, takes
$postId
. Explicitly lists needed fields (karma, content) needed byComment
child. - Const mutationHandlers:
createComment
. Splices for optimistic update. mapStateToProps
: uses postId. Creates acashay
query. Passes vars, key, mutation handlers.- React component: Gets data from
this.props.cashay.data
.
- Takes postId
- Gens new uuid
- Invokes
cashay.mutate('createComment', {variables, components: {comments: variables.postId}})
Dumb. No redux/cashay. Creates CommentContainer, passes in postId.
- Smart: redux, cashay, with @connect.
- Has query queryRecentPosts ($count).
this.props.cashay.data.recentProps
is set to empty array before fetching (nice!) - mutation createPost: has optimistic "OPTIMISTICAL" logic.
- mapStateToProps calls queryRecentPosts({variables: {count:2})
- Checks recentPosts.EOF to know if can fetch more.
- get2More: calls
setVariables
, increments count by 2.
- Smart: redux, cashay, with @connect.
- Query:
postCount: getPostCount
- mutation createPost(): returns postCount. Incremented if optimistic.
render()
checksprops.cashay.isComplete
to know if querying done.
GraphiQL IDE. Hack index.js to include it.
- Implements the "backend". Defines schema, queries, mutations.
- Imports hardcode data from static files in /DB.
npm run updateSchema
generates this -- fromsrc/schema.js
.- ... not sure where this is imported?
new Cashay
- Injects 1-second timeout in transport with setTimeout to resolve promise.
- Sets up Redux with a cashayReducer.