graphql-object
v1.0.3
Published
Use GraphQL syntax to query into an object in-memory.
Downloads
7
Maintainers
Readme
graphql-object
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.
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',
}
}
}