0.26.5 • Published 3 years ago

resolve-redux v0.26.5

Weekly downloads
1,992
License
MIT
Repository
github
Last release
3 years ago

resolve-redux

npm version

This package contains tools for integrating reSolve with Redux.

Table of Contents 📑

Tools 🛠

createResolveMiddleware

Redux middleware used to:

1) Automatically fetch a view model state and subscribe to events. 2) Send a command to the server side.

This function takes the following arguments:

createResolveMiddleware({ viewModels [, subscribeAdapter] })

createViewModelsReducer

Generates a standard Redux reducer using reSolve view models. It does not take any arguments as it receives required data from createResolveMiddleware automatically.

This reducer includes handling the reSolve's merge action.

connect

A higher-order component (HOC), which automatically subscribes/unsubscribes to/from a view model by aggregateId and connects a React component to a Redux store.

const mapStateToProps = state => ({
    ...state[viewModelName][aggregateId],
    viewModelName, // required field
    aggregateId // required field
});

export default connect(mapStateToProps)(Component);

graphqlConnector

A higher-order component (HOC), which automatically delivers a view model's actual state by a graphql query. A connector takes the following arguments:

  • gqlQuery - a GraphQL query for retrieving data from a read model
  • options - connector options (see ApolloClient's query method for details)
  • endpointUrl - a URL address with a graphql endpoint for a target read model
const ConnectedStoryComponent = gqlConnector(
  `query($id: ID!) {
    story($id: ID!) {
      id
      text
    }
  }`,
  {
    options: ({ storyId }) => ({
      variables: {
        id: storyId
      },
      fetchPolicy: 'network-only'
    })
  },
  '/api/query/graphql'
)(StoryComponent)

createActions

Generates Redux actions using a reSolve aggregate. This function uses the reSolve's sendCommand action to pass a command from Redux to the server side. Generated actions are named as an aggregate's commands. This function takes two arguments:

  • aggregate - reSolve aggregate
  • extendActions - actions to extend or redefine resulting actions

actions

A plain object used to send special actions to be automatically handled by createResolveMiddleware. It implements the following functions.

  • sendCommand

    Sends a command to the server side. It takes the object with the following required arguments:
    • command
    • aggregateId
    • aggregateName
    • payload
  • subscribe

    Subscribes to new server-side events. This function takes two arguments:

    • eventTypes - an array of event types
    • aggregateId - an aggregate id
  • unsubscribe

    Unsubscribes from provided server-side events. This function takes two arguments:

    • eventTypes - an array of event types
    • aggregateId - an aggregate id
  • merge

    Produces an action handled by a reducer which the [`createViewModelsReducer`](#createviewmodelsreducer) function generates. A view model state is replaced with a new state
    . It takes three arguments: viewModelName - the name of a view model whose state should be updated
    aggregateId - an aggregate id * state - the state to be merged with the specified view model's existing state

Basic Usage 💻

How to Create Redux Store

import { createStore, applyMiddleware } from 'redux';
import { createResolveMiddleware } from 'resolve-redux';
import reducer from '../reducers';
import viewModels from '../../common/view-models';

const middleware = [createResolveMiddleware(viewModels)];

export default initialState => createStore(reducer, initialState, applyMiddleware(...middleware));

How to Generate Actions from Aggregate

import { createActions } from 'resolve-redux'
import { connect, bindActionCreators } from 'redux'

import App from './components/App'

export const aggregate {
  name: 'User',
  commands: {
    createUser: (state, { aggregateId, payload }) => ({
      type: 'UserCreated',
      aggregateId,
      payload
    })
  }
}

function mapDispatchToProps(dispatch) {
  actions: bindActionCreators(createActions(aggregate))
}

export default connect(() => {}, mapDispatchToProps)(App)

How to Send Command to Server

import { actions } from 'resolve-redux';

export function sendCommandAddTodoItem(aggregateId) {
    return {
        type: 'SEND_COMMAND_ADD_TODO_ITEM',
        aggregateId,
        aggregateName: 'TodoList',
        payload: { name: 'todo-list' },
        command: {
            type: 'TodoListItemAdd'
        }
    };
}

store.dispatch(sendCommandAddTodoItem('aggregateId'));

or

store.dispatch(actions.sendCommand({
    aggregateId: 'aggregateId',
    aggregateName: 'TodoList',
    payload: { name: 'todo-list' },
    command: {
        type: 'TodoListItemRemove'
    }
}));
0.27.15-alpha

3 years ago

0.27.14-alpha

3 years ago

0.27.12-alpha

3 years ago

0.27.11-alpha

3 years ago

0.27.10-alpha

3 years ago

0.27.9-alpha

3 years ago

0.27.8-alpha

3 years ago

0.27.7-alpha

3 years ago

0.27.6-alpha

3 years ago

0.27.4-alpha

3 years ago

0.27.5-alpha

3 years ago

0.27.3-alpha

3 years ago

0.26.5

3 years ago

0.27.2-alpha

3 years ago

0.27.0-alpha

3 years ago

0.26.4

3 years ago

0.26.3

3 years ago

0.26.2

3 years ago

0.26.1

3 years ago

0.26.0

3 years ago

0.25.18

3 years ago

0.25.17

4 years ago

0.25.16

4 years ago

0.25.15

4 years ago

0.25.14

4 years ago

0.25.13

4 years ago

0.25.12

4 years ago

0.25.11

4 years ago

0.25.10

4 years ago

0.25.9

4 years ago

0.25.8

4 years ago

0.25.7

4 years ago

0.25.6

4 years ago

0.25.5

4 years ago

0.25.4

4 years ago

0.25.3

4 years ago

0.25.2

4 years ago

0.25.1

4 years ago

0.25.0

4 years ago

0.24.23

4 years ago

0.24.22

4 years ago

0.24.21

4 years ago

0.24.20

4 years ago

0.24.19

4 years ago

0.24.18

4 years ago

0.24.17

4 years ago

0.24.16

4 years ago

0.24.15

4 years ago

0.24.14

4 years ago

0.24.13

4 years ago

0.24.12

4 years ago

0.24.11

4 years ago

0.24.10

4 years ago

0.24.9

4 years ago

0.24.8

4 years ago

0.24.5

4 years ago

0.24.7

4 years ago

0.24.6

4 years ago

0.24.4

4 years ago

0.24.3

4 years ago

0.24.3-alpha.0

4 years ago

0.24.2

4 years ago

0.24.0

4 years ago

0.23.2

4 years ago

0.23.1

4 years ago

0.23.0

4 years ago

0.22.15

4 years ago

0.22.14

4 years ago

0.22.12

4 years ago

0.22.13

4 years ago

0.22.11

4 years ago

0.22.10

4 years ago

0.22.9

4 years ago

0.22.8

4 years ago

0.22.7

4 years ago

0.22.6

4 years ago

0.22.5

4 years ago

0.22.4

4 years ago

0.22.3

4 years ago

0.22.2

4 years ago

0.22.1

4 years ago

0.22.0

4 years ago

0.21.15

4 years ago

0.21.14

4 years ago

0.21.13

4 years ago

0.21.12

4 years ago

0.21.11

4 years ago

0.21.10

4 years ago

0.21.9

4 years ago

0.21.8

4 years ago

0.21.7

4 years ago

0.21.6

4 years ago

0.21.5

4 years ago

0.21.4

4 years ago

0.21.3

4 years ago

0.21.2

4 years ago

0.21.0

4 years ago

0.21.0-alpha.15

4 years ago

0.21.0-alpha.9

4 years ago

0.21.0-alpha.6

4 years ago

0.21.0-alpha.5

4 years ago

0.21.0-alpha.3

4 years ago

0.21.0-alpha.2

4 years ago

0.21.0-alpha.1

4 years ago

0.21.0-alpha.0

4 years ago

0.20.29

4 years ago

0.20.28

5 years ago

0.20.27

5 years ago

0.20.26

5 years ago

0.20.25

5 years ago

0.20.24

5 years ago

0.20.23

5 years ago

0.20.22

5 years ago

0.20.21

5 years ago

0.20.20

5 years ago

0.20.19

5 years ago

0.20.18

5 years ago

0.20.17

5 years ago

0.20.16

5 years ago

0.20.15

5 years ago

0.20.14

5 years ago

0.20.13

5 years ago

0.20.12

5 years ago

0.20.11

5 years ago

0.20.10

5 years ago

0.20.9

5 years ago

0.20.8

5 years ago

0.20.7

5 years ago

0.20.6

5 years ago

0.20.5

5 years ago

0.20.4

5 years ago

0.20.3

5 years ago

0.20.2

5 years ago

0.20.1

5 years ago

0.20.0

5 years ago

0.19.8

5 years ago

0.19.7

5 years ago

0.19.6

5 years ago

0.19.5

5 years ago

0.19.4

5 years ago

0.19.3

5 years ago

0.19.2

5 years ago

0.19.1

5 years ago

0.19.0

5 years ago

0.18.17

5 years ago

0.18.16

5 years ago

0.18.15

5 years ago

0.18.14

5 years ago

0.18.13

5 years ago

0.18.12

5 years ago

0.18.11

5 years ago

0.18.10

5 years ago

0.18.9

5 years ago

0.18.8

5 years ago

0.18.7

5 years ago

0.18.6

5 years ago

0.18.5

5 years ago

0.18.4

5 years ago

0.18.3

5 years ago

0.18.2

5 years ago

0.18.1

5 years ago

0.18.0

5 years ago

0.17.4

6 years ago

0.17.3

6 years ago

0.17.2

6 years ago

0.17.1

6 years ago

0.17.0

6 years ago

0.16.1

6 years ago

0.16.0

6 years ago

0.15.2

6 years ago

0.15.1

6 years ago

0.15.0

6 years ago

0.14.4

6 years ago

0.14.3

6 years ago

0.14.2

6 years ago

0.14.1

6 years ago

0.14.0

6 years ago

0.13.2

6 years ago

0.13.1

6 years ago

0.13.0

6 years ago

0.12.3

6 years ago

0.12.1

6 years ago

0.11.0

6 years ago

0.10.2

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.1

6 years ago

0.9.0

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.4

6 years ago

0.7.2

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.4

6 years ago

0.5.3

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.5.0-beta.3

6 years ago

0.5.0-beta.2

6 years ago

0.4.0

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.44

7 years ago

0.0.43

7 years ago

0.0.42

7 years ago

0.0.41

7 years ago

0.0.40

7 years ago

0.0.39

7 years ago

0.0.38

7 years ago

0.0.37

7 years ago

0.0.36

7 years ago

0.0.35

7 years ago

0.0.34

7 years ago

0.0.33

7 years ago

0.0.32

7 years ago

0.0.31

7 years ago

0.0.30

7 years ago

0.0.29

7 years ago

0.0.28

7 years ago

0.0.27

7 years ago

0.0.26

7 years ago

0.0.25

7 years ago

0.0.24

7 years ago

0.0.23

7 years ago

0.0.22

7 years ago

0.0.21

7 years ago

0.0.20

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago