Skip to content

Commit

Permalink
Handle simple list return
Browse files Browse the repository at this point in the history
  • Loading branch information
Clifton McIntosh committed Jun 30, 2024
1 parent 754e951 commit 76fbd01
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/graphql_markdown/markdown_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ defmodule GraphqlMarkdown.MarkdownHelpers do
@spec returned_fields(OperationDetailsHelpers.return_type()) :: String.t()
defp returned_fields(%{kind: "SCALAR"}), do: ""

defp returned_fields(%{kind: "OBJECT"} = return_type) do
defp returned_fields(%{kind: kind} = return_type) when kind in ~w(OBJECT LIST) do
fields = Map.get(return_type, :fields, [])

return_values =
Expand Down
24 changes: 24 additions & 0 deletions lib/graphql_markdown/operation_details_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,30 @@ defmodule GraphqlMarkdown.OperationDetailsHelpers do
}
end

defp return_fields(
%{"name" => name, "kind" => "LIST", "ofType" => %{"kind" => "OBJECT"}} = return_field,
schema_details
) do
name_of_list_type = get_in(return_field, ["ofType", "name"])

fields =
schema_details
|> Map.get(:objects, [])
|> Enum.find(fn object -> object["name"] == name_of_list_type end)
|> Map.get("fields", [])
|> Enum.map(fn field ->
field_name = field["name"]
type = return_field_type(field)
%{name: field_name, type: type}
end)

%{
name: name,
kind: "LIST",
fields: fields
}
end

defp return_fields(return_type, _schema_details) do
%{
name: return_type["name"],
Expand Down
31 changes: 31 additions & 0 deletions test/fixtures/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,37 @@
"name": "Character",
"ofType": null
}
},
{
"args": [
{
"defaultValue": null,
"description": null,
"name": "episode",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
}
],
"deprecationReason": null,
"description": "Get droids for episode",
"isDeprecated": false,
"name": "droidsInEpisode",
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "Droid",
"ofType": null
}
}
}
],
"inputFields": null,
Expand Down
48 changes: 47 additions & 1 deletion test/graphql_markdown/operation_details_helpers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,35 @@ defmodule GraphqlMarkdown.OperationDetailsHelpersTest do
"type" => %{"kind" => "INTERFACE", "name" => "Character", "ofType" => nil}
}

@droids_in_episode_query %{
"args" => [
%{
"defaultValue" => nil,
"description" => nil,
"name" => "episode",
"type" => %{
"kind" => "NON_NULL",
"name" => nil,
"ofType" => %{"kind" => "SCALAR", "name" => "String", "ofType" => nil}
}
}
],
"deprecationReason" => nil,
"description" => "Get disputes",
"isDeprecated" => false,
"name" => "droidsInEpisode",
"type" => %{
"kind" => "LIST",
"name" => nil,
"ofType" => %{"kind" => "OBJECT", "name" => "Droid", "ofType" => nil}
}
}

@root_query %{
"fields" => [
@user_sso_details_query,
@hero_for_episode_query
@hero_for_episode_query,
@droids_in_episode_query
],
"inputFields" => nil,
"interfaces" => [],
Expand Down Expand Up @@ -520,5 +545,26 @@ defmodule GraphqlMarkdown.OperationDetailsHelpersTest do

assert operation_details.return_type == expected_return_type
end

test "returns the return type for an operation that has a list as its return type" do
expected_return_type = %{
fields: [
%{name: "id", type: "SCALAR"},
%{name: "name", type: "SCALAR"},
%{name: "primaryFunction", type: "SCALAR"}
],
kind: "LIST",
name: nil
}

operation_details =
OperationDetailsHelpers.generate_operation_details(
"queries",
@droids_in_episode_query,
@schema_details
)

assert operation_details.return_type == expected_return_type
end
end
end

0 comments on commit 76fbd01

Please sign in to comment.