Automatically generate GraphQL queries and decode results into Swift objects, and also interact with arbitrary GitHub API endpoints

Last update: Aug 2, 2022

GitHub API and GraphQL Client

This package provides a generic GitHub API client (GithubApiClient) as well as Codable-like GitHub GraphQL querying and decoding based on an object's properties.

These two targets automatically handle:

The GraphQL interface is handled by defining your query using a simple Result Builder interface:

static let query = Node(type: "ProjectV2") {
	Field("title")
	Field("url")
}

Using this tree, the actual GraphQL query string can be automatically generated, as can the translation from the JSON result into an instance of your type.

GraphQL functionality is currently designed only for GitHub's GraphQL API and may not function correctly with any other GraphQL server. Additionally, this is still in extremely early development and may not yet support even all GitHub GraphQL querying operations.

The GitHub API client has explicit helper methods for a limited subset of the full API, currently including:

  • Basic app authentication
  • GraphQL queries
  • Posting issue comments and reactions

However, any arbitrary API call may be made, with authentication automatically injected.

Quick Start

Pre-built examples:

  • GitHub Actions Webhook example: command-line utility that invokes an Actions webhook on a configured repository as a GitHub App (does not use any GraphQL functionality, only the GitHub API client)
  • GitHub Projects GraphQL example: command-line utility that fetches information about a GitHub Projects (V2) item given an input node ID, authenticating as a GitHub App

Add to your Package.Swift:

...
	dependencies: [
		...
		.package(url: "https://github.com/MPLew-is/github-graphql-client", branch: "main"),
	],
	targets: [
		...
		.target(
			...
			dependencies: [
				...
				.product(name: "GithubApiClient", package: "github-graphql-client"),
				.product(name: "GithubGraphqlQueryable", package: "github-graphql-client"),
			]
		),
		...
	]
]

Query a test object from the GitHub GraphQL API:

import GithubGraphqlClient
import GithubGraphqlQueryable

@main struct GithubGraphqlExample {
	struct ProjectV2 {
		static let query = Node(type: "ProjectV2") {
			Field("title")
		}

		@Value var title: String
	}

	static func main() async throws{
		let privateKey: String = """
			-----BEGIN RSA PRIVATE KEY-----
			...
			-----END RSA PRIVATE KEY-----
			""" // Replace with your GitHub App's private key
		let client: GithubApiClient = try await .init(
			appId: "123456", // Replace with your GitHub App ID
			privateKey: privateKey
		)

		let item = try await client.graphqlQuery(
			ProjectV2.self,
			id: "PVT_...", // Replace with the unique node ID for your GitHub Project (V2)
			for: "MPLew-is" // Replace with the user on which your app has been installed)
		)
	}
}

(See the GraphQL client example for more detailed instructions on how to set up a GitHub app and get the required authentication/configuration values)

Targets provided

  • GithubGraphqlQueryable: a protocol and associated types for automatic query generation and decoding from a GraphQL JSON response

  • GithubApiClient: an abstraction layer around supported GitHub API endpoints (including the GraphQL one), automatically handling the authentication needed for a GitHub App

GitHub

https://github.com/MPLew-is/github-api-client
You might also like...

Queues, timers, and task groups in Swift

Queues, timers, and task groups in Swift

Dispatcher eases the pain of using Grand Central Dispatch by introducing 4 new Swift classes. Dispatcher Queue Group Timer Requirements Swift 2.0+ Ins

Jan 29, 2022

Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch (iOS7+ and OS X 10.9+ compatible)

Async.legacy Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch (GCD) Async rewritten for iOS7 and OS X 10.9 Compatibility

Jul 1, 2019

A Swift DSL that allows concise and effective concurrency manipulation

A Swift DSL that allows concise and effective concurrency manipulation

NOTE Brisk is being mothballed due to general incompatibilities with modern version of Swift. I recommend checking out ReactiveSwift, which solves man

May 24, 2019

⚡️ Fast async task based Swift framework with focus on type safety, concurrency and multi threading

⚡️ Fast async task based Swift framework with focus on type safety, concurrency and multi threading

Our apps constantly do work. The faster you react to user input and produce an output, the more likely is that the user will continue to use your appl

Aug 4, 2022

