0.26.5 • Published 3 years ago

resolve-query v0.26.5

Weekly downloads
40
License
MIT
Repository
github
Last release
3 years ago

resolve-query

npm version

Provides an interface for creating read and view models and query facade for them.

Queries are used to observe a system 's state. Read Models answer Queries and are built using Projection functions. All events from the beginning of time are applied to a Read Model to build its current state (or a state at a particular moment in time if necessary). Some Read Models, called View Models, are sent to the client UI to be a part of a Redux app state. They are small enough to fit into memory and can be kept up to date in the browser.

import { createReadModel, createViewModel, createFacade } from 'resolve-query'

Usage

To create a read model, pass the following arguments to the createReadModel factory function:

  • eventStore - a configured eventStore instance.
  • projection - functions converting an event stream into a read model storage. A projection form is dependent on the used adapter. When the default adapter is used, a projection is a map of functions (one function for each event type) which manipulate data in the provided MongoDB-like store.
  • adapter - a read model adapter instance. A memory adapter supporting the MongoDB-like query language is used by default.

To create a view model, pass the following arguments to the createViewModel factory function:

To create a query facade for a read/view model, pass the following arguments to the createFacade factory function:

  • model - a read/view model resource a factory function created.
  • gqlSchema - a read model data schema description in terms of the GraphQL schema language.
  • gqlResolvers - a map of resolvers for replying to a GraphQL query depending on the specified gqlSchema and read model storage data.
  • customResolvers - optional resolvers for specific read/view models.

A facade supports the following functions to send queries to a read/view model:

  • executeQueryGraphql - gets data from read/view models using a GraphQL request;
  • executeQueryCustom - executes a custom resolver function;
  • dispose - removes a facade and releases resources.

The executeQueryGraphql function receives the following arguments:

  • qraphQLQuery (required) - the GraphQL query to get data;
  • graphQLVariables - specify it if the graphQLQuery contains variables;
  • jwtToken - non-verified actual JWT token provided from client.

The executeQueryCustom function receives the following arguments:

  • name (required) - a custom resolver name to handle a request;
  • customParams - custom parameters passed to a resolver function;
  • jwtToken - non-verified actual JWT token provided from client.

A custom query can be helpful in the following cases:

  • if the selected read model storage provides a n API for retrieving data, like Elasticsearch or Searchify;
  • to pass the actual view model state as a client-side redux initial state;
  • to use the selected adapter's internal features which are not accessible via a regular graphql query.

Example

Implement a read model for building a News state with custom GraphQL resolvers and use the resolve-query library to get the first news page. It handles events an aggregate produces ( see the resolve-command documentation).

import { createReadModel, createFacade } from 'resolve-query'
import createMemoryAdapter from 'resolve-readmodel-memory'
import createEventStore from 'resolve-es'
import createStorageAdapter from 'resolve-storage-lite'
import createBusAdapter from 'resolve-bus-memory'

import newsReadModel from './news-read-model.js'

const eventStore = createEventStore({ 
    storage: createStorageAdapter(), 
    bus: createBusAdapter()
})

const executeQueryGraphql = createFacade({
  model: createReadModel({
    eventStore,
    projection: newsReadModel.projection,
    adapter: createMemoryAdapter()
  }),
  gqlSchema: newsReadModel.gqlSchema,
  gqlResolvers: newsReadModel.gqlResolvers
})

// Request by GraphQL query with paramaters
executeQueryGraphql(
  'query ($page: ID!) { news(page: $page) { title, text, link } }',
  { page: 1 }
).then((result) => {
  console.log(result)
})
news-read-model.js
const NUMBER_OF_ITEMS_PER_PAGE = 10

export default {
  projection: {
    NewsCreated: async (store, { aggregateId,  timestamp, payload: { title, link, text } }) => {
      const news = await store.collection('News')
      await news.insert({ id: aggregateId, title, text, link })
    },

    NewsDeleted: (store, { aggregateId }) => {
      const news = await store.collection('News')
      await news.remove({ id: aggregateId })
    }
  },

  gqlSchema: `
    type News {
      id: ID!
      title: String!
      text: String
      link: String
    }
    type Query {
      news(page: Int, aggregateId: ID): [News]
    }
  `,

  gqlResolvers: {
    news: async (store, { page }) => {
      const news = await store.collection('News')

      if(Number.isInteger(+page) && (+page > 0)) {
        return await news.find({})
          .skip((page - 1) * NUMBER_OF_ITEMS_PER_PAGE)
          .limit(NUMBER_OF_ITEMS_PER_PAGE)
      }
        
      return await news.find({})
    }
  }
}
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

4 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.7

4 years ago

0.24.6

4 years ago

0.24.5

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.9

4 years ago

0.21.10

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

5 years ago

0.21.0-alpha.5

5 years ago

0.21.0-alpha.3

5 years ago

0.21.0-alpha.2

5 years ago

0.21.0-alpha.1

5 years ago

0.21.0-alpha.0

5 years ago

0.20.29

5 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.5.0-beta.1

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