Skip to content

Commit

Permalink
Handle interface return types when providing GQL samples in markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Clifton McIntosh committed Jun 29, 2024
1 parent 5574be8 commit 754e951
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 2 deletions.
42 changes: 40 additions & 2 deletions lib/graphql_markdown/markdown_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,51 @@ defmodule GraphqlMarkdown.MarkdownHelpers do

return_values =
for possible_type <- possible_types do
"... on #{possible_type.name} {\n }"
"... on #{possible_type.name} {\n }"
end

case return_values do
[_ | _] ->
"\n __typename\n " <> Enum.join(return_values, "\n ")
_ -> ""

_ ->
""
end
end

defp returned_fields(%{kind: "INTERFACE"} = return_type) do
fields = Map.get(return_type, :fields, [])
possible_types = Map.get(return_type, :possible_types, [])

interface_fields =
for field <- fields do
if field.type == "OBJECT" do
"#{field.name} {\n }"
else
field.name
end
end

shared_fields_string =
case interface_fields do
[_ | _] -> "\n " <> Enum.join(interface_fields, "\n ")
_ -> ""
end

specific_types =
for possible_type <- possible_types do
"... on #{possible_type.name} {\n }"
end

specific_types_string =
case specific_types do
[_ | _] ->
"\n " <> Enum.join(specific_types, "\n ")

_ ->
""
end

shared_fields_string <> specific_types_string
end
end
179 changes: 179 additions & 0 deletions test/fixtures/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,158 @@
"name": "Boolean",
"possibleTypes": null
},
{
"description": "Information about a character",
"enumValues": null,
"fields": [
{
"args": [],
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "id",
"type": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
}
},
{
"args": [],
"deprecationReason": null,
"description": "The name",
"isDeprecated": false,
"name": "name",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
],
"inputFields": null,
"interfaces": [],
"kind": "INTERFACE",
"name": "Character",
"possibleTypes": [
{
"kind": "OBJECT",
"name": "Human",
"ofType": null
},
{
"kind": "OBJECT",
"name": "Droid",
"ofType": null
}
]
},
{
"description": null,
"enumValues": null,
"fields": [
{
"args": [],
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "id",
"type": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
}
},
{
"args": [],
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "name",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"args": [],
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "age",
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
}
}
],
"inputFields": null,
"interfaces": [
{
"kind": "INTERFACE",
"name": "Character",
"ofType": null
}
],
"kind": "OBJECT",
"name": "Human",
"possibleTypes": null
},
{
"description": null,
"enumValues": null,
"fields": [
{
"args": [],
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "id",
"type": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
}
},
{
"args": [],
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "name",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"args": [],
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "primaryFunction",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
],
"inputFields": null,
"interfaces": [
{
"kind": "INTERFACE",
"name": "Character",
"ofType": null
}
],
"kind": "OBJECT",
"name": "Droid",
"possibleTypes": null
},
{
"description": null,
"enumValues": null,
Expand Down Expand Up @@ -1393,6 +1545,33 @@
"name": "UserSsoDetails",
"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": null,
"isDeprecated": false,
"name": "heroForEpisode",
"type": {
"kind": "INTERFACE",
"name": "Character",
"ofType": null
}
}
],
"inputFields": null,
Expand Down

0 comments on commit 754e951

Please sign in to comment.