A lightweight swift network layer with Combine, Async-Await, and a traditional completion block.

CombineNetwork A simple light-weight network library to make network requesting simpler. It supports newer techonology such as async/await as well as

Jan 3, 2022

The projects and materials that accompany the Modern Concurrency in Swift book

Modern Concurrency in Swift: Materials This repo contains all the downloadable materials and projects associated with the Modern Concurrency in Swift

Aug 7, 2022

Tools for using Swift Concurrency on macOS 10.15 Catalina, iOS 13, tvOS 13, and watchOS 6.

ConcurrencyCompatibility Tools for using Swift Concurrency on macOS 10.15 Catalina, iOS 13, tvOS 13, and watchOS 6. Xcode 13.2 adds backwards deployme

May 17, 2022

Egg-Timer - Intermediate Swift Programming - Control Flow and Optionals

Egg-Timer Intermediate Swift Programming - Control Flow and Optionals What I lea

Jan 10, 2022

OTOperations - Useful Swift NSOperation and NSOperationQueue subclasses

OTOperations Useful Swift NSOperation (Operation) and NSOperationQueue (Operatio

Feb 23, 2022
Slack message generator and API client, written in Swift with Result Builders and Concurrency

Slack Message Client This package provides a Swift object model for a Slack Block Kit message, as well as a Result Builder convenience interface for e

Jul 30, 2022
Schedule timing task in Swift using a fluent API. (A friendly alternative to Timer)
Schedule timing task in Swift using a fluent API. (A friendly alternative to Timer)

Schedule(简体中文) Schedule is a timing tasks scheduler written in Swift. It allows you run timing tasks with elegant and intuitive syntax. Features Elega

Aug 1, 2022
Swift TableView pagination with async API request.
Swift TableView pagination with async API request.

SwiftTableViewPagination Swift TableView pagination with async API request. Output UML Create puml file. $ cd SwiftTableViewPagination/scripts/swiftum

Dec 26, 2021
Throttle massive number of inputs in a single drop of one line API.
Throttle massive number of inputs in a single drop of one line API.

Icon credits: Lorc, Delapouite & contributors Throttler Throttler is a library that throttles unnecessarily repeated and massive inputs until the last

Jun 24, 2022
PixabayImageSearchSample - SwiftUI + LazyVGrid + Pixabay API + Codable + sync/async Sample
PixabayImageSearchSample - SwiftUI + LazyVGrid + Pixabay API + Codable + sync/async Sample

PixabayImageSearchSample SwiftUI + LazyVGrid + Pixabay API + Codable + sync/asyn

Jan 13, 2022
Venice - Coroutines, structured concurrency and CSP for Swift on macOS and Linux.
Venice - Coroutines, structured concurrency and CSP for Swift on macOS and Linux.

Venice provides structured concurrency and CSP for Swift. Features Coroutines Coroutine cancelation Coroutine groups Channels Receive-only chan

Aug 6, 2022
Async and concurrent versions of Swift’s forEach, map, flatMap, and compactMap APIs.

CollectionConcurrencyKit Welcome to CollectionConcurrencyKit, a lightweight Swift package that adds asynchronous and concurrent versions of the standa

Jul 23, 2022
A complete set of primitives for concurrency and reactive programming on Swift
A complete set of primitives for concurrency and reactive programming on Swift

A complete set of primitives for concurrency and reactive programming on Swift 1.4.0 is the latest and greatest, but only for Swift 4.2 and 5.0 use 1.

Jan 29, 2022
GroupWork is an easy to use Swift framework that helps you orchestrate your concurrent, asynchronous functions in a clean and organized way
GroupWork is an easy to use Swift framework that helps you orchestrate your concurrent, asynchronous functions in a clean and organized way

GroupWork is an easy to use Swift framework that helps you orchestrate your concurrent, asynchronous functions in a clean and organized way. This help

Jan 29, 2022
SwiftCoroutine - Swift coroutines for iOS, macOS and Linux.
SwiftCoroutine - Swift coroutines for iOS, macOS and Linux.

Many languages, such as Kotlin, Go, JavaScript, Python, Rust, C#, C++ and others, already have coroutines support that makes the async/await pattern i

Aug 6, 2022