1
1
import { useMemo } from 'react'
2
2
3
- import { getToken } from '@baseapp-frontend/utils/functions/token'
3
+ import { baseAppFetch } from '@baseapp-frontend/utils/functions/fetch/baseAppFetch'
4
+ import { getToken } from '@baseapp-frontend/utils/functions/token/getToken'
4
5
5
6
import { createClient } from 'graphql-ws'
6
7
import WebSocket from 'isomorphic-ws'
@@ -20,26 +21,17 @@ import {
20
21
21
22
const CACHE_TTL = 5 * 1000 // 5 seconds, to resolve preloaded results
22
23
23
- type RequestVariables = {
24
- method : string
25
- headers : {
26
- Accept : string
27
- 'Content-Type' ?: string
28
- Authorization : string
29
- }
24
+ type GetFetchOptions = {
25
+ request : RequestParameters
26
+ variables : Variables
27
+ uploadables ?: UploadableMap | null
30
28
}
31
29
32
- const getFetchOptions = async (
33
- request : RequestParameters ,
34
- variables : Variables ,
35
- uploadables ?: UploadableMap | null ,
36
- ) => {
37
- const authToken = await getToken ( )
38
- const requestVariables : RequestVariables = {
30
+ const getFetchOptions = ( { request, variables, uploadables } : GetFetchOptions ) => {
31
+ const requestVariables = {
39
32
method : 'POST' ,
40
33
headers : {
41
34
Accept : 'application/json' ,
42
- Authorization : authToken ? `Token ${ authToken } ` : '' ,
43
35
} ,
44
36
}
45
37
@@ -56,15 +48,14 @@ const getFetchOptions = async (
56
48
}
57
49
}
58
50
59
- requestVariables . headers [ 'Content-Type' ] = 'application/json'
60
51
return {
61
52
...requestVariables ,
62
- body : JSON . stringify ( {
53
+ body : {
63
54
query : request . text ,
64
55
operationName : request . name ,
65
56
operationKind : request . operationKind ,
66
57
variables,
67
- } ) ,
58
+ } ,
68
59
}
69
60
}
70
61
@@ -74,24 +65,29 @@ export async function httpFetch(
74
65
cacheConfig ?: CacheConfig ,
75
66
uploadables ?: UploadableMap | null ,
76
67
) : Promise < GraphQLResponse > {
77
- const fetchOptions = await getFetchOptions ( request , variables , uploadables )
78
-
79
- const response = await fetch ( process . env . NEXT_PUBLIC_RELAY_ENDPOINT as string , fetchOptions )
80
-
81
- const json = await response . json ( )
68
+ const fetchOptions = getFetchOptions ( { request, variables, uploadables } )
69
+ const response = await baseAppFetch ( '' , {
70
+ baseUrl : process . env . NEXT_PUBLIC_RELAY_ENDPOINT ,
71
+ decamelizeRequestBodyKeys : false ,
72
+ decamelizeRequestParamsKeys : false ,
73
+ camelizeResponseDataKeys : false ,
74
+ stringifyBody : ! uploadables ,
75
+ setContentType : ! uploadables ,
76
+ ...fetchOptions ,
77
+ } )
82
78
83
79
// GraphQL returns exceptions (for example, a missing required variable) in the "errors"
84
80
// property of the response. If any exceptions occurred when processing the request,
85
81
// throw an error to indicate to the developer what went wrong.
86
- if ( Array . isArray ( json . errors ) ) {
82
+ if ( Array . isArray ( response . errors ) ) {
87
83
throw new Error (
88
84
`Error fetching GraphQL query '${ request . name } ' with variables '${ JSON . stringify (
89
85
variables ,
90
- ) } ': ${ JSON . stringify ( json . errors ) } `,
86
+ ) } ': ${ JSON . stringify ( response . errors ) } `,
91
87
)
92
88
}
93
89
94
- return json
90
+ return response
95
91
}
96
92
97
93
const wsClient = createClient ( {
0 commit comments