MVVM with RxSwift+Moya+GraphQL

In iOS, we using different architectures for different kinds of requirements based on the project. Apple introduces the first MVC pattern, then various patterns used like MVP, MVVM, VIPER, and there may be more. Each one has different benefits and drawbacks.
Here I am talking about the best way for using the MVVM pattern in the project. RxSwift is a very popular library for achieving observable patterns. Moya is also one of the popular libraries used for managing network calls. GraphQL uses query-based string, which is easily readable and parses in the parameters when using API calls.
Below is a brief definition of these three.
RxSwift
Using RxSwift, you can react to changes on different threads. You do this with a lot less code, less complexity, less bugs. You can listen to an event on the main thread and react in background. Finally you can go back to the main thread to show the results.
Moya
It’s Swift Network Abstraction Library. It provides us with an abstraction to make network calls without directly communicating with Alamofire.
GarphQL
GraphQL is a query language for APIs. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.
These three things together provide an awesome ViewModel-based structure.
I have created a useful demo project for understanding this. GitHub link:- https://github.com/Dhaval1094/RocketLaunchApp-RxSwift-Moya-GraphQL
It’s the application for showing listing the data of rockets and their launch details. Apolo provides a web service for fetching rocket and launch details. From that, these three functional APIs are bound for these events.
- Fetch the launch details
- Fetch load more launch details, for pagination
- Fetch selected Rocket details

These web services and UI components are updated with the observable events already provided by RxSwift.
These are the useful pods for this architecture, which are required to run the project.
pod ‘Moya/RxSwift’, ‘~> 14.0’
The Alamofire is a widely used library for managing network calls and Moya is a highly abstracted library of the Alamofire. So, it’s better to use this pod when we are using RxSwift in the project.
Moya is providing the ‘TargetType’ protocol, with confirming this protocol we can pass all the required values in the variables, related to the web services i.e. baseurl, headers, path, etc.
On the other hand, Moya is providing the below enum cases, where we can pass the query or parameters, as an HTTP task.
When we are using GraphQL, we use ”case requestedData(Data)”.
We have to pass the JSON data of the query string using JSONSerialization.
Here the “resultObj” containing “query” is converted in the JSON data and passed to the task.
pod ‘Action’, ‘~> 4.0’
This library is used with RxSwift to provide an abstraction on top of observables: actions. Actions accept a closure that takes some input and produces an observable.
When execute()
is called, it passes its parameter to this closure and subscribes to the work.
Actions can only execute one thing at a time. If you try to execute an action that’s currently executing, you’ll get an error. The executing
property sends true
and false
values as Next
events.
pod ‘RxSwiftExt’, ‘~> 5.2’
Using only RxSwift built-in operators does not bring the exact functionality you want some time. So, this repository’s purpose is to provide additional convenience operators and Reactive Extensions.
When we are using requests for Moya provider, we can use materialize()
operator.
It provides two operators with it.
- element
- error
elements
returns a sequence of filtered element events, omitting errors.
errors
returns a sequence of filtered error events, omitting elements.
There is a list of operators provided by RxSwiftExt.
References
https://github.com/ReactiveX/RxSwift
https://github.com/RxSwiftCommunity/Action
Refer to this project for understanding structural flow and the use of the above libraries.
https://github.com/Dhaval1094/RocketLaunchApp-RxSwift-Moya-GraphQL