Note
This project is no longer maintained due to the deprecation of GraphQLKit.
Countries is a GraphQL package for Swift, created using the GraphQL Viewer app. It enables creating queries and mutations using a type-safe, easy-to-use interface.
- Open your Swift package in Xcode.
- Navigate to
File > Add Packages
. - Paste this URL into the search field:
https://github.com/david-swift/Countries
- Click on
Copy Dependency
. - Navigate to the
Package.swift
file. - In the
Package
initializer, underdependencies
, paste the dependency into the array.
- Open your Xcode project in Xcode.
- Navigate to
File > Add Packages
. - Paste this URL into the search field:
https://github.com/david-swift/Countries
- Click on
Add Package
.
Creating a request is really simple. Here is an example on how to transfer a GraphQL query to Swift. Note that this example is using the GraphQLZero API.
try await GraphQL(url: "https://graphqlzero.almansi.me/api").query {
UserQuery(id: "1", fields: .init(
id: { print($0) },
address: .init(
geo: .init(
lat: { print($0) },
lng: { print($0) }
)
)
))
}
That is an example implementation of the following query:
query {
user(id: "1") {
id
address {
geo {
lat
lng
}
}
}
}
Instead of getting the data and having to manually find the data you requested in a second step, only one step is required in GraphQLKit
. You define what happens after fetching the data at the same time as you define what data to fetch. In the example above, the data is printed after being fetched successfully.
Many Swift features, such as loops, switch statements, and many more, enhance the way you create GraphQL requests.
For mutations, use mutation(mutations:getRequest:)
instead of query(queries:getRequest:)
.
- GraphQLKit licensed under the MIT license
- SwiftLintPlugin licensed under the MIT license
- The contributors
- SourceDocs used for generating the docs
- SwiftLint for checking whether code style conventions are violated
- The programming language Swift