# RiddlerGraphQL

RiddlerGraphQL gives a host Absinthe types and fields for serving
[Riddler](https://hex.pm/packages/riddler) Journeys over GraphQL. Riddler
answers what one visitor sees at one step of a Journey; this package
exposes that answer as GraphQL types and root fields a host imports into its
own existing schema. It defines no schema and no endpoint of its own.

## Why RiddlerGraphQL

A host that serves a Journey over GraphQL would otherwise write the same
types for every screen, question and finding, and keep them in step with
Riddler's vocabulary by hand. Putting those types in one package keeps a
patron-registration screen shaped the same wherever it is served, and keeps
the host to what is its own: the schema, the transport, who may see a Journey
and what happens when a visitor submits.

The package adds the `journey` query and the `startJourney`, `submitJourney`
and `goBack` mutations. Each one asks the host's access module before it
starts or steps a Journey or returns anything of one, and answers a Journey
that does not exist and one the actor may not see with the same single
error.

## Installation

Add `riddler_graphql` to the dependencies in your `mix.exs`:

```elixir
def deps do
  [
    {:riddler_graphql, "~> 0.1.0"}
  ]
end
```

> **Pre-1.0.** The public API is still moving, and a minor version may break
> it. Pinning to an exact minor - `~> X.Y.0` - is the recommended way to
> consume the package until 1.0.

## Basic usage

Name your `Riddler.Journey.Host` and `Riddler.Journey.Access` modules in
your configuration, `config :riddler_graphql, host: Library.RegistrationHost,
access: Library.PatronAccess`, and have your own plug put the signed-in
actor in the Absinthe context under `:riddler_actor`. Then import the
package into the schema you already have, beside your own types and fields:

```elixir
defmodule LibraryWeb.Schema do
  use Absinthe.Schema

  import_types LibraryWeb.Schema.CatalogueTypes

  import_types RiddlerGraphQL.Types.JSON
  import_types RiddlerGraphQL.Types.Answer
  import_types RiddlerGraphQL.Types.Finding
  import_types RiddlerGraphQL.Types.Scope
  import_types RiddlerGraphQL.Types.Journey

  query do
    field :book, :book do
      arg :isbn, non_null(:string)
      resolve &LibraryWeb.Resolvers.Catalogue.book/3
    end

    import_fields :journey_queries

    field :patron, :patron do
      resolve &LibraryWeb.Resolvers.Patrons.current/3
    end
  end

  mutation do
    import_fields :journey_mutations

    field :renew_loan, :loan do
      arg :loan, non_null(:id)
      resolve &LibraryWeb.Resolvers.Loans.renew/3
    end
  end
end
```

A client can now start a patron registration with `startJourney`, read where
it is with `journey`, and select the host's `book` or `patron` beside it in
the same request.

## The schema file

`priv/riddler_graphql.graphql` is the package's schema in the GraphQL schema
definition language: the types and root fields the package adds, printed
from a schema that imports them and nothing else, so it names no type of a
host's. The file at a release tag is the client contract: a client generates
its types from the file at the tag of the version its host depends on.

The file is regenerated from the package's types with:

```bash
MIX_ENV=test mix absinthe.schema.sdl --schema RiddlerGraphQL.Test.PackageSchema priv/riddler_graphql.graphql
```

The quality gate fails when the committed file and the types differ. Two
additions are not a break, and a later release may make either: an optional
argument on an existing field, and a nullable field on an existing type.

## Documentation

- Do
  - [Add Journeys to the schema you already have](https://hexdocs.pm/riddler_graphql/add-journeys-to-your-schema.html): the two modules, the actor key, the imports, and what a client is refused.
  - [Poll a waiting Journey on its retry hint](https://hexdocs.pm/riddler_graphql/poll-a-waiting-journey.html): read a waiting answer and ask again after `retryAfterMs`.
  - [Keep a secret answer out of your request logs](https://hexdocs.pm/riddler_graphql/keep-a-secret-out-of-your-logs.html): filter the variables your transport logs and Absinthe's own telemetry.
  - [Handle an element type your client does not know](https://hexdocs.pm/riddler_graphql/handle-an-element-type-you-do-not-know.html): do not draw it, and do not submit that screen.
- Look up
  - [`RiddlerGraphQL`](https://hexdocs.pm/riddler_graphql/RiddlerGraphQL.html): the configuration, the order of the access check, and every error.
  - [`RiddlerGraphQL.Types.Journey`](https://hexdocs.pm/riddler_graphql/RiddlerGraphQL.Types.Journey.html): the root fields, their arguments and the payload.
  - [`RiddlerGraphQL.Types.Answer`](https://hexdocs.pm/riddler_graphql/RiddlerGraphQL.Types.Answer.html): the screen, waiting and done types.
  - [The changelog](https://hexdocs.pm/riddler_graphql/changelog.html): what changed in each version.
  - [Riddler](https://hexdocs.pm/riddler): the package whose answers this one serves.
  - [Absinthe](https://hexdocs.pm/absinthe): the GraphQL engine a host's schema runs on.
- Understand
  - [The decision record](https://github.com/riddler/riddler_graphql/blob/main/docs/adr/0001-what-the-package-is.md): why the package is types a host imports, behind the host's access check, and calls nothing but the host.

## Compatibility

RiddlerGraphQL needs Elixir 1.18 or later (`elixir: "~> 1.18"`). It has two
runtime dependencies: [riddler](https://hex.pm/packages/riddler) `~> 0.9`,
which presents each step of a Journey, and
[absinthe](https://hex.pm/packages/absinthe) `~> 1.12`, the GraphQL engine.
Nothing in the statifier family is a dependency: a host wires its execution
engine to this package, never the reverse.

## License

MIT. See [LICENSE](https://github.com/riddler/riddler_graphql/blob/main/LICENSE).
