graphql-object

1.0.3 • Public • Published

graphql-object

Use GraphQL syntax to query into an object in-memory.

npm

npm add graphql-object (copy) yarn add graphql-object (copy)

A dependency-free micro-library to letting you select from an object using GraphQL syntax. Unlike alternatives such as graphql-anywhere, it does not rely on the heavy graphql library as a peer dependency.

You can use graphql-tag for string query syntax, but it's recommended to use babel-plugin-graphql-tag to pre-compile them at build-time for almost no runtime bundlesize overhead.

This library could be handy when when migrating existing traditional applications that get data from REST endpoints to a GraphQL ecosystem by decoupling the fetching of that data from the querying into it for usage in components.

Or, it could be the basis for an architecture to switch to persisted queries from REST endpoints at runtime in production, while still using real-time GraphQL queries in development environments.

To keep it light, it does not support @skip, @include, arguments, variables, fragments, or aliases at this time.

import gql from 'graphql-tag'
import graphql from 'graphql-object'
 
const object = {
  firstName: 'Bill',
  lastName: 'Gates',
  job: {
    title: 'CEO',
    extra: true,
    company: {
      name: 'Microsoft',
      address: 'Redmond, WA 98052'
    }
  }
}
 
const query = gql`
  {
    firstName
    lastName
    job {
      title
      company {
        name
      }
    }
  }
`
 
const result = graphql(object, query)
// =>
{
  firstName: 'Bill',
  lastName: 'Gates',
  job: {
    title: 'CEO',
    company: {
      name: 'Microsoft',
    }
  }
}

Package Sidebar

Install

npm i graphql-object

Weekly Downloads

0

Version

1.0.3

License

none

Unpacked Size

4.72 kB

Total Files

5

Last publish

Collaborators

  • andersdjohnson