mirror of
https://github.com/ash-project/ash_graphql.git
synced 2024-09-20 05:13:33 +12:00
improvement: setup generate_object? setting on resource (#32)
This commit is contained in:
parent
5e2b1bc16f
commit
6fb99a3ab0
5 changed files with 63 additions and 20 deletions
|
@ -10,6 +10,7 @@ locals_without_parens = [
|
|||
depth_limit: 1,
|
||||
destroy: 2,
|
||||
destroy: 3,
|
||||
generate_object?: 1,
|
||||
get: 2,
|
||||
get: 3,
|
||||
identity: 1,
|
||||
|
|
|
@ -245,6 +245,12 @@ defmodule AshGraphql.Resource do
|
|||
* the type for the resource will implement the `Node` interface
|
||||
* pagination over that resource will behave as a Connection.
|
||||
"""
|
||||
],
|
||||
generate_object?: [
|
||||
type: :boolean,
|
||||
doc:
|
||||
"Whether or not to create the GraphQL object, this allows you to manually create the GraphQL object.",
|
||||
default: true
|
||||
]
|
||||
],
|
||||
sections: [
|
||||
|
@ -296,6 +302,10 @@ defmodule AshGraphql.Resource do
|
|||
Extension.get_opt(resource, [:graphql], :primary_key_delimiter, nil)
|
||||
end
|
||||
|
||||
def generate_object?(resource) do
|
||||
Extension.get_opt(resource, [:graphql], :generate_object?, nil)
|
||||
end
|
||||
|
||||
def ref(env) do
|
||||
%{module: __MODULE__, location: %{file: env.file, line: env.line}}
|
||||
end
|
||||
|
@ -2093,24 +2103,26 @@ defmodule AshGraphql.Resource do
|
|||
end
|
||||
|
||||
def type_definition(resource, api, schema) do
|
||||
type = Resource.type(resource)
|
||||
if generate_object?(resource) do
|
||||
type = Resource.type(resource)
|
||||
|
||||
interfaces =
|
||||
if relay?(resource) do
|
||||
[:node]
|
||||
else
|
||||
[]
|
||||
end
|
||||
interfaces =
|
||||
if relay?(resource) do
|
||||
[:node]
|
||||
else
|
||||
[]
|
||||
end
|
||||
|
||||
%Absinthe.Blueprint.Schema.ObjectTypeDefinition{
|
||||
description: Ash.Resource.Info.description(resource),
|
||||
interfaces: interfaces,
|
||||
fields: fields(resource, api, schema),
|
||||
identifier: type,
|
||||
module: schema,
|
||||
name: Macro.camelize(to_string(type)),
|
||||
__reference__: ref(__ENV__)
|
||||
}
|
||||
%Absinthe.Blueprint.Schema.ObjectTypeDefinition{
|
||||
description: Ash.Resource.Info.description(resource),
|
||||
interfaces: interfaces,
|
||||
fields: fields(resource, api, schema),
|
||||
identifier: type,
|
||||
module: schema,
|
||||
name: Macro.camelize(to_string(type)),
|
||||
__reference__: ref(__ENV__)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
defp fields(resource, api, schema) do
|
||||
|
|
11
test/resource_test.exs
Normal file
11
test/resource_test.exs
Normal file
|
@ -0,0 +1,11 @@
|
|||
defmodule AshGraphql.ResourceTest do
|
||||
use ExUnit.Case
|
||||
|
||||
test "object generated according to generate_object?" do
|
||||
assert %Absinthe.Type.Object{
|
||||
identifier: :user
|
||||
} = Absinthe.Schema.lookup_type(AshGraphql.Test.Schema, :user)
|
||||
|
||||
assert nil == Absinthe.Schema.lookup_type(AshGraphql.Test.Schema, :no_object)
|
||||
end
|
||||
end
|
|
@ -4,13 +4,14 @@ defmodule AshGraphql.Test.Registry do
|
|||
|
||||
entries do
|
||||
entry(AshGraphql.Test.Comment)
|
||||
entry(AshGraphql.Test.CompositePrimaryKey)
|
||||
entry(AshGraphql.Test.DoubleRelRecursive)
|
||||
entry(AshGraphql.Test.DoubleRelToRecursiveParentOfEmbed)
|
||||
entry(AshGraphql.Test.NonIdPrimaryKey)
|
||||
entry(AshGraphql.Test.NoObject)
|
||||
entry(AshGraphql.Test.Post)
|
||||
entry(AshGraphql.Test.PostTag)
|
||||
entry(AshGraphql.Test.Tag)
|
||||
entry(AshGraphql.Test.User)
|
||||
entry(AshGraphql.Test.NonIdPrimaryKey)
|
||||
entry(AshGraphql.Test.CompositePrimaryKey)
|
||||
entry(AshGraphql.Test.DoubleRelRecursive)
|
||||
entry(AshGraphql.Test.DoubleRelToRecursiveParentOfEmbed)
|
||||
end
|
||||
end
|
||||
|
|
18
test/support/resources/no_object.ex
Normal file
18
test/support/resources/no_object.ex
Normal file
|
@ -0,0 +1,18 @@
|
|||
defmodule AshGraphql.Test.NoObject do
|
||||
@moduledoc false
|
||||
|
||||
use Ash.Resource,
|
||||
data_layer: Ash.DataLayer.Ets,
|
||||
extensions: [AshGraphql.Resource]
|
||||
|
||||
graphql do
|
||||
type :no_object
|
||||
generate_object? false
|
||||
end
|
||||
|
||||
attributes do
|
||||
uuid_primary_key(:id)
|
||||
|
||||
attribute(:name, :string)
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue