Recipes
The compiler's command line
batonc: the command's contract for a build outside SwiftPM.
batonc is the compiler the SwiftPM plugin runs. Each release publishes
one binary per platform, in the artifact bundle: a universal macOS binary,
and a static Linux binary for each of x86_64 and aarch64. Each is
listed in the bundle's info.json under the triples a host reports, so
SwiftPM on either platform, and a Bazel toolchain, select theirs from the
one file. scripts/build-compiler.sh builds the host's binary from
compiler/ in a checkout. Its command line is the contract a build
outside SwiftPM uses: Bazel, a committed-output step, an editor's check.
What follows is that contract. The output is deterministic: the same
schema, configuration, and sources produce the same bytes, and the
goldens under compiler/src/tests fix those bytes.
Commands#
batonc generate --config <baton.json> [--language swift|kotlin] (--out <dir> | --emit <source>=<output>...) [--shared <file>] [--report <file>] [--fragments <file>] [--module <name>] [--persisted <file>] [--base <file>...] [--check] <files...>
batonc validate --config <baton.json> [--language swift|kotlin] [--base <file>...] <files...>
batonc print <OperationName> --config <baton.json> [--language swift|kotlin] [--base <file>...] <files...>An argument @<file> is replaced by the arguments the file holds, one a
line: a build system's params file, since the bases of a target at the
top of an app outgrow a command line.
--schema <file or directory> may replace --config when there is no
configuration. It may repeat, and a directory stands for its .graphql
files in name order; the files of every --schema, in the order given,
are the schema together, so a build system that declares each input
names each file. Beside a configuration, it overrides that
configuration's schema or schemaDir, for a schema that is a build's
own output. Otherwise the configuration names the schema, relative to the
configuration's directory: schema one file, or Relay's schemaDir a
directory whose .graphql files, in name order, are the schema. It names
one of the two; both, or a schemaDir that is not a directory, is an
error. Its schemaExtensions and, unless --persisted names it, the
persisted documents file are relative to the same directory.
The schema's files are a set: every definition is built before every
extension, so the order the files are given in does not matter, and the
schema's digest, which versions the image, is the same for the same files
in any order and under any names, since the compiler sorts their texts
before hashing; a schema of one file keeps the digest it had.
Where a file is listed says whose its fields are. A file of the schema
describes what the transport answers, the server's SDL and, beside it,
the fields another backend answers through the app's own transport
(a backend that is not GraphQL); an extend in it
adds server fields, and may come before the type it extends. A file
under schemaExtensions declares client fields. extend schema,
extend input and extend scalar are errors at their line in either
list, since Relay's front end has no code for them: write what they add
in the definition they extend.
<files...> are the target's host sources, Swift or Kotlin, and its
.graphql or .gql documents. The compiler reads the GraphQL out of the
markers (@Fragment, @Query, @Mutation, @Subscription, bare or
qualified as @Baton.Query in Swift and @baton.Query in Kotlin) and
compiles every document of the target together, since a fragment spread
in one file is declared in another.
The language#
A run writes one language, which --language names as swift or
kotlin. Without that flag, the first host file's extension does:
.swift or .kt. A run of .graphql files alone writes Swift, and a
host file of the other language is an error at that file. Run batonc
once per language.
The Kotlin target writes the operation values with their plans, and the
shared file. A fragment's lens, the fields of an operation's Data, and
a mutation's optimistic builder come with the Kotlin runtime's readers.
A Kotlin marker takes one string literal, labelled document = or not, in
any of Kotlin's forms: "…" with its escapes, a raw """…""", or either
after a run of dollars, $$"""…""". In that last form, a run of fewer
dollars than the prefix is text. A GraphQL variable is a template in a
string without enough dollars, so write a document with a variable in a
$$ string. A template is an error at the marker. A raw string is read
dedented: its common indentation is removed, and a blank first and last
line are dropped, so it equals the .graphql file an author would write.
A bare @Query in a file that imports another library's Query, Room's
or Retrofit's, is that library's.
The Kotlin target reads two entries of baton.json:
"kotlin": {"package": "<package>"}: the package of the shared file and of the code written for a.graphqlsource. A.kthost's code takes the host's ownpackage. The entry is required when the run writes the shared file or compiles a.graphqlsource.- A mapped scalar's
kotlinentry undercustomScalarTypes. That entry is the Kotlin type the scalar reads as, and theobjectimplementing the runtime'sScalarConverterfor it:"Decimal": {"swift": "Foundation.Decimal", "kotlin": {"type": "java.math.BigDecimal", "converter": "app.Decimals"}}. A mapped scalar without one is an error at the configuration in a Kotlin run, as one without a Swift type is in a Swift run.
generate#
generate writes, and it writes nothing when a document has an error:
- One output per source that declares GraphQL, named under
--outby the source's path relative to the working directory. Each separator becomes an underscore, and.baton.swiftstands in place of.swift(Sources/App/Screen.swiftwritesSources_App_Screen.baton.swift). In a Kotlin run,.baton.ktstands in place of.kt, or the file is written exactly where--emit <source>=<output>says. A source an--emitnames that holds no document gets a header only, whether or not it names a marker, so a build system declares one output per source without reading the source. Under--out, an output of the run's language in the directory that this run did not write is removed, so a renamed source leaves nothing behind. - The shared file,
Baton.baton.swift, orBaton.baton.ktin a Kotlin run, under--outor--shared <file>, holds the types every output names, the schema's digest, and the format marker. - The persisted documents file, under
persistConfig, goes where--persisted <file>says, since a build system declares its outputs before it reads the configuration. Otherwise it goes under--outwhen given, and otherwise beside the configuration.--persistedwithoutpersistConfigis an error. - The report,
--report <file>: what the target compiled, as JSON, each lens with its accessors' names, keys, and shapes. See Report.--module <name>names the Swift module the generated code is compiled in, written into the report's head, so a target that spreads this one's fragments imports their lenses from it. A Kotlin run takes no--module, which is a usage error there: its head'smoduleis the package of the shared file, thekotlin.packageof the configuration. - The fragments file,
--fragments <file>: the report's head and, of each fragment the target declares, its name, type condition, source, definition and, under Kotlin, package. It is what a dependent target reads of this one as its base, and it changes only when a fragment does, so a build that hands dependents this file and not the report reruns a dependent only when a base's fragments change, not with each edit to one of this target's operations.
--check writes nothing. Every output is computed and compared with the
file at its path, and each stale or missing one is named on stderr, with
exit code 1 when any is. A team that commits its generated code runs
generate as a developer step and generate --check in CI.
validate#
validate runs the same compilation and writes no output. You get
diagnostics and an exit code, for an editor or a pre-commit hook.
print#
print runs the same compilation, printing one operation's text. That
text is the exact text the app sends, compact on one line as the compiler
prints it, and preceded by # documentId: <id> under persistConfig.
Paste it into a server's tool, which formats it.
Bases#
A target spreads the fragments of another target, its
base, when each --base <file> names a
base's report or fragments file. generate, validate, print and
--check take it alike, since they are the same compilation. Give the
files of the whole closure: an operation's text prints every fragment it
reaches, however deep its base. Only the base fragments the target's
documents reach, directly or through another base fragment, join the
compilation; each is validated with the target's documents, inlined into
its operations' plans and printed in their texts, so an operation's id
covers it. The target generates no lens for a base fragment and no
refetch query from it: a spread returns the base's lens, named bare, as
the target's own are. In Swift each generated file of the target imports
each such base's module as @_spi(Generated) import <Module>; in Kotlin
a file imports a base's lens from the package the base's file gives. An
enum or an input object that only a base fragment names is the base's
and is not generated again. A base's fragments are not listed in the
target's report, and an operation's fragments names them all the same.
A base generates a lens's isPresent and fieldErrors only where its
own documents defer or catch the fragment, so when the target defers a
base fragment, catches it, or reads the errors of a base @inline value
inside one of its own, it generates those checks itself, from the
fragment's definition, in its shared file under Bases, and the spread
reads them there. What the definition itself settles, satisfied and
the throwing read under @throwOnFieldError, is still read from the
base's lens.
See the decision.
A base raises these errors:
- A base of another schema's digest, at the configuration, naming the
base. The digest covers the schema's files, the client extensions,
identityandtransient. - A base's file of another
format, or a file that is not a report or a fragments file, at the base's file. - A fragment the target declares and a base declares too, at the target's fragment; two bases that declare one name, naming both. A fragment's name is unique among a target and its bases.
- A base fragment that spreads a fragment no base given declares, naming it: pass the base of the target that declares it.
- In Swift, a base fragment reached through a base whose file names no
module, naming
--module. - In Kotlin, a base whose shared file is in the target's package, at the
configuration, naming the base and the package; two bases whose shared
files are in one package, at the second base's file, naming both. The
shared file's objects are public and named alike in every target, so
an app cannot hold two in one package: name another
kotlin.packagefor one of them. Every base given is checked, reached or not.
Diagnostics and exit codes#
Diagnostics are printed to stderr as
path:line:column: severity: message, with the position inside the
GraphQL text in the host file. Xcode, Gradle, and Bazel show them at the
line. A warning does not fail the command.
A fragment of the target that no operation of the target reaches,
directly or through another fragment, is a warning at its definition:
"fragment X is spread by no operation of this target; nothing in it
reads the fragment". A target that lists this one as a base may spread
it, which this compilation cannot see. A base's fragments are not warned
of.
| Exit | Meaning |
|---|---|
| 0 | Done; warnings may have been printed. |
| 1 | A document has an error, an output is stale under --check, or a file could not be read or written; the reason is on stderr. |
| 2 | No command, or one batonc does not have. |
Under the SwiftPM plugin#
The plugin runs one generate per target, with --out in its own output
directory, --shared, --report, and an --emit for each Swift source
that names a marker. The generated Swift joins the target's sources,
while the report and the persisted documents file sit beside them,
undeclared, so SwiftPM does not bundle them as resources. baton.json
is read from the target's directory, then the package root.
Outside SwiftPM: committed output#
batonc generate --config Sources/App/baton.json --out Sources/App/Generated Sources/App/*.swiftCommit Sources/App/Generated, add it to the target's sources, and gate
it with --check:
batonc generate --config Sources/App/baton.json --out Sources/App/Generated --check Sources/App/*.swiftOutside SwiftPM: Bazel#
rules_baton, under bazel/ in this repository, is this command as a
Bazel rule, and the release's bundle as a toolchain. Every input is
declared, there is one output per source, and the report, the fragments
file and the persisted documents file are output groups. A target's
deps are its bases, passed as --base. A test runs over --check for
committed output. Bazel is its page.
Baton