An overview of GraphQL in general is available in the
README for the
Specification for GraphQL. That overview
describes a simple set of GraphQL examples that exist as tests
in this repository. A good way to get started with this repository is to walk
through that README and the corresponding tests in parallel.
Graphiti provides two important capabilities: building a type schema, and
serving queries against that type schema.
Defining entities
First, we declare our regular Swift entities.
struct Message : Codable {
let content: String
}
⭐️ One of the main design decisions behind Graphiti is not to polute your entities declarations. This way you can bring your entities to any other solution with ease.
Defining the context
Second step is to create your application’s context. The context will be passed to all of your field resolver functions. This allows you to apply dependency injection to your API. This is the place where you can put code that talks to a database or another service.
The resolver functions also support NIO-style concurrency. To do so, just add one more parameter with type EventLoopGroup to the resolver function and change the return type to EventLoopFuture<YouReturnType>. Don’t forget to import NIO.
This library supports GraphQL subscriptions, and supports them through the Swift Concurrency AsyncThrowingStream type. See the Usage Guide for details.
If you are unable to use Swift Concurrency, you must create a concrete subclass of the EventStream class that implements event streaming
functionality. If you don’t feel like creating a subclass yourself, you can use the GraphQLRxSwift repository
to integrate RxSwift observables out-of-the-box. Or you can use that repository as a reference to connect a different
stream library like ReactiveSwift, OpenCombine, or
one that you’ve created yourself.
Additional Examples
For a progressive walkthrough, see the Usage Guide. The Star Wars API provides a fairly complete example.
Contributing
This repo uses SwiftFormat, and includes lint checks to enforce these formatting standards.
To format your code, install swiftformat and run:
swiftformat .
License
This project is released under the MIT license. See LICENSE for details.
Graphiti
Graphiti is a Swift library for building GraphQL schemas fast, safely and easily.
Looking for help? Find resources from the community.
Getting Started
An overview of GraphQL in general is available in the README for the Specification for GraphQL. That overview describes a simple set of GraphQL examples that exist as tests in this repository. A good way to get started with this repository is to walk through that README and the corresponding tests in parallel.
Using Graphiti
Add Graphiti to your
Package.swift
Graphiti provides two important capabilities: building a type schema, and serving queries against that type schema.
Defining entities
First, we declare our regular Swift entities.
⭐️ One of the main design decisions behind Graphiti is not to polute your entities declarations. This way you can bring your entities to any other solution with ease.
Defining the context
Second step is to create your application’s context. The context will be passed to all of your field resolver functions. This allows you to apply dependency injection to your API. This is the place where you can put code that talks to a database or another service.
⭐️ Notice again that this step doesn’t require Graphiti. It’s purely business logic.
Defining the GraphQL API resolver
Now that we have our entities and context we can create the GraphQL API resolver.
Defining the GraphQL API schema
Now we can finally define the GraphQL API with its schema.
Schemas may also be created in a modular way using
SchemaBuilder
:⭐️ Notice that
API
allows dependency injection. You could pass mocks ofresolver
andcontext
when testing, for example.Querying
To query the schema we need to pass in a NIO EventLoopGroup to feed the execute function alongside the query itself.
The output will be:
API.execute
returns aGraphQLResult
which adoptsEncodable
. You can use it with aJSONEncoder
to send the response back to the client using JSON.Async resolvers
Resolver functions can also be
async
:NIO resolvers
The resolver functions also support
NIO
-style concurrency. To do so, just add one more parameter with typeEventLoopGroup
to the resolver function and change the return type toEventLoopFuture<YouReturnType>
. Don’t forget to import NIO.Subscription
This library supports GraphQL subscriptions, and supports them through the Swift Concurrency
AsyncThrowingStream
type. See the Usage Guide for details.If you are unable to use Swift Concurrency, you must create a concrete subclass of the
EventStream
class that implements event streaming functionality. If you don’t feel like creating a subclass yourself, you can use the GraphQLRxSwift repository to integrate RxSwift observables out-of-the-box. Or you can use that repository as a reference to connect a different stream library like ReactiveSwift, OpenCombine, or one that you’ve created yourself.Additional Examples
For a progressive walkthrough, see the Usage Guide. The Star Wars API provides a fairly complete example.
Contributing
This repo uses SwiftFormat, and includes lint checks to enforce these formatting standards. To format your code, install
swiftformat
and run:License
This project is released under the MIT license. See LICENSE for details.