A simple library to dynamically resolve GraphQL Fragments
npm i dynql
# Or Yarn
yarn add dynqlSimply create a new store instance, save fragments into it and resolve them from your query:
import{FragmentStore}from'dynql';conststore=newFragmentStore();// Manually register your fragmentstore.registerFragment("simple",`fragment simple on Something { field1 field2 ...some_other field3 { ...yet_another }}`);// Automatically registers all fragmentsstore.autoRegisterFragment(`fragment some_other on CoolElement { value1 value2 nested { ...yet_another }}fragment yet_another on AdvancedValue { hell_world}`);// Resolve the required fragments for the queryconstresolvedFragments=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}`]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 {field1field2 ...objectB}","fragment objectB on SomethingElse {field3field4}"]