improvement: setup generate_object? setting on resource (#32)

This commit is contained in:
Michael St Clair 2022-05-09 19:53:01 -06:00 committed by GitHub
parent 5e2b1bc16f
commit 6fb99a3ab0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 20 deletions

View file

@ -10,6 +10,7 @@ locals_without_parens = [
depth_limit: 1,
destroy: 2,
destroy: 3,
generate_object?: 1,
get: 2,
get: 3,
identity: 1,

View file

@ -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
View 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

View file

@ -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

View 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