https://github.com/deckdom/dynql
A simple library to dynamically resolve GraphQL Fragments
https://github.com/deckdom/dynql
dynamic fragments graphql
Last synced: 5 months ago
JSON representation
A simple library to dynamically resolve GraphQL Fragments
- Host: GitHub
- URL: https://github.com/deckdom/dynql
- Owner: deckdom
- License: mit
- Created: 2020-03-13T19:52:30.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:39:56.000Z (over 3 years ago)
- Last Synced: 2025-09-26T04:51:29.482Z (9 months ago)
- Topics: dynamic, fragments, graphql
- Language: JavaScript
- Size: 126 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
DynQL
A simple library to dynamically resolve GraphQL Fragments
## Installation
```sh
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:
```ts
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.
```ts
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
}"
]
```