From cf531e9e52b1475960fee83bf497ac083d5c0844 Mon Sep 17 00:00:00 2001 From: Brad Schwartz Date: Mon, 26 Aug 2024 13:13:16 -0700 Subject: [PATCH] Add Support for Subscriptions (#53) * Add Support for Subscriptions My project leverages Graphql Subscriptions as the main way to move data. Seemed like it was mainly as easy as just adding the new key wherever query/mutation was already referenced * add to readme --- .gitignore | 1 + CHANGELOG.md | 6 ++++++ README.md | 4 +++- lib/graphql_markdown.ex | 2 ++ lib/graphql_markdown/multi_page.ex | 14 ++++++++++++-- .../operation_details_helpers.ex | 1 + lib/graphql_markdown/schema.ex | 17 ++++++++++++++++- lib/graphql_markdown/single_page.ex | 5 +++-- mix.exs | 2 +- test/fixtures/schema.json | 4 +++- test/graphql_markdown_test.exs | 2 ++ 11 files changed, 50 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 67bb114..cf3a2ca 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ guides/objects.md guides/queries.md guides/scalars.md guides/unions.md +guides/subscriptions.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 96b6b9b..b03ed68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.4.0 - 2024-08-26 + +### Added + +* Added support for Subscriptions + ## 0.3.1 - 2024-07-01 ### Fixed diff --git a/README.md b/README.md index 7167bb4..814682e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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" ] ... diff --git a/lib/graphql_markdown.ex b/lib/graphql_markdown.ex index ae3f6cc..8a29b5f 100644 --- a/lib/graphql_markdown.ex +++ b/lib/graphql_markdown.ex @@ -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 = @@ -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()), diff --git a/lib/graphql_markdown/multi_page.ex b/lib/graphql_markdown/multi_page.ex index 8fabe57..3a93451 100644 --- a/lib/graphql_markdown/multi_page.ex +++ b/lib/graphql_markdown/multi_page.ex @@ -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") @@ -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) diff --git a/lib/graphql_markdown/operation_details_helpers.ex b/lib/graphql_markdown/operation_details_helpers.ex index 90ed786..e988622 100644 --- a/lib/graphql_markdown/operation_details_helpers.ex +++ b/lib/graphql_markdown/operation_details_helpers.ex @@ -41,6 +41,7 @@ defmodule GraphqlMarkdown.OperationDetailsHelpers do case type do "queries" -> "query" "mutations" -> "mutation" + "subscriptions" -> "subscription" end arguments = operation_arguments(field["args"]) diff --git a/lib/graphql_markdown/schema.ex b/lib/graphql_markdown/schema.ex index c55d4d0..52406ec 100644 --- a/lib/graphql_markdown/schema.ex +++ b/lib/graphql_markdown/schema.ex @@ -8,6 +8,7 @@ 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()), @@ -15,7 +16,17 @@ defmodule GraphqlMarkdown.Schema do 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" @@ -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 diff --git a/lib/graphql_markdown/single_page.ex b/lib/graphql_markdown/single_page.ex index c73be5a..6940fdd 100644 --- a/lib/graphql_markdown/single_page.ex +++ b/lib/graphql_markdown/single_page.ex @@ -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) @@ -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() diff --git a/mix.exs b/mix.exs index 713fd02..3b1f6fa 100644 --- a/mix.exs +++ b/mix.exs @@ -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 [ diff --git a/test/fixtures/schema.json b/test/fixtures/schema.json index 1b4316f..37d8185 100644 --- a/test/fixtures/schema.json +++ b/test/fixtures/schema.json @@ -62,7 +62,9 @@ "queryType": { "name": "RootQueryType" }, - "subscriptionType": null, + "subscriptionType": { + "name": "RootSubscriptionType" + }, "types": [ { "description": "Represents a directive", diff --git a/test/graphql_markdown_test.exs b/test/graphql_markdown_test.exs index acb753b..8d19216 100644 --- a/test/graphql_markdown_test.exs +++ b/test/graphql_markdown_test.exs @@ -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", @@ -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",