LiveGQL 2.0.0

LiveGQL 2.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jan 2018
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Florian MARI.



LiveGQL 2.0.0

  • By
  • Florian Mari

LiveGQL: Use GraphQL Websocket subscription in Swift

LiveGQL is a simple library to use GraphQL Subscribtion on WebSocket based on Apollo Protocol.

The Android version is here

Features

  • [x] Connect to a GraphQL WebSocket server
  • [x] Send messages
  • [x] Subscribe / unsubscribe
  • [x] Close connection
  • [x] Data handling (delegate)
  • [x] Error handling
  • [x] Reconnect option
  • [x] JSON raw response
  • [x] Queue of unsent messages
  • [x] Implement all Apollo protocol (today just Client part totally implemented)
  • [ ] Next: Swift 4 update with native JSON Encoder/Decoder

Requirements

  • iOS 9.0 / tvOS 9.0
  • Xcode >= 8.1
  • Swift >= 3.0

We also use Starscream and JSONCodable, thanks to them

Communication

  • Use issue if you have any problem
  • Don’t hesitate to contribute to the project with a pull request

Installation

Manually

Just copy files in the Source folder!

Usage

Initializing

Important: to not fall out the variable in the scope, please instantiate above your viewDidLoad() for example.

import LiveGQL

let gql = LiveGQL(socket: "ws://localhost:7003/feedback")

gql.delegate = self
gql.initServer(connectionParams: nil, reconnect: true)

You can set a Dictionnary[String:String] as connectionParams like for authentification by example.

Subscribe / Unsubscribe

Subscribe

Just call subscribe method, set an identifier and your subscription query as well.

gql.subscribe(graphql: "subscription {feedbackAdded {id, text}}", identifier: "feed")
Treat server response

You have to implement delegate method, in your main ViewController (for example) just att that

override func viewDidLoad() {
        super.viewDidLoad()
        gql.delegate = self
        // Do any additional setup after loading the view, typically from a nib.
    }

Below your class add the folowing extension and implement the method:

extension ViewController: LiveGQLDelegate {
    func receivedMessage(text: String) {
        print("Received Message: \(text)")
    }
}

Unsubscribe

Just call unsubscribe method and your identifier

gql.unsubscribe(identifier: "feed")

Close connection

gql.closeConnection()

Bugs