fix: don't add graphql types if API doesn't compile

This commit is contained in:
Zach Daniel 2020-08-17 03:00:13 -04:00
parent f81f8d45cb
commit a7e4b165e4
No known key found for this signature in database
GPG key ID: C377365383138D4B

View file

@ -22,33 +22,39 @@ defmodule AshGraphql do
def run(blueprint, _opts) do
api = unquote(api)
Code.ensure_compiled(api)
blueprint_with_queries =
api
|> AshGraphql.Api.queries(__MODULE__)
|> Enum.reduce(blueprint, fn query, blueprint ->
Absinthe.Blueprint.add_field(blueprint, "RootQueryType", query)
end)
case Code.ensure_compiled(api) do
{:module, _} ->
blueprint_with_queries =
api
|> AshGraphql.Api.queries(__MODULE__)
|> Enum.reduce(blueprint, fn query, blueprint ->
Absinthe.Blueprint.add_field(blueprint, "RootQueryType", query)
end)
blueprint_with_mutations =
api
|> AshGraphql.Api.mutations(__MODULE__)
|> Enum.reduce(blueprint_with_queries, fn mutation, blueprint ->
Absinthe.Blueprint.add_field(blueprint, "RootMutationType", mutation)
end)
blueprint_with_mutations =
api
|> AshGraphql.Api.mutations(__MODULE__)
|> Enum.reduce(blueprint_with_queries, fn mutation, blueprint ->
Absinthe.Blueprint.add_field(blueprint, "RootMutationType", mutation)
end)
new_defs =
List.update_at(blueprint_with_mutations.schema_definitions, 0, fn schema_def ->
%{
schema_def
| type_definitions:
schema_def.type_definitions ++
AshGraphql.Api.type_definitions(api, __MODULE__)
}
end)
new_defs =
List.update_at(blueprint_with_mutations.schema_definitions, 0, fn schema_def ->
%{
schema_def
| type_definitions:
schema_def.type_definitions ++
AshGraphql.Api.type_definitions(api, __MODULE__)
}
end)
{:ok, %{blueprint_with_mutations | schema_definitions: new_defs}}
{:ok, %{blueprint_with_mutations | schema_definitions: new_defs}}
{:error, _} ->
# Something else will fail here, so we don't need to
{:ok, blueprint}
end
end
end