Skip to content

Commit

Permalink
improvement: support define_relay_types?: false
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Oct 16, 2023
1 parent 0e4ac71 commit a33e4f5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 60 deletions.
8 changes: 8 additions & 0 deletions documentation/topics/relay.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ The two changes that are made currently are:

* the type for the resource will implement the `Node` interface
* pagination over that resource will behave as a Connection.

## Using with Absinthe.Relay

Use the following option when calling `use AshGraphql`

```elixir
use AshGraphql, define_relay_types?: false
```
123 changes: 65 additions & 58 deletions lib/api/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ defmodule AshGraphql.Api do
end

@doc false
def type_definitions(api, resources, schema, env, first?) do
def type_definitions(api, resources, schema, env, first?, define_relay_types?) do
resource_types =
resources
|> Enum.reject(&Ash.Resource.Info.embedded?/1)
Expand All @@ -101,67 +101,74 @@ defmodule AshGraphql.Api do
end)

if first? do
[
%Absinthe.Blueprint.Schema.InterfaceTypeDefinition{
description: "A relay node",
name: "Node",
fields: [
%Absinthe.Blueprint.Schema.FieldDefinition{
description: "A unique identifier",
identifier: :id,
module: schema,
name: "id",
__reference__: AshGraphql.Resource.ref(env),
type: %Absinthe.Blueprint.TypeReference.NonNull{of_type: :id}
}
],
identifier: :node,
__reference__: AshGraphql.Resource.ref(env),
module: schema
},
%Absinthe.Blueprint.Schema.ObjectTypeDefinition{
description: "A relay page info",
name: "PageInfo",
fields: [
%Absinthe.Blueprint.Schema.FieldDefinition{
description: "When paginating backwards, are there more items?",
identifier: :has_previous_page,
module: schema,
name: "has_previous_page",
__reference__: AshGraphql.Resource.ref(env),
type: %Absinthe.Blueprint.TypeReference.NonNull{of_type: :boolean}
},
%Absinthe.Blueprint.Schema.FieldDefinition{
description: "When paginating forwards, are there more items?",
identifier: :has_next_page,
module: schema,
name: "has_next_page",
relay_types =
if define_relay_types? do
[
%Absinthe.Blueprint.Schema.InterfaceTypeDefinition{
description: "A relay node",
name: "Node",
fields: [
%Absinthe.Blueprint.Schema.FieldDefinition{
description: "A unique identifier",
identifier: :id,
module: schema,
name: "id",
__reference__: AshGraphql.Resource.ref(env),
type: %Absinthe.Blueprint.TypeReference.NonNull{of_type: :id}
}
],
identifier: :node,
__reference__: AshGraphql.Resource.ref(env),
type: %Absinthe.Blueprint.TypeReference.NonNull{of_type: :boolean}
module: schema
},
%Absinthe.Blueprint.Schema.FieldDefinition{
description: "When paginating backwards, the cursor to continue",
identifier: :start_cursor,
%Absinthe.Blueprint.Schema.ObjectTypeDefinition{
description: "A relay page info",
name: "PageInfo",
fields: [
%Absinthe.Blueprint.Schema.FieldDefinition{
description: "When paginating backwards, are there more items?",
identifier: :has_previous_page,
module: schema,
name: "has_previous_page",
__reference__: AshGraphql.Resource.ref(env),
type: %Absinthe.Blueprint.TypeReference.NonNull{of_type: :boolean}
},
%Absinthe.Blueprint.Schema.FieldDefinition{
description: "When paginating forwards, are there more items?",
identifier: :has_next_page,
module: schema,
name: "has_next_page",
__reference__: AshGraphql.Resource.ref(env),
type: %Absinthe.Blueprint.TypeReference.NonNull{of_type: :boolean}
},
%Absinthe.Blueprint.Schema.FieldDefinition{
description: "When paginating backwards, the cursor to continue",
identifier: :start_cursor,
module: schema,
name: "start_cursor",
__reference__: AshGraphql.Resource.ref(env),
type: :string
},
%Absinthe.Blueprint.Schema.FieldDefinition{
description: "When paginating forwards, the cursor to continue",
identifier: :end_cursor,
module: schema,
name: "end_cursor",
__reference__: AshGraphql.Resource.ref(env),
type: :string
}
# 'count' field is not compatible with keyset pagination
],
identifier: :page_info,
module: schema,
name: "start_cursor",
__reference__: AshGraphql.Resource.ref(env),
type: :string
},
%Absinthe.Blueprint.Schema.FieldDefinition{
description: "When paginating forwards, the cursor to continue",
identifier: :end_cursor,
module: schema,
name: "end_cursor",
__reference__: AshGraphql.Resource.ref(env),
type: :string
__reference__: AshGraphql.Resource.ref(env)
}
# 'count' field is not compatible with keyset pagination
],
identifier: :page_info,
module: schema,
__reference__: AshGraphql.Resource.ref(env)
}
] ++ resource_types
]
else
[]
end

relay_types ++ resource_types
else
resource_types
end
Expand Down
7 changes: 5 additions & 2 deletions lib/ash_graphql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ defmodule AshGraphql do
quote bind_quoted: [
apis: opts[:apis],
api: opts[:api],
action_middleware: opts[:action_middleware] || []
action_middleware: opts[:action_middleware] || [],
define_relay_types?: Keyword.get(opts, :define_relay_types?, true)
],
generated: true do
require Ash.Api.Info
Expand Down Expand Up @@ -169,7 +170,8 @@ defmodule AshGraphql do
unquote(resources),
unquote(schema),
__ENV__,
true
true,
unquote(define_relay_types?)
) ++
global_enums ++
global_unions ++
Expand All @@ -182,6 +184,7 @@ defmodule AshGraphql do
unquote(resources),
unquote(schema),
__ENV__,
false,
false
)
end
Expand Down

0 comments on commit a33e4f5

Please sign in to comment.