1+ let headers = require ( '../utils/headers.js' ) ;
2+ let variables = require ( '../utils/variables.js' ) ;
3+
4+ class CustomDataQuery {
5+ constructor ( queryName , customQuery , customVariables ) {
6+ this . queryName = queryName ;
7+ this . customQuery = customQuery ;
8+ this . customVariables = customVariables ;
9+ }
10+
11+ async getData ( ) {
12+ let queryName = this . queryName ;
13+ let customQuery = this . customQuery ;
14+ let customVariables = this . customVariables ;
15+
16+ let specialQueryVariables = {
17+ since : 'KarmaSince' ,
18+ count : 'Int' ,
19+ id : 'Int' ,
20+ limit : 'Int'
21+ }
22+ Object . freeze ( specialQueryVariables ) ;
23+
24+ let queryVariables = Object . keys ( customVariables ) ;
25+ let queryVariablesString = '' ;
26+ for ( let i = 0 ; i < queryVariables . length ; i ++ ) {
27+ let type ;
28+ if ( specialQueryVariables . hasOwnProperty ( queryVariables [ i ] ) ) {
29+ type = String ( specialQueryVariables [ queryVariables [ i ] ] ) ;
30+ } else {
31+ type = String ( typeof queryVariables [ i ] ) . split ( '' ) ;
32+ type [ 0 ] = type [ 0 ] . toUpperCase ( ) ;
33+ type = type . join ( '' )
34+ }
35+
36+ if ( i !== queryVariables . length - 1 ) {
37+ queryVariablesString += `$${ queryVariables [ i ] } : ${ type } !, `
38+ } else {
39+ queryVariablesString += `$${ queryVariables [ i ] } : ${ type } !`
40+ }
41+ }
42+
43+ let info = await variables
44+ . fetch ( variables . graphql , {
45+ method : 'POST' ,
46+ headers,
47+ body : JSON . stringify ( {
48+ query : `
49+ query ${ queryName } (${ queryVariablesString } ) {
50+ ${ customQuery }
51+ }` ,
52+ variables : JSON . stringify ( customVariables )
53+ } )
54+ } )
55+ . then ( res => res . json ( ) ) ;
56+
57+ if ( info . errors ) {
58+ throw new Error ( `Custom Data Query ${ this . queryName } returned an error: ${ JSON . stringify ( info . errors ) } ` ) ;
59+ } else {
60+ return info . data ;
61+ }
62+ }
63+ }
64+
65+ class CustomRecursiveQuery {
66+ constructor ( queryName , customQuery , customVariables , treePath , customAfter , customCount ) {
67+ this . queryName = queryName ;
68+ this . customQuery = customQuery ;
69+ this . customVariables = customVariables ;
70+ this . treePath = treePath ;
71+ this . customAfter = customAfter ;
72+ this . customCount = customCount ;
73+ }
74+
75+ async getData ( ) {
76+ let queryName = this . queryName ;
77+ let customQuery = this . customQuery ;
78+ let customVariables = this . customVariables ;
79+ let treePath = this . treePath ;
80+ let customAfter = this . customAfter ;
81+ let customCount = this . customCount ;
82+
83+ let specialQueryVariables = {
84+ since : 'KarmaSince' ,
85+ count : 'Int' ,
86+ id : 'Int' ,
87+ limit : 'Int'
88+ }
89+ Object . freeze ( specialQueryVariables ) ;
90+
91+ let queryVariables = Object . keys ( customVariables ) ;
92+ let queryVariablesString = '' ;
93+ for ( let i = 0 ; i < queryVariables . length ; i ++ ) {
94+ let type ;
95+ if ( specialQueryVariables . hasOwnProperty ( queryVariables [ i ] ) ) {
96+ type = String ( specialQueryVariables [ queryVariables [ i ] ] ) ;
97+ } else {
98+ type = String ( typeof queryVariables [ i ] ) . split ( '' ) ;
99+ type [ 0 ] = type [ 0 ] . toUpperCase ( ) ;
100+ type = type . join ( '' )
101+ }
102+
103+ if ( i !== queryVariables . length - 1 ) {
104+ queryVariablesString += `$${ queryVariables [ i ] } : ${ type } !, `
105+ } else {
106+ queryVariablesString += `$${ queryVariables [ i ] } : ${ type } !`
107+ }
108+ }
109+
110+ let output = [ ] ;
111+
112+ async function recurse ( after ) {
113+ if ( after === null ) return ;
114+
115+ let info = await variables
116+ . fetch ( variables . graphql , {
117+ method : 'POST' ,
118+ headers,
119+ body : JSON . stringify ( {
120+ query : `
121+ query ${ queryName } (${ queryVariablesString } ) {
122+ ${ customQuery }
123+ }` ,
124+ variables : JSON . stringify ( customVariables )
125+ } )
126+ } )
127+ . then ( res => res . json ( ) ) ;
128+
129+ if ( info . errors ) {
130+ throw new Error ( `Custom Recusive Query ${ queryName } returned an error: ${ JSON . stringify ( info . errors ) } ` ) ;
131+ } else {
132+ info . data [ customQuery . trim ( ) . split ( '(' ) [ 0 ] ] [ treePath ] . items . forEach ( item => {
133+ output . push ( item ) ;
134+ } ) ;
135+ if ( output . length !== customCount ) {
136+ await recurse ( info . data [ customQuery . trim ( ) . split ( '(' ) [ 0 ] ] [ treePath ] . pageInfo . nextCursor ) ;
137+ }
138+ }
139+
140+ }
141+
142+ await recurse ( this . customAfter ) ;
143+ return output ;
144+ }
145+ }
146+
147+ module . exports = {
148+ CustomDataQuery : CustomDataQuery ,
149+ CustomRecursiveQuery : CustomRecursiveQuery
150+ } ;
0 commit comments