Created
November 16, 2019 23:49
-
-
Save timbophillips/48a57979774a5c25aca21570d11c9240 to your computer and use it in GitHub Desktop.
modification to after ng add apollo-angular to include websockets and proxy (in this case with a local hasura server)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
...... | |
"serve": { | |
"builder": "@angular-devkit/build-angular:dev-server", | |
"options": { | |
"browserTarget": "apollo-hasura-learning:build", | |
"proxyConfig": "src/proxy.conf.json" | |
}, | |
"configurations": { | |
"production": { | |
"browserTarget": "apollo-hasura-learning:build:production" | |
} | |
} | |
}, | |
....... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NgModule } from '@angular/core'; | |
import { ApolloModule, APOLLO_OPTIONS, Apollo } from 'apollo-angular'; | |
import { HttpLinkModule, HttpLink } from 'apollo-angular-link-http'; | |
import { InMemoryCache } from 'apollo-cache-inmemory'; | |
import { WebSocketLink } from 'apollo-link-ws'; | |
import { split } from 'apollo-link'; | |
import { getMainDefinition } from 'apollo-utilities'; | |
// see src/proxyconf.json | |
const uri = '/api'; | |
// need to get the current webapp address and change it from http to ws.... solution helped out by | |
// https://stackoverflow.com/questions/48793151/angular-cli-proxy-websocket-with-proxy-conf-json | |
const wsURI = `${ window.location.protocol.replace('http', 'ws')}//${window.location.host}/ws` | |
export function createApollo(httpLink: HttpLink) { | |
// bastardised from https://www.apollographql.com/docs/angular/features/subscriptions/ | |
return { | |
link: | |
// Apollo function that returns different ApolloLink handlers for different requests | |
split( | |
({ query }) => { | |
let definition = getMainDefinition(query); | |
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription'; | |
}, | |
// if above test is true then its a subscription so it needs a WS | |
new WebSocketLink({ | |
uri: wsURI, | |
options: { | |
reconnect: true, | |
} | |
}), | |
// otherwise boring old HTTP will suffice | |
httpLink.create({ uri }), | |
), | |
cache: new InMemoryCache(), | |
}; | |
} | |
@NgModule({ | |
exports: [ApolloModule, HttpLinkModule], | |
providers: [ | |
{ | |
provide: APOLLO_OPTIONS, | |
useFactory: createApollo, | |
deps: [HttpLink], | |
}, | |
], | |
}) | |
export class GraphQLModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"/api": { | |
"target": "http://localhost:8080/v1/graphql", | |
"pathRewrite": { "^/api": "" }, | |
"secure": false | |
}, | |
"/ws": { | |
"target": "ws://localhost:8080/v1/graphql", | |
"pathRewrite": { "^/ws": "" }, | |
"secure": false, | |
"ws": true, | |
"changeOrigin": true | |
}, | |
"logLevel": "debug" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://blog.angularindepth.com/configuring-a-angular-cli-project-with-graphql-37217f66d419