1.0.9 • Published 6 years ago

dataql v1.0.9

Weekly downloads
5
License
ISC
Repository
github
Last release
6 years ago

DataQl

Fetching data by query from js object

dataql

How to install?

npm i dataql

How it works:

So you have some object with functions which return some data

  const resolvers = {
    post() {
      return {
        post: {
          _id: 1,
          name: 'Igor',
          hello: 'world',
        },
      }
    }

    postComments(post) {
      const comments = [
        {
          _id: 1,
          description: 'Hello world!',
          postId: 1,
        },
        {
          _id: 2,
          description: 'Hello universe!',
          postId: 1,
        },
        {
          _id: 3,
          description: 'Hello universe!',
          postId: 2,
        },
      ]

      return comments.filter(comment => comment.postId == post.id)
    }
  }

also you have a query

  const query = {
    post: {
      _id: null,
      name: null,
      comments: [
        _id: null,
        description: null,
      ],      
    }
  }

just fetch data by query from resolvers

const dataql = require('dataql')

dataql({ query, resolvers }).then((data) => {
  /*
    console.log(data) =>
    {
      post: {
        _id: 1,
        name: 'Igor',
        comments: [
          {
            _id: 1,
            description: 'Hello world!',
          },
          {
            _id: 2,
            description: 'Hello universe!',
          },        
        ]
      },
    }
  */
})

Typization

If you query object or array, your resolvers should return same type. In another case it will trigger error. For null query you can return anything.

API

dataql(args)

return: Promise

args Object | {}

args.query Object | null

query object where null is any possible value which return resolver.

if your resolver contain an array you should define this in query like this example above

args.entities Object | null

object which contains data, first it search data in here if he doesnt found he find in resolver

args.resolvers Object | null

object which contains functions called resolvers. resolvers should return a data

resolver can be a sync function or

args.root Object | null

data object passed to first level resolvers

args.params Object | null

query params which passed to every resolver

args.context Object | null

context object which passed to every resolver

args.info Object | { query: {} }

args.info.query Object | null

query object for resolver

resolver(root, params, context, query)

root Object | null

object from parent resolver

params Object | null

query params

context Object | null

context payload

query Object | { query: {} }

query for current resolver

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago