diff --git a/lib/graphql_markdown/markdown_helpers.ex b/lib/graphql_markdown/markdown_helpers.ex index f926f91..8fc6a99 100644 --- a/lib/graphql_markdown/markdown_helpers.ex +++ b/lib/graphql_markdown/markdown_helpers.ex @@ -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 = diff --git a/lib/graphql_markdown/operation_details_helpers.ex b/lib/graphql_markdown/operation_details_helpers.ex index 6bea1d0..f5c7008 100644 --- a/lib/graphql_markdown/operation_details_helpers.ex +++ b/lib/graphql_markdown/operation_details_helpers.ex @@ -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"], diff --git a/test/fixtures/schema.json b/test/fixtures/schema.json index c00ae4a..16fc233 100644 --- a/test/fixtures/schema.json +++ b/test/fixtures/schema.json @@ -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, diff --git a/test/graphql_markdown/operation_details_helpers_test.exs b/test/graphql_markdown/operation_details_helpers_test.exs index bf1174e..80c307f 100644 --- a/test/graphql_markdown/operation_details_helpers_test.exs +++ b/test/graphql_markdown/operation_details_helpers_test.exs @@ -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" => [], @@ -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