-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto generate mutation and query code sample (#41)
* Rename root mutation type in SchemaTest * Move schema test fixture into function * Reduce duplication in test by using module attributes * Add markdown helper function to create GQL operation text block * Add test for more than one argument for operation text block * Handle operation with no arguments * Add gql code block generation to multi_page and single_page modules * Update markdown helper to print out fields that are returned * Gather and send operation details in single- and multi-page markdown modules * Use Enum.map_join/3 instead of Enum.map/2 with Enum.join/2 * Update check for empty list of fields * Add config.md back in * Add typespecs and doc for OperationDetailsHelpers functions * Change function name to graphql_operation_code_block * Add doc and spec for MarkdownHelpers.graphql_operation_code_block/1 * Add specs for functsion in MarkdownHelpers * Correct test definition * Remove Elixir 1.13 and 1.14 workflows --------- Co-authored-by: Clifton McIntosh <[email protected]>
- Loading branch information
1 parent
ee525d1
commit 1199a7c
Showing
9 changed files
with
743 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
## Local Development configuration | ||
|
||
This section describes the available configuration when working with Graphql Markdown | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
defmodule GraphqlMarkdown.OperationDetailsHelpers do | ||
@moduledoc """ | ||
A set of helpers to generate query and mutation details. | ||
""" | ||
|
||
alias GraphqlMarkdown.Schema | ||
|
||
@type argument :: %{ | ||
name: String.t(), | ||
type: String.t(), | ||
required: boolean() | ||
} | ||
|
||
@type field :: %{ | ||
name: String.t(), | ||
type: String.t() | ||
} | ||
|
||
@type return_type :: %{ | ||
name: String.t(), | ||
kind: String.t(), | ||
fields: [field()] | ||
} | ||
|
||
@type graphql_operation_details :: %{ | ||
operation_type: String.t(), | ||
operation_name: String.t(), | ||
arguments: [argument()], | ||
return_type: return_type() | ||
} | ||
|
||
@doc """ | ||
Creates a map with the details of a query or mutation. The details created include | ||
the operation type, operation name, arguments, and return type. | ||
""" | ||
@spec generate_operation_details(String.t(), map(), GraphqlMarkdown.Schema.t()) :: | ||
graphql_operation_details() | ||
def generate_operation_details(type, field, schema_details) do | ||
operation_type = | ||
case type do | ||
"queries" -> "query" | ||
"mutations" -> "mutation" | ||
end | ||
|
||
arguments = operation_arguments(field["args"]) | ||
|
||
operation_details = %{ | ||
operation_type: operation_type, | ||
operation_name: field["name"], | ||
arguments: arguments, | ||
return_type: return_fields(field["type"], schema_details) | ||
} | ||
|
||
operation_details | ||
end | ||
|
||
@spec operation_arguments([map()]) :: [argument()] | ||
defp operation_arguments(args) do | ||
Enum.map(args, fn arg -> | ||
arg_type = arg["type"] | ||
type = Schema.field_type(arg_type) | ||
required = arg_type["kind"] == "NON_NULL" | ||
|
||
%{ | ||
name: arg["name"], | ||
type: type, | ||
required: required | ||
} | ||
end) | ||
end | ||
|
||
@spec return_fields(map(), GraphqlMarkdown.Schema.t()) :: return_type() | ||
defp return_fields(%{"name" => name, "kind" => "OBJECT"}, schema_details) do | ||
object_fields = | ||
schema_details | ||
|> Map.get(:objects, []) | ||
|> Enum.find(fn object -> object["name"] == name 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: "OBJECT", | ||
fields: object_fields | ||
} | ||
end | ||
|
||
defp return_fields(return_type, _schema_details) do | ||
%{ | ||
name: return_type["name"], | ||
kind: return_type["kind"], | ||
fields: [] | ||
} | ||
end | ||
|
||
@spec return_field_type(map()) :: String.t() | ||
defp return_field_type(%{"type" => %{"kind" => "NON_NULL"}} = field) do | ||
get_in(field, ["type", "ofType", "kind"]) | ||
end | ||
|
||
defp return_field_type(field) do | ||
get_in(field, ["type", "kind"]) | ||
end | ||
end |
Oops, something went wrong.