/Countries

An automatically generated Swift package for the Countries GraphQL API

Primary LanguageSwiftMIT LicenseMIT

Note

This project is no longer maintained due to the deprecation of GraphQLKit.

Countries Icon

Countries

GitHub · Contributor Docs

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.

Table of Contents

Installation

Swift Package

  1. Open your Swift package in Xcode.
  2. Navigate to File > Add Packages.
  3. Paste this URL into the search field: https://github.com/david-swift/Countries
  4. Click on Copy Dependency.
  5. Navigate to the Package.swift file.
  6. In the Package initializer, under dependencies, paste the dependency into the array.

 Xcode Project

  1. Open your Xcode project in Xcode.
  2. Navigate to File > Add Packages.
  3. Paste this URL into the search field: https://github.com/david-swift/Countries
  4. Click on Add Package.

Usage

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:).

Thanks

Dependencies

Other Thanks