forked from artsy/emission
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchQuery.ts
55 lines (49 loc) · 1.68 KB
/
fetchQuery.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { request } from "../metaphysics"
import * as cache from "../NativeModules/GraphQLQueryCache"
import { FetchFunction } from "relay-runtime"
export const fetchQuery: FetchFunction = (operation, variables, cacheConfig, _uploadables) => {
const isQuery = operation.operationKind === "query"
const queryID = operation.id
if (isQuery && !cacheConfig.force) {
return cache.get(queryID, variables).then(fromCache => {
if (fromCache) {
return JSON.parse(fromCache)
}
return _fetchQuery(queryID, variables, isQuery)
})
} else {
return _fetchQuery(queryID, variables, isQuery)
}
}
function _fetchQuery(queryID: string, variables: object, isQuery: boolean) {
// Mark queryID as in-flight
cache.set(queryID, variables, null)
let body
if (__DEV__) {
body = { query: require("../../__generated__/complete.queryMap.json")[queryID], variables }
} else {
body = { documentID: queryID, variables }
}
return request(body)
.then(response => response.text())
.then(responseBody => {
const json: { errors: any[] } = JSON.parse(responseBody)
if (isQuery && json.errors) {
// Unmark as in-flight
cache.clear(queryID, variables)
// Log to console/Sentry
json.errors.forEach(console.error)
// Throw here so that our error view gets shown.
// See https://github.com/facebook/relay/issues/1913
throw new Error("Server-side error occurred")
}
if (isQuery) {
// Fullfil in-flight request
cache.set(queryID, variables, responseBody)
} else if (!isQuery) {
// In case of a mutation clear all.
cache.clearAll()
}
return json
})
}