Skip to content
/ dynql Public

A simple library to dynamically resolve GraphQL Fragments

License

Notifications You must be signed in to change notification settings

deckdom/dynql

Repository files navigation

DynQL

Build Status npm Version npm bundle size (minified) Maintainability Test Coverage npm License

A simple library to dynamically resolve GraphQL Fragments

Installation

npm i dynql
# Or Yarn
yarn add dynql

Usage

Simply create a new store instance, save fragments into it and resolve them from your query:

import { FragmentStore } from 'dynql';

const store = new FragmentStore();

// Manually register your fragment
store.registerFragment("simple", `fragment simple on Something {
  field1
  field2
  ...some_other
  field3 {
    ...yet_another
  }
}`);

// Automatically registers all fragments
store.autoRegisterFragment(`
fragment some_other on CoolElement {
  value1
  value2
  nested {
    ...yet_another
  }
}

fragment yet_another on AdvancedValue {
  hell_world
}
`);

// Resolve the required fragments for the query
const resolvedFragments = store.resolve(`
query {
  someResolver {
      anElement {
          ...simple
      }
  }   
}`);

// resolvedFragments
[`fragment simple on Something {
  field1
  field2
}`, `fragment some_other on CoolElement {
  value1
  value2
  nested {
    ...yet_another
  }
}`, `fragment yet_another on AdvancedValue {
  hell_world
}`]

Nested Example

In this example, it shows how fragments can reference other fragments which are in the store which also get resolved dynamically.

store.registerFragment("objectA",
`fragment objectA on Something {
  field1
  field2
  ...objectB
}`);

store.registerFragment("objectB",
`fragment objectB on SomethingElse {
  field3
  field4
}`);

store.registerFragment("objectC",
`fragment objectC on Unused {
  unusedField
  unusedFragment   
}`);

store.resolve(`
query {
  someResolver {
      anElement {
          ...simple
      }
  }   
}`);

// Returns
[
    "fragment objectA on Something {
      field1
      field2
      ...objectB
    }",
    "fragment objectB on SomethingElse {
      field3
      field4
    }"
]

About

A simple library to dynamically resolve GraphQL Fragments

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published