Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render improvements #780

Merged
merged 4 commits into from
Sep 19, 2019
Merged

Render improvements #780

merged 4 commits into from
Sep 19, 2019

Conversation

benwilson512
Copy link
Contributor

No description provided.


union :search_result do
types [:order, :category]
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@binaryseed This PR started with the need to handle the above type. Master blows up because in SDL union Foo = Bar | Baz uses type references with a name. In the case of a macro based schema however we have identifiers. Turning an identifier into a name reliably requires access to the entire type set so that you can lookup the type by identifier, and then use its name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I ran into this a few times when building this out.. I tried hard to avoid needing the entire list of types when rendering because that makes it difficult to enable rendering just an individual type's SDL representation via inspect.

It seems like you've got that working out fine though since it's passing tests :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing you handle rendering both Blueprint.TypeReference.Name and Blueprint.TypeReference.Identifier separately, nice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah what I did is basically fall back to doing what you had where we just camelize stuff and hope for the best if the type can't be found. This allows us to render simple fragments of a schema without needing the whole thing.

@@ -173,7 +168,7 @@ defmodule SdlRenderTest do
with {:ok, %{input: doc}} <- Absinthe.Phase.Parse.run(sdl),
%Absinthe.Language.Document{definitions: [node]} <- doc,
blueprint = Absinthe.Blueprint.Draft.convert(node, doc) do
inspect(blueprint, pretty: true)
Inspect.inspect(blueprint, %Inspect.Opts{pretty: true})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change ensures that if the schema cannot be rendered due to throwing an exception, that exception remains unhandled so that a proper stacktrace can be accessed.

@@ -72,7 +72,7 @@ defmodule Absinthe.Mixfile do
{:benchee, ">= 0.0.0", only: :dev},
{:dialyxir, "~> 1.0.0-rc.6", only: [:dev], runtime: false},
{:phoenix_pubsub, ">= 0.0.0", only: :test},
{:mix_test_watch, "~> 0.4.1", only: [:test, :dev]}
{:mix_test_watch, "~> 0.4.1", only: [:test]}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enabling this in :dev made it impossible to use recompile in iex -S mix within the absinthe project, since it would re-run the entire test suite each time you did it.


# For macro schema
defp render(scalar) when is_atom(scalar) do
scalar |> to_string |> Macro.camelize()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This inference was unsafe, since types can be named anything, the only safe way is to look it up from the type list.

end

defp render(nil, _) do
raise "got nil"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • handle render nil case better

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When did you encounter nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't, this was more make sure nil wasn't treated as a simple atom identifier and rendered as type Nil.

defp render(%Blueprint.Schema.UnionTypeDefinition{} = union_type, type_definitions) do
types =
Enum.map(union_type.types, fn
identifier when is_atom(identifier) ->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should do a pass in the absinthe code base and eliminate all bare atom identifiers within blueprints and standardize on %TypeReference.Name{} | %TypeReference.Identifier{}.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created an issue #783


union :search_result do
types [:order, :category]
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I ran into this a few times when building this out.. I tried hard to avoid needing the entire list of types when rendering because that makes it difficult to enable rendering just an individual type's SDL representation via inspect.

It seems like you've got that working out fine though since it's passing tests :)

@@ -544,6 +553,20 @@ defmodule Absinthe.Schema do
|> Enum.map(&lookup_directive(schema, &1))
end

def to_sdl(schema) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice function to have!

We should use it in Mix.Tasks.Absinthe.Schema.Sdl as well

https://github.com/absinthe-graphql/absinthe/blob/master/lib/mix/tasks/absinthe.schema.sdl.ex#L76-L82


union :search_result do
types [:order, :category]
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing you handle rendering both Blueprint.TypeReference.Name and Blueprint.TypeReference.Identifier separately, nice

end

defp render(nil, _) do
raise "got nil"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When did you encounter nil?

defp render(%Blueprint.Schema.UnionTypeDefinition{} = union_type, type_definitions) do
types =
Enum.map(union_type.types, fn
identifier when is_atom(identifier) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created an issue #783

@benwilson512 benwilson512 merged commit c9b46e7 into master Sep 19, 2019
@benwilson512 benwilson512 deleted the render-improvements branch September 19, 2019 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants