You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FraQL brings component isolation and automocking into your application. You can now build data aware components.
20
+
FraQL solves several things:
21
+
22
+
* ☀️ Isolation: fragments don't rely on name anymore
23
+
* ✨ Mocking: generate data & props from fragments
24
+
* 🤯 Collocation: put GraphQL in your components
21
25
22
26
## Example
23
27
24
28
```js
25
29
importgqlfrom'fraql'
26
30
27
-
// Create fragment without naming it
31
+
// Create fragment without naming it.
28
32
constfragment=gql`
29
33
fragment_onArticle {
30
34
title
31
35
description
32
36
}
33
37
`
34
38
35
-
// Just use it in your query!
39
+
// Just use it in your queries!
36
40
constquery=gql`
37
41
queryArticles {
38
42
articles {
@@ -45,9 +49,11 @@ const query = gql`
45
49
46
50
## Motivation
47
51
48
-
Puting data next to your component is a good practice. It is built-in [Relay](https://facebook.github.io/relay/) and Lee Byron explains the advantages into [his talk about the IDEA architecture](https://www.youtube.com/watch?v=oTcDmnAXZ4E).
52
+
Putting data next to your component is a good practice. It is built-in [Relay](https://facebook.github.io/relay/) and Lee Byron explains the advantages into [his talk about the IDEA architecture](https://www.youtube.com/watch?v=oTcDmnAXZ4E).
53
+
54
+
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.
49
55
50
-
If you use Apollo or other GraphQL client, it is not an easy task. The goal of FraQL is to make it simple and easy to do in all your applications (React, VueJS...). Since GraphQL is statically typed, FraQL can also generate mock from fragments!
56
+
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!
51
57
52
58
## Usage with React
53
59
@@ -57,7 +63,7 @@ FraQL exports a default tag function that is a drop-in replacement for `graphql-
57
63
58
64
FraQL is not a framework, but it is recommended create a static property `fragments` on your components that contains a map of properties. For each one, you can specify the associated fragment.
59
65
60
-
You may have noticed that the fragment use "\_" as name. FraQL transforms your fragment into an inline fragment, the name is just dropped, using "\_" is just a convention.
66
+
You may have noticed that the fragment uses "\_" as name. FraQL transforms your fragment into an inline fragment, the name is just dropped, using "\_" is just a convention.
// Create a map of fragments and reference them on a static property "fragments"
73
80
ArticleCard.fragments= {
74
81
article:gql`
75
82
fragment_onArticle {
@@ -84,16 +91,17 @@ export default ArticleCard
84
91
85
92
### Use fragments into your queries
86
93
87
-
Using a fragment into a query is natural, the only thing to do it to reference it in your query.
94
+
With FraQL, using a fragment into a query is natural, the only thing to do it to reference it at the place you want to use it.
88
95
89
-
You can choose to import `gql`either from `fraql`or from `graphql-tag` since the magic of FraQL happens only when you use it on fragments. Using `fraql` on a query has exactly the same effect as using `graphql-tag`.
96
+
Importing `gql` from `fraql`is not require for building query. In this case this is just a pass-throught to `graphql-tag`. The magic behind FraQL only happens when you use it on a fragment.
**[See an example of FraQL + React & Apollo](https://github.com/smooth-code/fraql/tree/master/examples/react)**
131
+
122
132
## Mocking
123
133
124
-
It is common to use a tool like [StoryBook](https://github.com/storybooks/storybook) to develop your components in an isolated environment. But it is always complicated to know what data are required by the component.
134
+
Tools like [StoryBook](https://github.com/storybooks/storybook)permits you to develop your components into an isolated environment. But you still have to generate a set of data for displaying your components. Maintaining this set of data is a pain.
125
135
126
-
FraQL solves this by generating your props directly from the fragments.
136
+
If all your components have fragments, you get mocking for free!
127
137
128
138
#### 1. Generate introspection
129
139
@@ -132,7 +142,7 @@ Mocking data from a fragment requires to know all schema types. You have to gene
132
142
FraQL exposes a method `introspectSchema` to simplify this operation. The only thing you have to do is creating a script that dump your introspection result into a JSON file.
133
143
134
144
```js
135
-
//buildIntrospectionResult.js
145
+
//Example of script that generate an introspection result into "schema.json"
FraQL offers a drop-in replacement for `graphql-tag` but sometimes you don't want to use `gql` to define your fragments. As mentioned in [graphql-tag documentation](https://github.com/apollographql/graphql-tag) there is a lot of other ways to do it (using Babel, Webpack, etc..).
191
267
@@ -207,6 +283,33 @@ const query = gql`
207
283
`
208
284
```
209
285
286
+
### Mix named and inline fragments
287
+
288
+
Sometimes you may want to have the best of the two world, use a name fragment in one query and a inline fragment in another.
289
+
290
+
For this specific use-case FraQL exposes the original document:
291
+
292
+
```js
293
+
importgqlfrom'fraql'
294
+
295
+
constfragment=gql`
296
+
fragmentBaseArticleInfosonArticle {
297
+
title
298
+
text
299
+
}
300
+
`
301
+
302
+
constquery=gql`
303
+
queryArticles {
304
+
articles {
305
+
...BaseArticleInfos
306
+
}
307
+
}
308
+
309
+
${fragment.originalDocument}
310
+
`
311
+
```
312
+
210
313
### Use custom mocks
211
314
212
315
Mocking feature of FraQL is build on top of [grapql-tools](https://www.apollographql.com/docs/graphql-tools/), it means you can [customize all mocks](https://www.apollographql.com/docs/graphql-tools/mocking.html#Customizing-mocks).
0 commit comments