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

Add Support for Subscriptions #53

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ guides/objects.md
guides/queries.md
guides/scalars.md
guides/unions.md
guides/subscriptions.md
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.4.0 - 2024-08-26

### Added

* Added support for Subscriptions

## 0.3.1 - 2024-07-01

### Fixed
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ it will generate the following files in your current folder:

./queries.md
./mutations.md
./subscriptions.md
./objects.md
./inputs.md
./enums.md
Expand All @@ -85,7 +86,8 @@ defmodule Azeroth.MixProject do
"guides/graphql/objects.md",
"guides/graphql/queries.md",
"guides/graphql/scalars.md",
"guides/graphql/unions.md"
"guides/graphql/unions.md",
"guides/graphql/subscriptions.md"
]
...

Expand Down
2 changes: 2 additions & 0 deletions lib/graphql_markdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule GraphqlMarkdown do
defp render_schema(schema, options) do
query_type_name = Schema.query_type(schema)
mutation_type_name = Schema.mutation_type(schema)
subscription_type_name = Schema.subscription_type(schema)
types = Schema.types(schema)

filtered_types =
Expand All @@ -37,6 +38,7 @@ defmodule GraphqlMarkdown do
schema_details = %Schema{
mutations: List.first(Schema.find_and_sort_type(types, "name", mutation_type_name)),
queries: List.first(Schema.find_and_sort_type(types, "name", query_type_name)),
subscriptions: List.first(Schema.find_and_sort_type(types, "name", subscription_type_name)),
inputs: Schema.find_and_sort_type(filtered_types, "kind", Schema.input_kind()),
objects: Schema.find_and_sort_type(filtered_types, "kind", Schema.object_kind()),
scalars: Schema.find_and_sort_type(filtered_types, "kind", Schema.scalar_kind()),
Expand Down
14 changes: 12 additions & 2 deletions lib/graphql_markdown/multi_page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ defmodule GraphqlMarkdown.MultiPage do
output_dir = Keyword.get(options, :output_dir, ".")

Enum.map(
["queries", "mutations", "objects", "inputs", "enums", "scalars", "interfaces", "unions"],
[
"queries",
"mutations",
"objects",
"subscriptions",
"inputs",
"enums",
"scalars",
"interfaces",
"unions"
],
fn section ->
filename = Path.join(output_dir, "#{section}.md")

Expand Down Expand Up @@ -49,7 +59,7 @@ defmodule GraphqlMarkdown.MultiPage do
end

def generate_section(type, %{"fields" => fields} = _details, schema_details)
when type in ["queries", "mutations"] do
when type in ["queries", "mutations", "subscriptions"] do
Enum.each(fields, fn field ->
render(type, MarkdownHelpers.header(field["name"], 2))
render_newline(type)
Expand Down
1 change: 1 addition & 0 deletions lib/graphql_markdown/operation_details_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule GraphqlMarkdown.OperationDetailsHelpers do
case type do
"queries" -> "query"
"mutations" -> "mutation"
"subscriptions" -> "subscription"
end

arguments = operation_arguments(field["args"])
Expand Down
17 changes: 16 additions & 1 deletion lib/graphql_markdown/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@ defmodule GraphqlMarkdown.Schema do
@type t :: %__MODULE__{
mutations: list(map()),
queries: list(map()),
subscriptions: list(map()),
inputs: list(map()),
objects: list(map()),
enums: list(map()),
scalars: list(map()),
interfaces: list(map()),
unions: list(map())
}
defstruct [:mutations, :queries, :inputs, :objects, :enums, :scalars, :interfaces, :unions]
defstruct [
:mutations,
:queries,
:subscriptions,
:inputs,
:objects,
:enums,
:scalars,
:interfaces,
:unions
]

@object_kind "OBJECT"
@input_kind "INPUT_OBJECT"
Expand Down Expand Up @@ -95,6 +106,10 @@ defmodule GraphqlMarkdown.Schema do
schema["queryType"]["name"]
end

def subscription_type(schema) do
schema["subscriptionType"]["name"]
end

def types(schema) do
Enum.filter(schema["types"], fn type -> !String.starts_with?(type["name"], "__") end)
end
Expand Down
5 changes: 3 additions & 2 deletions lib/graphql_markdown/single_page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule GraphqlMarkdown.SinglePage do
defp generate_toc(schema_details, _options) do
print_toc_type("queries", schema_details.queries)
print_toc_type("mutations", schema_details.mutations)
print_toc_type("subscriptions", schema_details.subscriptions)
print_toc_type("objects", schema_details.objects)
print_toc_type("inputs", schema_details.inputs)
print_toc_type("enums", schema_details.enums)
Expand Down Expand Up @@ -117,9 +118,9 @@ defmodule GraphqlMarkdown.SinglePage do
render("None")
end

# Handles Mutations and Queries
# Handles Mutations and Queries and Subscriptions
def generate_section(type, %{"fields" => fields} = _details, schema_details)
when type in ["queries", "mutations"] do
when type in ["queries", "mutations", "subscriptions"] do
render(MarkdownHelpers.header(type, 2, true))
render_newline()

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule GraphqlMarkdown.MixProject do
use Mix.Project

@project_url "https://github.com/podium/graphql_markdown"
@version "0.3.1"
@version "0.4.0"

def project do
[
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
"queryType": {
"name": "RootQueryType"
},
"subscriptionType": null,
"subscriptionType": {
"name": "RootSubscriptionType"
},
"types": [
{
"description": "Represents a directive",
Expand Down
2 changes: 2 additions & 0 deletions test/graphql_markdown_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ defmodule GraphqlMarkdownTest do
"guides/queries.md",
"guides/mutations.md",
"guides/objects.md",
"guides/subscriptions.md",
"guides/inputs.md",
"guides/enums.md",
"guides/scalars.md",
Expand All @@ -69,6 +70,7 @@ defmodule GraphqlMarkdownTest do
"guides/queries.md",
"guides/mutations.md",
"guides/objects.md",
"guides/subscriptions.md",
"guides/inputs.md",
"guides/enums.md",
"guides/scalars.md",
Expand Down
Loading