@@ -173,88 +173,6 @@ login({
173173})
174174```
175175
176- ### Query Merging: Merge Multiple Queries into One Request
177-
178- ` graphql.js ` supports ** query merging** that allows you to collect all the requests into one request.
179-
180- Assume we've these queries on server, define them just like before we do:
181- ``` js
182- var fetchPost = graph .query (` {
183- post(id: $id) {
184- id
185- title
186- text
187- }
188- }` )
189-
190- var fetchComments = graph .query (` {
191- commentsOfPost: comments(postId: $postId) {
192- comment
193- owner {
194- name
195- }
196- }
197- }` )
198- ```
199-
200- Use ** ` .merge(mergeName, variables) ` ** command to put them into a merge buffer:
201-
202- ``` js
203- var postId = 123
204-
205- // This won't send a request.
206- fetchPost .merge (' buildPage' , { id: postId }).then (function (response ) {
207- console .log (response .post )
208- })
209-
210- // This also won't send a request.
211- fetchComments .merge (' buildPage' , { postId: postId }).then (function (response ) {
212- console .log (response .commentsOfPost )
213- })
214-
215- // This will send a merged request:
216- graph .commit (' buildPage' ).then (function (response ) {
217- // All base fields will be in response return.
218- console .log (response .post )
219- console .log (response .commentsOfPost )
220- })
221- ```
222-
223- This will create the following merged query generated by ** graphql.js** :
224-
225- ``` graphql
226- query ($merge024533__id : ID ! , $merge141499__postId : ID ! ) {
227- merge024533_post : {
228- post (id : $merge024533__id ) {
229- id
230- title
231- text
232- }
233- }
234- merge141499_commentsOfPost : {
235- comments (postId : $merge141499__postId ) {
236- comment
237- owner {
238- name
239- }
240- }
241- }
242- }
243- ```
244-
245- And variables will be generated, too:
246-
247- ``` js
248- {
249- " merge024533__id" : 123 ,
250- " merge141499__postId" : 123
251- }
252- ```
253-
254- > The ` merge{number} ` aliases won't be passed into your responses, since they will be used for initial seperation.
255-
256- > ⚠️ ** Important Restriction** : You cannot use multiple root fields using query merging.
257-
258176#### Direct Execution with ` .run ` and ES6 Template Tag
259177
260178If your query doesn't need any variables, it will generate a lazy execution query by default.
@@ -636,6 +554,116 @@ fragment username_admin on AdminUser {
636554}
637555```
638556
557+ ### Query Merging: Merge Multiple Queries into One Request
558+
559+ ` graphql.js ` supports ** query merging** that allows you to collect all the requests into one request.
560+
561+ Assume we've these queries on server, define them just like before we do:
562+ ``` js
563+ var fetchPost = graph .query (` {
564+ post(id: $id) {
565+ id
566+ title
567+ text
568+ }
569+ }` )
570+
571+ var fetchComments = graph .query (` {
572+ commentsOfPost: comments(postId: $postId) {
573+ comment
574+ owner {
575+ name
576+ }
577+ }
578+ }` )
579+ ```
580+
581+ Normally, we make requests as following:
582+
583+ ``` js
584+ var postId = 123
585+
586+ // This will send a request.
587+ fetchPost ({ id: postId }).then (function (response ) {
588+ console .log (response .post )
589+ })
590+
591+ // This also will send a request.
592+ fetchComments ({ postId: postId }).then (function (response ) {
593+ console .log (response .commentsOfPost )
594+ })
595+ ```
596+
597+ This will make two requests:
598+
599+ ![ ss1] ( ./resources/ss1.png )
600+
601+ Use ** ` .merge(mergeName, variables) ` ** command to put them into a merge buffer:
602+
603+ ``` js
604+ var postId = 123
605+
606+ // This won't send a request.
607+ fetchPost .merge (' buildPage' , { id: postId }).then (function (response ) {
608+ console .log (response .post )
609+ })
610+
611+ // This also won't send a request.
612+ fetchComments .merge (' buildPage' , { postId: postId }).then (function (response ) {
613+ console .log (response .commentsOfPost )
614+ })
615+ ```
616+
617+ These will create a buffer with * buildPage* name, and append the queries to that buffer. You need to use ** ` commit(mergeName) ` ** to merge the buffer and send to the server, the response will be consolidated:
618+
619+ ``` js
620+ // This will send a merged request:
621+ graph .commit (' buildPage' ).then (function (response ) {
622+ // All base fields will be in response return.
623+ console .log (response .post )
624+ console .log (response .commentsOfPost )
625+ })
626+ ```
627+
628+ And this will create only one request:
629+
630+ ![ ss2] ( ./resources/ss2.png )
631+
632+ This will create the following merged query generated by ** graphql.js** :
633+
634+ ``` graphql
635+ query ($merge024533__id : ID ! , $merge141499__postId : ID ! ) {
636+ merge024533_post : {
637+ post (id : $merge024533__id ) {
638+ id
639+ title
640+ text
641+ }
642+ }
643+ merge141499_commentsOfPost : {
644+ comments (postId : $merge141499__postId ) {
645+ comment
646+ owner {
647+ name
648+ }
649+ }
650+ }
651+ }
652+ ```
653+
654+ And variables will be generated, too:
655+
656+ ``` js
657+ {
658+ " merge024533__id" : 123 ,
659+ " merge141499__postId" : 123
660+ }
661+ ```
662+
663+ > The ` merge{number} ` aliases won't be passed into your responses, since they will be used for initial seperation.
664+
665+ > ⚠️ ** Important Restriction** : You cannot use multiple root fields using query merging.
666+
639667## Debugging
640668
641669You can pass ` debug: true ` to options parameter to get a console output looks like following:
0 commit comments