⚛️😎⚛️ Use GraphQL syntax to query into an object in-memory.
MIT License
Use GraphQL syntax to query into an object in-memory.
A dependency-free micro-library to letting you select from an object using GraphQL syntax.
It's lighter than alternatives like graphql-anywhere
.
To avoid dependency on the heavy graphql
library as a peer dependency, it's recommended to use graphql-tag
for string query syntax
along with babel-plugin-graphql-tag
to pre-compile them at build-time.
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',
}
}
}