Last active
December 13, 2018 01:43
-
-
Save joninsky/df990be428db19654d3aa6e62a788231 to your computer and use it in GitHub Desktop.
Apollo Controller
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 Apollo | |
struct ApolloController { | |
//MARK: Properties | |
//The Apollo graph QL Root object | |
var apollo: ApolloClient? | |
//MARK: Init | |
init() { | |
self.apollo = self.configureApolloWithWebSockets() | |
} | |
//MARK: Functions | |
private func configureApolloWithWebSockets() -> ApolloClient? { | |
//Struct I have to obfuscate my Auth headers. | |
let authHeaders = Hasura_Auth_Headers() | |
//I have dev and staging environments in my app. This struct will return the correct one. | |
let environment = EnvironmentController().retreiveEnvironment() | |
//Just get the default configuraiton per the docs | |
let configuration = URLSessionConfiguration.default | |
// Add additional headers as needed from my header struct. | |
configuration.httpAdditionalHeaders = authHeaders.dictionaryOfHeaders | |
//Take my Ec2 Server string and make a URL for the graph QL and subscriptions | |
guard let httpURL = environment.graphQLServerURL, let webSocketURL = environment.subscriptionServerURL else { | |
return nil | |
} | |
let httpTransport = HTTPNetworkTransport(url: httpURL, configuration: configuration, sendOperationIdentifiers: false) | |
let request = URLRequest(url: webSocketURL) | |
//This is tricky and not well documented on either Hasura or Apollo. I had a hard time following the GraphQL Spec to see if this is compliant or not. | |
let map: GraphQLMap = ["headers": authHeaders.accessHeaderDictionary] | |
let webSocketTransport = WebSocketTransport(request: request, connectingPayload: map) | |
let splitTransport = SplitNetworkTransport(httpNetworkTransport: httpTransport, webSocketNetworkTransport: webSocketTransport) | |
//Initalize the APolloClient with that URL. | |
return ApolloClient(networkTransport: splitTransport) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment