RiddlerGraphQL

RiddlerGraphQL gives a host Absinthe types and fields for serving 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:

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:

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:

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

Compatibility

RiddlerGraphQL needs Elixir 1.18 or later (elixir: "~> 1.18"). It has two runtime dependencies: riddler ~> 0.9, which presents each step of a Journey, and 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.