0.1.0 • Published 6 years ago

microql v0.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

MicroQL

Sort of inspired by GraphQL. But what if you have regular old JSON REST APIs which may be yours or third parties, and you want to compose them and return some kind of result? This lets you do that, and also integrate other sync and async tasks declaratively.

I probably just reinvented lisp or something. Oh well.

query({
  debug: true,
  input: {creatureType: 'Monkey'},
  services: {fieldAgent, truck},
  jobs: {

    // result       service        action            args
    monkey:    ['fieldAgent', 'findAnimal',     {animal: '$.input.creatureType'}],
    caged:     ['fieldAgent', 'tranquilize',    {animal: '$.monkey'}],
    pet:       ['truck',      'bringHome',      {animal: '$.caged'}],
  },

}, (error, {pet}) => {
  // yay, we have a monkey as a pet
  expect(error).to.not.exist
  expect(pet).to.exist
  expect(pet).to.equal('Friendly Sleepy Monkey')
  done()
})

See the full test here.

What just happened?

  1. We assume you want to aggregate results from multiple async services.
  2. Any async service (API, network, DB, hard disk) can be represented as a function: (action, args, next) =>
  3. Describe your inputs/outputs and MicroQL will infer the dependency graph and execute in maximum concurrency.
  4. All results from a query will be accumulated on a single JS object root.

How's it work?

Well, it's a combination of async.auto and jsonpath. Async auto builds an execution graph for async tasks, but you have to describe the dependencies yourself. But if we make the assumption that you describe your inputs using jsonpath, we can infer your dependencies for you. The result is there's no duplicated code and it feels similar to writing a SQL query.