Created
September 15, 2020 19:00
-
-
Save Ch4s3/029d277e8af07bcd94a4c8de0563c8b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApi.Schema.Pipeline.MyCompilePhase do | |
@moduledoc """ | |
based on Absinthe phases https://hexdocs.pm/absinthe/1.5.3/Absinthe.Phase.html | |
""" | |
alias Absinthe.{Blueprint, Pipeline, Phase} | |
alias Absinthe.Blueprint.Schema.{ObjectTypeDefinition, FieldDefinition} | |
def pipeline(pipeline) do | |
Pipeline.insert_after(pipeline, Phase.Schema.TypeImports, __MODULE__) | |
end | |
def run(blueprint = %Blueprint{}, _) do | |
{:ok, Blueprint.prewalk(blueprint, &rename_library_types(&1))} | |
end | |
defp rename_library_types(struct = %ObjectTypeDefinition{module: _}) do | |
do_process(struct, Module.split(struct.module)) | |
end | |
defp rename_library_types(struct = %FieldDefinition{module: _}) do | |
do_process(struct, Module.split(struct.module)) | |
end | |
defp rename_library_types(struct), do: struct | |
defp do_process(struct, ["LibraryModule" | _]) do | |
identifier = struct.identifier |> Atom.to_string() | |
new_identifier = String.to_atom("prepend_name_" <> identifier) | |
new_name = "PrependName" <> struct.name | |
%{struct | identifier: new_identifier, name: new_name} | |
end | |
defp do_process(struct, _), do: struct | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment