diff --git a/config/config.exs b/config/config.exs index b555247..50c5625 100644 --- a/config/config.exs +++ b/config/config.exs @@ -33,7 +33,7 @@ config :logger, level: :info # Example # config :graphql_relay, # schema_module: GraphQL.Schema.Root, -# schema_json_path: "#{Path.dirname(__DIR__)}/priv/repo/graphql" +# schema_json_path: "#{Path.dirname(__DIR__)}/priv/graphql" # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. diff --git a/examples/todo/config/config.exs b/examples/todo/config/config.exs index dffd69f..4c61ef5 100644 --- a/examples/todo/config/config.exs +++ b/examples/todo/config/config.exs @@ -30,4 +30,4 @@ config :phoenix, :generators, config :graphql_relay, schema_module: Todo.GraphQL.Schema.Root, - schema_json_path: "#{Path.dirname(__DIR__)}/priv/repo/graphql" + schema_json_path: "#{Path.dirname(__DIR__)}/priv/graphql" diff --git a/examples/todo/package.json b/examples/todo/package.json index d11e811..249dbb5 100644 --- a/examples/todo/package.json +++ b/examples/todo/package.json @@ -29,7 +29,7 @@ }, "metadata": { "graphql": { - "schema": "./priv/repo/graphql/schema.json" + "schema": "./priv/graphql/schema.json" } } } diff --git a/lib/graphql_relay.ex b/lib/graphql_relay.ex index 3d383f2..90ab730 100644 --- a/lib/graphql_relay.ex +++ b/lib/graphql_relay.ex @@ -20,10 +20,10 @@ defmodule GraphQL.Relay do data_dir = Application.fetch_env!(:graphql_relay, :schema_json_path) File.mkdir_p!(data_dir) - File.write!(Path.join(data_dir, "schema.json"), introspection) + File.write!(Path.join(data_dir, "schema.json"), introspect) end - defp introspection do + def introspect do schema_module = Application.fetch_env!(:graphql_relay, :schema_module) {_, data} = GraphQL.execute(apply(schema_module, :schema, []), GraphQL.Type.Introspection.query) Poison.encode!(data, pretty: true) diff --git a/lib/mix/tasks/graphql.gen.schema.ex b/lib/mix/tasks/graphql.gen.schema.ex new file mode 100644 index 0000000..063c90a --- /dev/null +++ b/lib/mix/tasks/graphql.gen.schema.ex @@ -0,0 +1,45 @@ +defmodule Mix.Tasks.Graphql.Gen.SchemaJson do + use Mix.Task + + @shortdoc "Generates a `schema.json` file" + + @moduledoc """ + Creates/updates the GraphQL `schema.json` file. + + mix graphql.gen.schema_json --build brunch + + You can specify which build system you want to trigger a rebuild for. + If no build system is specified then no build is triggered. + + ## Command line + + * `--build` - Rebuild the JavaScript. Can be set to one of either: + + * `brunch` - use Brunch. This is the Phoenix default build system. + + * `webpack` - use Webpack + + """ + + @doc false + @spec run(OptionParser.argv) :: :ok + def run(args) do + case OptionParser.parse!(args, strict: [build: :string]) do + {[], []} -> + GraphQL.Relay.generate_schema_json! + {[build: build_system], []} -> + case build_system do + "brunch" -> + GraphQL.Relay.generate_schema_json! + System.cmd "brunch", ["build"] + "webpack" -> + GraphQL.Relay.generate_schema_json! + System.cmd "webpack", ["-p"] + _ -> + Mix.raise("`#{build_system}` is not currently supported.") + end + {_, _} -> + Mix.raise("Invalid options") + end + end +end diff --git a/mix.exs b/mix.exs index 97ef358..11dfde9 100644 --- a/mix.exs +++ b/mix.exs @@ -25,7 +25,7 @@ defmodule GraphQL.Relay.Mixfile do applications: [:logger, :ecto], env: [ schema_module: StarWars.Schema, # Module with a .schema function that returns your GraphQL schema - schema_json_path: "./schema.json" + schema_json_path: "./priv/graphql" ] ] end diff --git a/test/mix/tasks/graphql.gen.schema_json_test.exs b/test/mix/tasks/graphql.gen.schema_json_test.exs new file mode 100644 index 0000000..f4bb1f8 --- /dev/null +++ b/test/mix/tasks/graphql.gen.schema_json_test.exs @@ -0,0 +1,22 @@ +Code.require_file "../../support/star_wars/data.exs", __DIR__ +Code.require_file "../../support/star_wars/schema.exs", __DIR__ + +defmodule Mix.Tasks.Graphql.Gen.SchemaJsonTest do + use ExUnit.Case, async: true + + test "fails when invalid options given" do + assert_raise Mix.Error, "Invalid options", fn -> + Mix.Tasks.Graphql.Gen.SchemaJson.run(["invalid"]) + end + end + + test "generates schema.json" do + Mix.Tasks.Graphql.Gen.SchemaJson.run([]) + assert File.read!("priv/graphql/schema.json") == GraphQL.Relay.introspect + end + + # test "generates schema.json" do + # Mix.Tasks.Graphql.Gen.SchemaJson.run([]) + # assert File.read!("priv/graphql/schema.json") == GraphQL.Relay.introspect + # end +end