| 114 | }) |
| 115 | export class AppModule { |
| 116 | constructor( |
| 117 | private apollo: Apollo, |
| 118 | private httpLink: HttpLink, |
| 119 | private store: Store |
| 120 | ) { |
| 121 | // Create an http link: |
| 122 | const http: HttpLinkHandler = httpLink.create({ |
| 123 | uri: environment.GQL_ENDPOINT, |
| 124 | }); |
| 125 | |
| 126 | // Create a WebSocket link: |
| 127 | const ws: WebSocketLink = new WebSocketLink({ |
| 128 | uri: environment.GQL_SUBSCRIPTIONS_ENDPOINT, |
| 129 | options: { |
| 130 | reconnect: true, |
| 131 | lazy: true, |
| 132 | }, |
| 133 | }); |
| 134 | |
| 135 | const authLink: ApolloLink = setContext((_, { headers }) => { |
| 136 | // get the authentication token from local storage if it exists |
| 137 | const token = store.token; |
| 138 | // return the headers to the context so httpLink can read them |
| 139 | return { |
| 140 | headers: { |
| 141 | ...headers, |
| 142 | authorization: token ? `Bearer ${token}` : '', |
| 143 | }, |
| 144 | }; |
| 145 | }); |
| 146 | |
| 147 | apollo.create({ |
| 148 | link: authLink.concat( |
| 149 | ApolloLink.split( |
| 150 | (operation) => { |
| 151 | const operationAST = getOperationAST( |
| 152 | operation.query, |
| 153 | operation.operationName |
| 154 | ); |
| 155 | return ( |
| 156 | !!operationAST && |
| 157 | operationAST.operation === 'subscription' |
| 158 | ); |
| 159 | }, |
| 160 | ws, |
| 161 | http |
| 162 | ) |
| 163 | ), |
| 164 | defaultOptions: { |
| 165 | watchQuery: { |
| 166 | fetchPolicy: 'network-only', |
| 167 | errorPolicy: 'ignore', |
| 168 | }, |
| 169 | query: { |
| 170 | fetchPolicy: 'network-only', |
| 171 | errorPolicy: 'all', |
| 172 | }, |
| 173 | mutate: { |