Graphus is a powerful and strongly-typed, pure-Swift GraphQL client for iOS. It allows you to build GraphQL requests based on your models. If you like the project, do not forget to put star ★
If you have a question you can message me at ilia3546@me.com or via telegram.
// Make simple model
structAuthor:Decodable,Queryable{varfirstName:StringvarsecondName:Stringstaticfunc buildQuery(with builder:QueryBuilder){letquery= builder.query(keyedBy:CodingKeys.self)
query.addField(.firstName)
query.addField(.secondName)}}
// Create query for retriving authors list
letauthorsQuery=Query("authors", model:Author.self)
// Send request to the server & map response
self.client.request(authorsQuery).send(mapToDecodable:[Author].self){(result)inswitch result {case.success(let response):print("Authors", response.data)print("GraphQL errors", response.errors)case.failure(let error):print("Job failed: \(error.localizedDescription)")}}- Building GraphQL requests based on your models.
- Add any arguments to each field.
- Mutation requests
- GraphQL pagination support
- Response mapping using Codable protocol
- [TODO] ObjectMapper support
Swift 4.2 or 5.0. Ready for use on iOS 10+
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate Graphus into your Xcode project using CocoaPods, specify it in your Podfile:
pod'Graphus'# Without mappingpod'Graphus/Codable'# Codable mapping support (recommended)Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate Graphus into your Xcode project using Carthage, specify it in your Cartfile:
github "ilia3546/Graphus"
If you prefer not to use any of the aforementioned dependency managers, you can integrate Graphus into your project manually. Put Source folder in your Xcode project. Make sure to enable Copy items if needed and Create groups.
To build requests which are based on your class or struct just needs to implement the Queryable protocol which includes the following static function:
staticfunc buildQuery(with builder:QueryBuilder)Graphus use QueryBuilder object to add fields and child models:
structBook:Queryable{vartitle:Stringvarisbn:Stringstaticfunc buildQuery(with builder:QueryBuilder){letquery= builder.query()
query.addField("title")
query.addField("isbn")}}Also you can create query keyed by CodingKeys if your model is implemented Decodable protocol and you use Codable intergration:
staticfunc buildQuery(with builder:QueryBuilder){letquery= builder.query(keyedBy:CodingKeys.self)
query.addField(.title)
query.addField(.isbn)}The above example will generate following GraphQL request model:
{
title, isbn
}
You can add child models to the GraqhQL request:
structAuthor:Decodable,Queryable{varfirstName:StringvarsecondName:Stringvarbooks:[Book]staticfunc buildQuery(with builder:QueryBuilder){letquery= builder.query(keyedBy:CodingKeys.self)
query.addField(.firstName)
query.addField(.secondName)
query.addChild(Book.self, forKey:.books)}}The above example will generate following GraphQL request model:
{
firstName,
secondName,
books {
title,
isbn
}
}
After you've created GraphusClient and models you could make your first Query object:
letauthorsQuery=Query("authors", model:Author.self)Also you can add some arguments (see arguments section):
letauthorArguments:Arguments=["filter":["age":24]]letauthorsQuery=Query("authors", arguments: authorArguments, model:Author.self)Also if you do not want you can use [Field] array:
let fields: [Field] = ["firstName", "secondName"]
let authorsQuery = Query("authors", fields: fields)
In most cases, you'll want to create a single shared instance of GraphusClient and point it at your GraphQL server. The easiest way to do this is to define a global variable:
varclient:GraphusClient={letconfiguration=URLSessionConfiguration.default
// Add additional headers as needed
configuration.httpAdditionalHeaders =["Authorization":"Bearer <token>"] // Replace `<token>`
leturl=URL(string:"http://localhost:8080/graphql")!
returnGraphusClient(url: url, configuration: configuration)}()TODO
TODO
Graphus is released under the MIT license. Check LICENSE.md for details.
If you need any application or UI to be developed, message me at ilia3546@me.com or via telegram. I develop iOS apps and designs. I use swift for development. To request more functionality, you should create a new issue.
