GraphQL iOS Swift5.1 使用

上一篇刚介绍了配置,这一篇我们说一下使用。

Apollo使用
首先定义client

let urlString = "http://localhost:8080/graphql"
let url = URL.init(string: urlString)   
let apollo = ApolloClient(url: url!)

查询操作

apollo.fetch(query: queryCustomCode, cachePolicy: .returnCacheDataElseFetch, context: nil, queue: DispatchQueue.main) { (result) in
        switch result{
        case .success(let response):
            print(response)
        case .failure(let error):
            print(error.localizedDescription)
        }
}

变更操作

apollo.perform(mutation: mutationCustomCode), context: nil, queue: DispatchQueue.main) { (result) in
        switch result{
        case .success(let response):
            print("response")
        case .failure(let error):
            print(error.localizedDescription)
        }
    }

订阅操作

apollo.subscribe(subscription: GraphQLSubscription()) { (subscriptionResult) in
        switch subscriptionResult{
        case .success(let response):
            print("")                
        case .failure(let error):
            print(error.localizedDescription)
        }
    }

你可能感兴趣的:(GraphQL iOS Swift5.1 使用)