Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bdbergeron/apollo-ios-extensions
Helpful extensions to the apollo-ios package.
https://github.com/bdbergeron/apollo-ios-extensions
Last synced: 3 days ago
JSON representation
Helpful extensions to the apollo-ios package.
- Host: GitHub
- URL: https://github.com/bdbergeron/apollo-ios-extensions
- Owner: bdbergeron
- License: mit
- Created: 2023-10-20T02:58:29.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-31T21:11:20.000Z (9 months ago)
- Last Synced: 2025-01-17T17:52:39.369Z (25 days ago)
- Language: Swift
- Size: 6.96 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# apollo-ios-extensions
Helpful extensions to the [apollo-ios](https://github.com/apollographql/apollo-ios) package.[![Build and Test](https://github.com/bdbergeron/apollo-ios-extensions/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/bdbergeron/apollo-ios-extensions/actions/workflows/build-and-test.yml)
[![codecov](https://codecov.io/gh/bdbergeron/apollo-ios-extensions/graph/badge.svg?token=e6ZQ0eQaJk)](https://codecov.io/gh/bdbergeron/apollo-ios-extensions)
[![CodeFactor](https://www.codefactor.io/repository/github/bdbergeron/apollo-ios-extensions/badge)](https://www.codefactor.io/repository/github/bdbergeron/apollo-ios-extensions)## Features
### ApolloClientProtocol Default Values
It's wonderful we're provided with `ApolloClientProtocol`, which `ApolloClient` conforms to.
However, the `ApolloClient` implementation provides default parameter values that `ApolloClientProtocol`
doesn't provide. Consider this a non-issue now!### MockApolloClient
Want to test your app's code around `ApolloClient`? Look no further than `MockApolloClient`!
This mock object conforms to `AppoloClientProtocol` and allows you to stub responses to queries,
mutations, and subscriptions.```swift
func test_query() throws {
let apolloClient = MockApolloClient()
apolloClient.fetchResult = { parameters in
let data = TestQuery.Data(items: .init(edges: [
.init(node: .init(id: "1",name: "Brad")),
]))
return .success(GraphQLResult(data: data))
}let expectation = expectation(description: #function)
apolloClient.fetch(query: TestQuery()) { result in
expectation.fulfill()
switch result {
case .success(let result):
XCTAssertEqual(result.data?.items.edges.count, 1)
XCTAssertEqual(result.data?.items.edges[0].node.id, "1")
XCTAssertEqual(result.data?.items.edges[0].node.name, "Brad")
case .failure(let error):
XCTFail("Unexpected error: \(error)")
}
}
wait(for: [expectation], timeout: 1.0)
}
```### Testing / Mocking GraphQLResult
Want to test your generated GraphQL data models against stable JSON data that you control?
`GraphQLResult.mockedJSONResponse` is your answer!Mock your response, like `items.json` in your app bundle:
```json
{
"data": {
"items": {
"__typename": "ItemCollection",
"edges": [
{
"__typename": "ItemCollectionEdge",
"node": {
"__typename": "Item",
"id": "1",
"name": "Brad"
}
}
]
}
}
}
```Then test against that mock response:
```swift
func test_mockedJSONResponse() throws {
let url = try XCTUnwrap(Bundle.module.url(forResource: "items", withExtension: "json"))
let response = try GraphQLResult.mockedJSONResponse(
for: TestQuery(),
jsonURL: url)
let data = try XCTUnwrap(response.data)
XCTAssertEqual(data.items.edges.first?.node.__typename, "Item")
XCTAssertEqual(data.items.edges.first?.node.id, "1")
XCTAssertEqual(data.items.edges.first?.node.name, "Brad")
}
```