Skip to content

Commit

Permalink
create docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rudebono committed May 14, 2024
1 parent a2759c9 commit e37dad9
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
95 changes: 94 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit e37dad9

Please sign in to comment.