4
4
import {
5
5
CacheConfig ,
6
6
Environment ,
7
- GraphQLResponse ,
8
7
Network ,
9
8
QueryResponseCache ,
10
9
RecordSource ,
11
10
RequestParameters ,
12
11
Store ,
13
12
Variables ,
14
13
} from 'relay-runtime' ;
15
- import { fetchQuery } from '@ttoss/relay-amplify' ;
14
+ import { encodeCredentials } from '@ttoss/relay-amplify' ;
15
+ import { fetchAuthSession } from 'aws-amplify/auth' ;
16
+ import { generateClient } from 'aws-amplify/api' ;
16
17
17
18
const CACHE_TTL = 5 * 1000 ; // 5 seconds, to resolve preloaded results
18
19
20
+ const client = generateClient ( ) ;
21
+
19
22
export const responseCache : QueryResponseCache = new QueryResponseCache ( {
20
23
size : 100 ,
21
24
ttl : CACHE_TTL ,
@@ -26,7 +29,6 @@ const createNetwork = () => {
26
29
params : RequestParameters ,
27
30
variables : Variables ,
28
31
cacheConfig : CacheConfig
29
- // eslint-disable-next-line max-params
30
32
) => {
31
33
const isQuery = params . operationKind === 'query' ;
32
34
const cacheKey = params . id ?? params . cacheID ;
@@ -40,7 +42,46 @@ const createNetwork = () => {
40
42
}
41
43
}
42
44
43
- return fetchQuery ( params , variables , { } ) as GraphQLResponse ;
45
+ let credentials : string | undefined ;
46
+
47
+ try {
48
+ const authSession = await fetchAuthSession ( ) ;
49
+
50
+ if (
51
+ authSession . credentials &&
52
+ authSession . identityId &&
53
+ authSession . credentials ?. sessionToken
54
+ ) {
55
+ credentials = encodeCredentials ( {
56
+ accessKeyId : authSession . credentials ?. accessKeyId ,
57
+ identityId : authSession . identityId ,
58
+ sessionToken : authSession . credentials ?. sessionToken ,
59
+ secretAccessKey : authSession . credentials ?. secretAccessKey ,
60
+ expiration : authSession . credentials ?. expiration ,
61
+ authenticated : true ,
62
+ } ) ;
63
+ }
64
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
65
+ } catch ( err : any ) {
66
+ // eslint-disable-next-line no-console
67
+ console . error ( err ?. message ) ;
68
+ credentials = undefined ;
69
+ }
70
+
71
+ const headers : { [ key : string ] : string } = { } ;
72
+
73
+ if ( credentials ) {
74
+ headers [ 'x-credentials' ] = credentials ;
75
+ }
76
+
77
+ return client . graphql (
78
+ {
79
+ query : params . text as string ,
80
+ variables,
81
+ } ,
82
+ headers
83
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
84
+ ) as any ;
44
85
} ;
45
86
46
87
const network = Network . create ( fetchResponse ) ;
0 commit comments