# Add Journeys to the schema you already have

This page shows how to serve Riddler Journeys from the Absinthe schema your
application already runs: the dependency, the two modules you name in
configuration, the key your plug puts the signed-in actor under, and the
imports that add the package's types and root fields beside your own. It uses
patron registration: a visitor gets a library card, and the Journey is that
one visitor's execution through the screens.

It assumes your host already implements riddler's two behaviours,
`Riddler.Journey.Host` and `Riddler.Journey.Access`. If it does not, start
from riddler's guide, [Present a Journey: a screen, waiting or
done](https://hexdocs.pm/riddler/present-a-journey.html), and the two
behaviours' documentation.

## Add the dependency

Add `riddler_graphql` to the dependencies in your `mix.exs`, at the version
the [README](https://hexdocs.pm/riddler_graphql/readme.html#installation)
pins. The package brings `riddler` and `absinthe` and nothing else. It serves no
request itself: your schema is served by the transport you already have.

## Name your two modules

The package calls your host module and your access module, and nothing else
of yours. Name them in your configuration:

```elixir
config :riddler_graphql,
  host: Library.RegistrationHost,
  access: Library.PatronAccess
```

`host` is your `Riddler.Journey.Host` module and `access` your
`Riddler.Journey.Access` module. Both are read when the package compiles, in
your build, so set them in `config/config.exs` (or a file it imports), not
in `config/runtime.exs`. A build with either one missing does not compile,
and the error names the missing key: a forgotten access module is a compile
error, never a Journey shown to everyone.

## Put the signed-in actor in the context

Every root field asks `Riddler.Journey.Access.check/4` whether the actor may
see the Journey, and reads the actor from the Absinthe context under
`:riddler_actor`. The package reads it from nowhere else and passes it to the
check as it finds it, so the actor is whatever your access module expects: a
patron id, a user struct.

Wherever your request pipeline builds the Absinthe context, add the actor
under that key. With `absinthe_plug`, a plug of your own before it might
read:

```elixir
defmodule LibraryWeb.Plugs.RiddlerActor do
  @behaviour Plug

  @impl true
  def init(opts), do: opts

  @impl true
  def call(conn, _opts) do
    Absinthe.Plug.put_options(conn, context: %{riddler_actor: conn.assigns[:patron_id]})
  end
end
```

If your pipeline already puts a context of its own, add the key to the map
it builds rather than setting a second one.

To read the actor under a key of your own, name it:

```elixir
config :riddler_graphql, actor_key: :current_patron
```

A request with no actor under the key is checked with `nil` as the actor,
and your access module decides what `nil` may see.

## Import the types and the root fields

Add five `import_types` lines, and the two objects of root fields with
`import_fields`: `:journey_queries` into your root query type and
`:journey_mutations` into your root mutation type, beside the fields you
already have.

```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
  end

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

    import_fields :journey_mutations
  end
end
```

That adds the `journey` query and the `startJourney`, `submitJourney` and
`goBack` mutations. Every type the package adds is named `Journey...`, and
its one scalar is `RiddlerJSON`, so it sits beside a `JSON` scalar, a
`Screen` type or a `Finding` type of your own. A type of your own named
`Journey...`, or a scalar named `RiddlerJSON`, collides: rename yours.

One request may select your fields and the package's together:

```graphql
query {
  book(isbn: "978-0-00-000000-0") { title }
  journey(id: "J-1001") {
    ... on JourneyScreen { screen { key title } elements moves { outcomes back } }
  }
}
```

The fields, their arguments and every type are in
`RiddlerGraphQL.Types.Journey` and `RiddlerGraphQL.Types.Answer`; the
package's whole schema, printed in the GraphQL schema definition language, is
`priv/riddler_graphql.graphql` in the repository at the release tag.

## What a client is refused

A Journey that does not exist and a Journey the actor may not see are
answered the same single error, so a refusal tells the asker nothing about
which Journeys exist:

```json
{
  "data": {"journey": null},
  "errors": [
    {
      "message": "no such journey",
      "path": ["journey"],
      "extensions": {"code": "NO_SUCH_JOURNEY"},
      "locations": [{"line": 1, "column": 9}]
    }
  ]
}
```

`startJourney` answers the same error for a Path your host does not have and
for a scope of the wrong kind, as your `start/2` does. A scope that
`Riddler.Journey.Scope.new/2` refuses, such as an empty `id`, is answered
`invalid journey scope`, with the code `INVALID_JOURNEY_SCOPE`, before
anything of yours is called.

A submit or a back that riddler or your host refuses with findings is not an
error: the payload's `answer` is null and its `findings` say why, each a
`JourneyFinding`.

## Present a screen again against a draft

The `journey` query takes an optional `draft`, the visitor's unsent
responses on the screen drawn, so a client can show a question whose
condition reads another response on the same screen before the visitor
submits. To serve it, name a function of yours that presents the screen
again against the draft:

```elixir
config :riddler_graphql,
  draft: {Library.RegistrationHost, :present_draft}
```

It is called with the Journey and the draft, after the access check, and
answers `{:ok, answer}` or `{:error, :no_such_journey}`. It steps nothing and
stores nothing. The response under the key of each secret question on the
presented screen is dropped from the draft before your function sees it. With no `draft`
configured, a request that sends one is answered `draft not supported`, with
the code `DRAFT_NOT_SUPPORTED`.
