File tree Expand file tree Collapse file tree 3 files changed +80
-2
lines changed
apollo-cache-inmemory/src/__tests__ Expand file tree Collapse file tree 3 files changed +80
-2
lines changed Original file line number Diff line number Diff line change @@ -165,6 +165,38 @@ describe('reading from the store', () => {
165
165
} ) ;
166
166
} ) ;
167
167
168
+ it ( 'runs a basic query with custom directives' , ( ) => {
169
+ const query = gql `
170
+ query {
171
+ id
172
+ firstName @include(if: true)
173
+ lastName @upperCase
174
+ birthDate @dateFormat(format: "DD-MM-YYYY")
175
+ }
176
+ ` ;
177
+
178
+ const store = defaultNormalizedCacheFactory ( {
179
+ ROOT_QUERY : {
180
+ id : 'abcd' ,
181
+ firstName : 'James' ,
182
+ 'lastName@upperCase' : 'BOND' ,
183
+ 'birthDate@dateFormat({"format":"DD-MM-YYYY"})' : '20-05-1940' ,
184
+ } ,
185
+ } ) ;
186
+
187
+ const result = readQueryFromStore ( {
188
+ store,
189
+ query,
190
+ } ) ;
191
+
192
+ expect ( result ) . toEqual ( {
193
+ id : 'abcd' ,
194
+ firstName : 'James' ,
195
+ lastName : 'BOND' ,
196
+ birthDate : '20-05-1940' ,
197
+ } ) ;
198
+ } ) ;
199
+
168
200
it ( 'runs a basic query with default values for arguments' , ( ) => {
169
201
const query = gql `
170
202
query someBigQuery(
Original file line number Diff line number Diff line change @@ -221,6 +221,38 @@ describe('writing to the store', () => {
221
221
} ) ;
222
222
} ) ;
223
223
224
+ it ( 'properly normalizes a query with custom directives' , ( ) => {
225
+ const query = gql `
226
+ query {
227
+ id
228
+ firstName @include(if: true)
229
+ lastName @upperCase
230
+ birthDate @dateFormat(format: "DD-MM-YYYY")
231
+ }
232
+ ` ;
233
+
234
+ const result : any = {
235
+ id : 'abcd' ,
236
+ firstName : 'James' ,
237
+ lastName : 'BOND' ,
238
+ birthDate : '20-05-1940' ,
239
+ } ;
240
+
241
+ const normalized = writeQueryToStore ( {
242
+ result,
243
+ query,
244
+ } ) ;
245
+
246
+ expect ( normalized . toObject ( ) ) . toEqual ( {
247
+ ROOT_QUERY : {
248
+ id : 'abcd' ,
249
+ firstName : 'James' ,
250
+ 'lastName@upperCase' : 'BOND' ,
251
+ 'birthDate@dateFormat({"format":"DD-MM-YYYY"})' : '20-05-1940' ,
252
+ } ,
253
+ } ) ;
254
+ } ) ;
255
+
224
256
it ( 'properly normalizes a nested object with an ID' , ( ) => {
225
257
const query = gql `
226
258
{
Original file line number Diff line number Diff line change @@ -164,6 +164,8 @@ export type Directives = {
164
164
} ;
165
165
} ;
166
166
167
+ const KNOWN_DIRECTIVES : string [ ] = [ 'connection' , 'include' , 'skip' ] ;
168
+
167
169
export function getStoreKeyName (
168
170
fieldName : string ,
169
171
args ?: Object ,
@@ -197,13 +199,25 @@ export function getStoreKeyName(
197
199
}
198
200
}
199
201
202
+ let completeFieldName : string = fieldName ;
203
+
200
204
if ( args ) {
201
205
const stringifiedArgs : string = JSON . stringify ( args ) ;
206
+ completeFieldName += `(${ stringifiedArgs } )` ;
207
+ }
202
208
203
- return `${ fieldName } (${ stringifiedArgs } )` ;
209
+ if ( directives && Object . keys ( directives ) . length ) {
210
+ Object . keys ( directives ) . forEach ( key => {
211
+ if ( KNOWN_DIRECTIVES . indexOf ( key ) !== - 1 ) return ;
212
+ if ( directives [ key ] && Object . keys ( directives [ key ] ) . length ) {
213
+ completeFieldName += `@${ key } (${ JSON . stringify ( directives [ key ] ) } )` ;
214
+ } else {
215
+ completeFieldName += `@${ key } ` ;
216
+ }
217
+ } ) ;
204
218
}
205
219
206
- return fieldName ;
220
+ return completeFieldName ;
207
221
}
208
222
209
223
export function argumentsObjectFromField (
You can’t perform that action at this time.
0 commit comments