@operato/graphql 1.0.0-beta.5 → 1.0.0-beta.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,6 @@
1
1
  import { createUploadLink } from 'apollo-upload-client'
2
- import { SubscriptionClient } from 'subscriptions-transport-ws'
2
+ import { createClient } from 'graphql-ws'
3
3
 
4
- import { ServerParseError } from '@apollo/client'
5
4
  import {
6
5
  ApolloClient,
7
6
  ApolloLink,
@@ -9,13 +8,18 @@ import {
9
8
  from,
10
9
  HttpLink,
11
10
  HttpOptions,
12
- InMemoryCache
11
+ InMemoryCache,
12
+ ServerParseError,
13
+ split
13
14
  } from '@apollo/client/core'
14
15
  import { ErrorLink, onError } from '@apollo/client/link/error'
16
+ import { GraphQLWsLink } from '@apollo/client/link/subscriptions'
17
+ import { getMainDefinition } from '@apollo/client/utilities'
15
18
 
16
19
  // import { persistCache } from 'apollo-cache-persist'
17
20
 
18
21
  export const GRAPHQL_URI = '/graphql'
22
+ export const SUBSCRIPTION_URI = GRAPHQL_URI
19
23
 
20
24
  const defaultOptions: DefaultOptions = {
21
25
  watchQuery: {
@@ -103,7 +107,7 @@ const httpOptions: HttpOptions = {
103
107
 
104
108
  const httpLink = ApolloLink.split(
105
109
  operation => operation.getContext().hasUpload,
106
- createUploadLink(httpOptions),
110
+ createUploadLink(httpOptions) as any,
107
111
  new HttpLink(httpOptions)
108
112
  )
109
113
 
@@ -116,71 +120,38 @@ const httpLink = ApolloLink.split(
116
120
 
117
121
  // initPersistCache()
118
122
 
123
+ const wsLink = new GraphQLWsLink(
124
+ createClient({
125
+ url: location.origin.replace(/^http/, 'ws') + SUBSCRIPTION_URI,
126
+ connectionParams: {
127
+ headers: {
128
+ /*
129
+ 특정 도메인의 데이타만 받고자 하는 경우에, referer 정보를 제공해서 서버에서 서브도메인 정보를 취득하도록 한다.
130
+ referer: location.href
131
+ 또는, 이미 서브도메인 정보를 알고 있다면,
132
+ 'x-things-factory-domain': '[subdomain]'
133
+ 을 보낼 수 있다.
134
+ 관련 정보를 보내지 않는다면, 사용자가 권한을 가진 모든 도메인의 데이타를 수신하게 된다.
135
+ */
136
+ referer: location.href
137
+ }
138
+ }
139
+ })
140
+ )
141
+
119
142
  export const client = new ApolloClient({
120
143
  defaultOptions,
121
144
  cache,
122
- link: from([onError(ERROR_HANDLER), httpLink])
145
+ link: split(
146
+ ({ query }) => {
147
+ const def = getMainDefinition(query)
148
+ return def.kind === 'OperationDefinition' && def.operation === 'subscription'
149
+ },
150
+ wsLink,
151
+ from([onError(ERROR_HANDLER), httpLink])
152
+ )
123
153
  })
124
154
 
125
- /* SubscriptionClient */
126
- var subscriptionClient: Promise<SubscriptionClient> | null
127
-
128
- const getSubscriptionClient = async (): Promise<SubscriptionClient> => {
129
- if (!subscriptionClient) {
130
- subscriptionClient = new Promise((resolve, reject) => {
131
- var client = new SubscriptionClient(location.origin.replace(/^http/, 'ws') + '/subscriptions', {
132
- reconnect: true,
133
- connectionParams: {
134
- headers: {
135
- /*
136
- 특정 도메인의 데이타만 받고자 하는 경우에, referer 정보를 제공해서 서버에서 서브도메인 정보를 취득하도록 한다.
137
- referer: location.href
138
- 또는, 이미 서브도메인 정보를 알고 있다면,
139
- 'x-things-factory-domain': '[subdomain]'
140
- 을 보낼 수 있다.
141
- 관련 정보를 보내지 않는다면, 사용자가 권한을 가진 모든 도메인의 데이타를 수신하게 된다.
142
- */
143
- referer: location.href
144
- }
145
- }
146
- })
147
-
148
- client.onError(err => {
149
- //readyState === 3 인 경우 url을 잘 못 입력했거나, 서버에 문제가 있는 경우이므로 강제로 close() 시킨다.
150
- if (client.status === 3) {
151
- client.close()
152
- }
153
- reject(err)
154
- })
155
-
156
- client.onConnected(() => {
157
- resolve(client)
158
- })
159
- })
160
- }
161
-
162
- return await subscriptionClient
163
- }
164
-
165
- var subscriptions: (() => void)[] = []
166
-
167
155
  export const subscribe = async (request: any, subscribe: any) => {
168
- var client = await getSubscriptionClient()
169
- var { unsubscribe } = client.request(request).subscribe(subscribe)
170
-
171
- subscriptions.push(unsubscribe)
172
-
173
- return {
174
- unsubscribe() {
175
- subscriptions.splice(subscriptions.indexOf(unsubscribe), 1)
176
- unsubscribe()
177
-
178
- if (subscriptions.length == 0) {
179
- client.unsubscribeAll()
180
- client.close(true)
181
-
182
- subscriptionClient = null
183
- }
184
- }
185
- }
156
+ return client.subscribe(request).subscribe(subscribe)
186
157
  }
package/tsconfig.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "rootDir": "./",
18
18
  "declaration": true,
19
19
  "incremental": true,
20
- "types": ["node", "mocha", "@apollo/client"]
20
+ "types": ["node", "mocha"]
21
21
  },
22
22
  "include": ["**/*.ts"]
23
23
  }