Skip to content

Commit

Permalink
Merge pull request #1214 from maartenvanvliet/add_opts_to_pipeline_mo…
Browse files Browse the repository at this point in the history
…difiers

Allow passthrough of opts to pipeline modifiers
  • Loading branch information
benwilson512 authored Apr 20, 2024
2 parents faebd88 + cbca63e commit f63b860
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- Bug Fix: [Validate field identifier uniqueness](https://github.com/absinthe-graphql/absinthe/pull/1200)
- Bug Fix: [Validate type references for invalid wrapped types](https://github.com/absinthe-graphql/absinthe/pull/1195)
- Bug Fix: Adds **optional fix** for non compliant built-in scalar Int type. `use Absinthe.Schema, use_spec_compliant_int_scalar: true` in your schema to use the fixed Int type. It is also advisable to upgrade for custom types if you are leveraging the use of integers outside the GraphQl standard. [#1131](https://github.com/absinthe-graphql/absinthe/pull/1131).
- Feature: [Support custom opts in schema pipeline modifiers](https://github.com/absinthe-graphql/absinthe/pull/1214)
- Feature: [Support error tuples when scalar parsing fails](https://github.com/absinthe-graphql/absinthe/pull/1187)
- Feature: [Convert SDL Language.\* structs to SDL notation](https://github.com/absinthe-graphql/absinthe/pull/1160)
- Feature: [Support passing the resolution struct to dataloader helper callbacks](https://github.com/absinthe-graphql/absinthe/pull/1211)
Expand Down
28 changes: 23 additions & 5 deletions lib/absinthe/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,31 @@ defmodule Absinthe.Schema do
end

@spec apply_modifiers(Absinthe.Pipeline.t(), t) :: Absinthe.Pipeline.t()
def apply_modifiers(pipeline, schema) do
def apply_modifiers(pipeline, schema, opts \\ []) do
Enum.reduce(schema.__absinthe_pipeline_modifiers__, pipeline, fn
{module, function}, pipeline ->
apply(module, function, [pipeline])
cond do
function_exported?(module, function, 1) ->
apply(module, function, [pipeline])

function_exported?(module, function, 2) ->
apply(module, function, [pipeline, opts])

true ->
pipeline
end

module, pipeline ->
module.pipeline(pipeline)
cond do
function_exported?(module, :pipeline, 1) ->
module.pipeline(pipeline)

function_exported?(module, :pipeline, 2) ->
module.pipeline(pipeline, opts)

true ->
pipeline
end
end)
end

Expand Down Expand Up @@ -640,12 +658,12 @@ defmodule Absinthe.Schema do
}"
"""
@spec to_sdl(schema :: t) :: String.t()
def to_sdl(schema) do
def to_sdl(schema, opts \\ []) do
pipeline =
schema
|> Absinthe.Pipeline.for_schema(prototype_schema: schema.__absinthe_prototype_schema__)
|> Absinthe.Pipeline.upto({Absinthe.Phase.Schema.Validation.Result, pass: :final})
|> apply_modifiers(schema)
|> apply_modifiers(schema, opts)

# we can be assertive here, since this same pipeline was already used to
# successfully compile the schema.
Expand Down
32 changes: 32 additions & 0 deletions test/absinthe/schema/sdl_render_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,36 @@ defmodule Absinthe.Schema.SdlRenderTest do
union SearchResult = Order | Category
"""
end

defmodule TestModifier do
def pipeline(pipeline, opts) do
send(self(), type: :module, opts: opts)
pipeline
end
end

defmodule ModifiedTestSchema do
use Absinthe.Schema

def custom_pipeline(pipeline, opts) do
send(self(), type: :function, opts: opts)
pipeline
end

@pipeline_modifier TestModifier
@pipeline_modifier {__MODULE__, :custom_pipeline}

@sdl """
type Query {
echo: String
}
"""
import_sdl @sdl
end

test "Render SDL takes opts" do
Absinthe.Schema.to_sdl(ModifiedTestSchema, sdl_render: true)
assert_received type: :function, opts: [sdl_render: true]
assert_received type: :module, opts: [sdl_render: true]
end
end

0 comments on commit f63b860

Please sign in to comment.