Relay.swift is available as a SwiftPM package. You can add the package to an existing Xcode project using Xcode's built-in support for SwiftPM packages. If you want, you can follow along with the Getting Started guide by doing this in a new Single View SwiftUI app.
https://github.com/relay-tools/Relay.swift
for the package repository URL.Relay
and RelaySwiftUI
libraries to the target for your app.At this point, you should be able to use import Relay
and import RelaySwiftUI
in your Swift files without issue.
Relay.swift needs to generate Swift code for you based on the queries you use in your app. We use the official Relay compiler with a custom language plugin to do this. You can create a nearly empty npm package for your app, and then install the necessary dependencies into that package:
$ npm init -y
$ npm install graphql relay-config relay-compiler relay-compiler-language-swift
You only need to worry about installing these while developing your app. Any code generated by the Relay compiler should get committed into version control, so your app will be able to build without these dependencies.
Now you should be able to run the Relay compiler using npx
:
$ npx relay-compiler --help
Create Relay generated files
relay-compiler --schema <path> --src <path> [--watch]
Options:
...
We can configure the relay-compiler by creating a relay-config.js
file:
module.exports = {
src: ".",
schema: "schema.graphql",
language: "swift",
artifactDirectory: "./__generated__"
};
src
is the directory where all of your Swift sources are. These are the files that the Relay compiler will scan for GraphQL queries.
schema
is the path to your GraphQL schema file. You'll need a local copy of your schema for the compiler to generate code for your queries.
artifactDirectory
is the directory where the Relay compiler will generate Swift files. This directory should be designated exclusively for files generated by Relay, as the compiler will remove any files it didn't generate.
Now you should be able to run npx relay-compiler
. There won't be any files to generate yet. We'll get to those soon.
Next page: The Relay Environment