GraphQL fragments made simple ⚡️
npm install fraql graphql graphql-tools graphql-tag
FraQL solves several things:
- ☀️ Isolation: fragments don't rely on name anymore
- ✨ Mocking: generate data & props from fragments
- 🤯 Collocation: put GraphQL in your components
Example
// Create fragment without naming it.const fragment = gql` fragment _ on Article { title description }` // Just use it in your queries!const query = gql` query Articles { articles { id } }`
⚡️ See live example on CodeSandbox
Motivation
Putting data next to your component is a good practice. It is built-in Relay and Lee Byron explains the advantages into his talk about the IDEA architecture.
I tried to do it by myself, but relying on fragment names is not an easy task. FraQL solves this issue by bringing isolation, fragments do not rely on their names.
The second problem solved by FraQL is the mocking. Generating a set of data for complex components is a pain. FraQL solves it by generating data right from your fragments!
Usage with React
Reference fragments into components
FraQL exports a default tag function that is a drop-in replacement for graphql-tag
. By using it you can create reusable fragments easily.
FraQL is not a framework, but it comes with good practices. It is recommended to create a static property fragments
on your components that contains a map of component properties. For each one, you specify the associated fragment.
You may have noticed that the name of the fragment is "_". FraQL transforms your fragment into an inline fragment. You can pick any name you want, because it will be dropped the transformation anyway.
const ArticleCard = <div> <h1>articletitle</h1> <p>articledescription</p> </div> // Create a map of fragments and reference them on a static property "fragments".ArticleCardfragments = article: gql` fragment _ on Article { title description } `
Use fragments into your queries
With FraQL, using a fragment into a query is obvious, just put the fragment where you want to use it.
Importing gql
from fraql
is not required for queries. In this case this is just a pass-through to graphql-tag
. The magic behind FraQL only happens when you use it on a fragment.
// Build your query by using your fragment.const ARTICLES = gql` query Articles { articles { id } }` const ArticleList = <div> <Query query=ARTICLES> dataarticles && dataarticles </Query> </div>
⚡️ See live example on CodeSandbox
⚡️ See React example in this repository
Mocking
Tools like StoryBook allows you to develop your components into an isolated environment. But you still have to write a set of data for displaying your components. Each time you modify your component, you have to modify this set of data, it is a real pain to maintain!
If all your components have fragments, you get mocking for free!
1. Generate introspection
Mocking data from a fragment requires knowing all schema types. That's why you have to generate a introspection result from your schema in order to use mocking.
FraQL exposes a method introspectSchema
to simplify this operation. The only thing you have to do is create a script that dumps your introspection result into a JSON file.
// Example of script that generates an introspection result into "schema.json".const writeFileSync = const introspectSchema = const schema = // Your schema defined server-side const data = fs
2. Create a mocker
FraQL exposes a method createMockerFromIntrospection
that creates a mocker from your schema.json
.
It is recommended to create one mocker and to use it wherever you need to generate data.
// mocker.js introspectionData
3. Mock your fragments
You can now mock fragments using mockFragment
or mockFragments
methods.
Single fragment
const fragment = gql` fragment _ on Article { id title author { name } }` const data = mocker// {// id: '4b165f7d-2ee1-4f09-8fd7-fc90d38a238a',// title: 'Hello World',// author: {// name: 'Hello World',// },// }
Multiple fragments (components)
// Generate all props directly from fragments.const props = mocker // Create an element using generated props.const articleCard = <ArticleCard ...props />
⚡️ See StoryBook example in this repository
Recipes
Compose fragments
One of the principles of React is component composition. It is recommended to do the same with your GraphQL fragments.
// ArticleTitle.js const ArticleTitle = <h2>articletitle</h2> ArticleTitlefragments = article: gql` fragment _ on Article { title } `
// ArticleCard.js const ArticleCard = <div> <ArticleTitle article=article /> <div>articletext</div> </div> ArticleCardfragments = article: gql` fragment _ on Article { text } `
gql
Use without FraQL offers a drop-in replacement for graphql-tag
but sometimes you don't use gql
to define your fragments. As mentioned in graphql-tag documentation there are lots of other ways to do it (using Babel, Webpack, etc..).
FraQL exposes a function toInlineFragment
that transforms a GraphQL fragment into an inline fragment.
const inlineFragment = const query = gql` query { articles { } }`
Mix named and inline fragments
Sometimes you may want to have the best of the two worlds, use a named fragment in one query and an inline fragment in another.
For this specific use-case FraQL exposes the original document:
const fragment = gql` fragment BaseArticleInfos on Article { title text }` const query = gql` query Articles { articles { ...BaseArticleInfos } } `
Use custom mocks
Mocking feature of FraQL is build on top of graphql-tools, it means you can customize all your mocks.
You can define global mocks when you create the mocker:
const mocker =
And you can override them into mockFragment
and mockFragments
:
const props = mocker
API
fraql
/ gql
The default export of fraql
is a drop-in replacement for graphql-tag
that automatically converts fragments into inline fragments.
const inlineFragment = gql` fragment _ on Article { title }` const query = gql` { articles { id } }`
toInlineFragment(fragmentDocument)
Converts a fragment into an inline fragment usable in requests.
const fragment = gql` fragment ArticleTitle on Article { title }` const inlineFragment = const query = gql` { articles { id } }`
introspectSchema(schema)
Generates introspection data from a schema.
const introspectionData =
createMockerFromIntrospection(introspectionData, { mocks } = {})
Generates a mocker from an introspection result generated using introspectSchema
.
You can specify mocks, using the same format as graphql-tools
.
const mocker =
mocker.mockFragment(fragment, { mocks } = {})
Generates mock data from one fragment.
You can specify mocks, using the same format as graphql-tools
.
const fragment = gql` fragment _ on Article { title }` const data = fraqlMocker// { title: 'Hello World' }
mocker.mockFragments(fragments, { mocks } = {})
Generates mock data from a map of fragments.
You can specify mocks, using the same format as graphql-tools
.
const fragments = article: gql` fragment _ on Article { title author { name } } ` book: gql` fragment _ on Book { title } ` const data = fraqlMocker// {// article: {// title: 'Hello World',// author: {// name: 'Hello World',// },// },// book: {// title: 'Hello World',// }// }
Inspiration & Thanks
- Thanks to Relay for bringing the collocation idea
- Thanks to Lee Byron for his awesome talk about IDEA Architecture
- Thanks to Apollo for the awesome tools they provide to the GraphQL community
License
MIT