# `RiddlerGraphQL`
[🔗](https://github.com/riddler/riddler_graphql/blob/v0.1.0/lib/riddler_graphql.ex#L1)

Absinthe types and fields for serving Riddler Journeys over GraphQL.

A host that already runs its own Absinthe schema will import what this
package defines into that schema, and the types and fields then answer what
Riddler presents at one step of a Journey. The package never defines a
schema or an endpoint of its own: the schema, the transport and who may see
a Journey stay the host's.

The package depends on `riddler` and `absinthe` only.

It defines the `RiddlerJSON` scalar (`RiddlerGraphQL.Types.JSON`) and the
`JourneyAnswer` union of a screen, a waiting and a done state
(`RiddlerGraphQL.Types.Answer`), which a host brings into its schema with
`import_types`.

The root fields are two objects in `RiddlerGraphQL.Types.Journey`:
`:journey_queries`, the `journey` query, and `:journey_mutations`, the
`startJourney`, `submitJourney` and `goBack` mutations, each answering a
`JourneyPayload`. A host imports that module's types beside the finding and
scope types (`RiddlerGraphQL.Types.Finding`, `RiddlerGraphQL.Types.Scope`),
then each object into its own root query or root mutation type with
`import_fields`, next to the fields it already has.

## Configuration

The host names its `Riddler.Journey.Host` module and its
`Riddler.Journey.Access` module in its own configuration, and may name the
key its actor is read under:

    config :riddler_graphql,
      host: Library.RegistrationHost,
      access: Library.PatronAccess,
      actor_key: :riddler_actor

The three are read when the package compiles, in the host's build. A
build with no `:host` or no `:access` configured does not compile, and the
error names the missing key. `:actor_key` is `:riddler_actor` when the host
names none: the host's own plug puts its signed-in actor in the Absinthe
context under that key, and the package reads it from nowhere else and
passes it to `Riddler.Journey.Access.check/4` without reading it.

The host may also name a draft function, read at compile time beside the
other three:

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

The `journey` query's optional `draft` argument carries a visitor's unsent
responses on the screen drawn, as an object keyed by element key. The
resolver calls `present/1`, asks `Riddler.Journey.Access.check/4` with
`:present`, and then calls the function with the Journey and the draft;
the function answers `{:ok, answer}`, the screen presented again against
the draft, or `{:error, :no_such_journey}`. Nothing is stepped and nothing
is stored. A response under the key of a secret question on the screen is
dropped from the draft before the function sees it. A Journey that is
waiting or done is answered as the read without a draft, and the function
is not called. With no `:draft` configured, a draft is answered the error
`draft not supported`, code `DRAFT_NOT_SUPPORTED`, after the check passes;
a draft that is not an object is answered `draft is not an object`. A
`:draft` that is not a `{module, function_name}` pair stops the build, as
a missing module does.

Every root field asks `Riddler.Journey.Access.check/4` before it calls a
host callback that starts or steps a Journey, and before it returns
anything of an answer. `present/1` is the one callback called before the
check, for the Journey's scope; `startJourney` checks the scope the client
names. A refused check, and a callback answering
`{:error, :no_such_journey}`, answer the same GraphQL error, message
`no such journey` with the code `NO_SUCH_JOURNEY` in its extensions, for a
Journey that does not exist and for one the actor may not see. A scope
`Riddler.Journey.Scope.new/2` refuses answers `invalid journey scope`, code
`INVALID_JOURNEY_SCOPE`, before anything of the host's is called. A submit
and a back request are decoded by `Riddler.Journey.decode_submit/1` after
the check and before the host sees them; its findings come back in the
payload's `findings`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
