-
Notifications
You must be signed in to change notification settings - Fork 2
/
graphql.ts
83 lines (72 loc) · 2.46 KB
/
graphql.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { InMemoryCache, ApolloClient } from '@apollo/client/core'
import { createHttpLink } from '@apollo/client/link/http'
import { ApolloLink } from '@apollo/client/link/core/ApolloLink'
import fetch from 'cross-fetch'
import { ApolloClients } from '@vue/apollo-composable'
import type { ApolloClientOptions, NormalizedCacheObject } from '@apollo/client/core'
import type { ApolloClients as SSRApolloClients } from '@vue/apollo-ssr'
import type { App } from 'vue'
/*import { onError } from '@apollo/client/link/error'
const errorlink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
),
)
if (networkError) console.log(`[Network error]: ${networkError}`)
})*/
function networkMiddleware(networkToken: string)
{
return new ApolloLink((operation, forward) =>
{
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,
'network-token': networkToken
}
}))
return forward(operation)
})
}
function genClients(networkToken?: string)
{
const http = createHttpLink({ uri: (__IS_SERVER__) ? 'http://strapi:1337/graphql' : 'https://fsmpi.uni-bayreuth.de/v1/graphql', fetch, useGETForQueries: true, credentials: 'same-origin' })
const apolloOptions: ApolloClientOptions<NormalizedCacheObject> = {
link: (__IS_SERVER__ && networkToken) ? networkMiddleware(networkToken).concat(http) : http,
cache: !__IS_SERVER__
//@ts-ignore
? new InMemoryCache().restore((<Object>window.__APOLLO_STATE__).default)
: new InMemoryCache(),
...(__IS_SERVER__ ? {
// Set this on the server to optimize queries when SSR
ssrMode: true
} : {
// This will temporary disable query force-fetching
ssrForceFetchDelay: 100
})
}
if (!__IS_SERVER__)
Array.from(window!.document!.getElementsByTagName('script'))!.find((val)=>
{
return val.text.startsWith("window.__APOLLO_STATE__")
})!.remove()
let clients: SSRApolloClients = {}
clients["default"] = new ApolloClient(apolloOptions)
return clients
}
export function createGraphql(networkToken?: string)
{
const clients = genClients(networkToken)
return {
clients,
install(app: App)
{
//for (const key in clients)
app.provide(ApolloClients,
{
default: clients["default"]
})
}
}
}