JSPM

resolve-query

0.0.25
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 43
  • Score
    100M100P100Q82039F
  • License MIT

This package creates a function to execute a query.

Package Exports

  • resolve-query

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (resolve-query) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

🔍 resolve-query npm version

Provides a function to execute a query and get required information from a read model.

Usage

When initializing a query, pass the following arguments:

After the query is initialized, you get a function that is used to get data from read models by GraphQL request. This function receives the following arguments:

  • readModelName (required) - read model name
  • qraphQLQuery (required) - GraphQL query to get data
  • graphQLVariables - specify it, if graphQLQuery contains variables
  • getJwt - callback for retrieve actual client state stored in verified JWT token

Example

import createQueryExecutor from 'resolve-query'
import createEventStore from 'resolve-es'
import createStorageDriver from 'resolve-storage-memory'
import createBusDriver from 'resolve-bus-memory'

const eventStore = createEventStore({ 
    storage: createStorageDriver(), 
    bus: createBusDriver()
})

const readModels = [{
  name: 'users',
  initialState: [],
  eventHandlers: {
    UserCreated: (state, { payload })  => state.concat(payload)
  },
  gqlSchema: `
    type User { id: ID!, UserName: String }
    type Query { Users: [User], UserById(id: ID!): User }
  `,
  gqlResolvers: {
    Users: root => root,
    UserById: (root, args) => root.find(user => user.id === args.id)
  }
}]

const query = createQueryExecutor({ eventStore, readModels })

// Request whole read-model state
query('users').then(state => {
  console.log('Read model Users', state)
})

// Request by GraphQL query without paramaters
query('users', 'query { Users { id, UserName } }').then(state => {
  console.log('Read model Users', state)
})

// Request by GraphQL query with paramaters
query(
  'users',
  'query ($testId: ID!) { UserById(id: $testId) { id, UserName } }',
  { testId: 1 }
).then(state => {
  console.log('Read model Users', state)
})