From e37dad95af35db67eb8575eb6c026b4c9a1061fe Mon Sep 17 00:00:00 2001 From: Hany Rock Date: Wed, 15 May 2024 00:11:22 +0900 Subject: [PATCH] create docs --- LICENSE | 21 ++++++++++++ README.md | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e67cdc2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 rudebono + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 859a499..fd7eeb9 100644 --- a/README.md +++ b/README.md @@ -1 +1,94 @@ -# AbsintheOneOf +# Absinthe one_of + +`:absinthe_one_of` helps you create a GraphQL Input Union Type using Absinthe, allowing only one field to be non-null among several options. + +## Installation + +Add `:absinthe_one_of` to your list of dependencies in `mix.exs`: + +```elixir +def deps() do + [ + {:absinthe_one_of, "~> 0.1.0"} + ] +end +``` + +## Usage + +### Step 1: Define Schema + +Create a schema module and define your types, including the `@prototype_schema AbsintheOneOf.Directive`: + +```elixir +defmodule YourWebApp.Schema do + use Absinthe.Schema + + @prototype_schema AbsintheOneOf.Directive + + query do + ... + end + + mutation do + ... + end + + subscription do + ... + end + + ... + + input_object(:one_of_input) do + directive(:one_of) + field(:a, :a_input) + field(:b, :b_input) + end + + input_object(:a_input) do + ... + end + + input_object(:b_input) do + ... + end +end +``` + +### Step 2: Setup Absinthe Pipeline + +Configure the Absinthe pipeline to include the `absinthe_one_of` phase: + +```elixir +defmodule YourWebApp.Endpoint do + use YourWebApp, :router + + ... + + def pipeline(config, pipeline_opts) do + config.schema_mod + |> Absinthe.Pipeline.for_document(pipeline_opts) + |> Absinthe.Pipeline.insert_after( + Absinthe.Phase.Document.Validation.OnlyOneSubscription, + AbsintheOneOf.Phase + ) + end +end +``` + +### Step 3: Testing + +To write tests for your implementation, you can refer to the example tests provided in [test/absinthe_one_of_test.exs](test/absinthe_one_of_test.exs). These tests demonstrate how to validate the functionality of the one_of directive and ensure that only one field is non-null among several options. + + +## Inspiration + +This package was inspired by an article written by Maarten Van Vliet. + +You can read the original article [Creating an Input Union Type System Directive in Absinthe](https://maartenvanvliet.nl/2022/04/28/absinthe_input_union/). + + +## License + +This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.