mirror of
https://github.com/ash-project/ash_graphql.git
synced 2024-09-20 21:32:59 +12:00
34 lines
769 B
Markdown
34 lines
769 B
Markdown
|
# Use Maps with GraphQL
|
||
|
|
||
|
If you define an `Ash.Type.NewType` that is a subtype of `:map`, _and_ you add the `fields` constraint which specifies field names and their types, `AshGraphql` will automatically derive an appropriate GraphQL type for it.
|
||
|
|
||
|
For example:
|
||
|
|
||
|
```elixir
|
||
|
defmodule MyApp.Types.Metadata do
|
||
|
@moduledoc false
|
||
|
use Ash.Type.NewType, subtype_of: :map, constraints: [
|
||
|
fields: [
|
||
|
title: [
|
||
|
type: :string
|
||
|
],
|
||
|
description: [
|
||
|
type: :string
|
||
|
]
|
||
|
]
|
||
|
]
|
||
|
|
||
|
def graphql_type, do: :metadata
|
||
|
end
|
||
|
|
||
|
```
|
||
|
|
||
|
## Bypassing type generation for an map
|
||
|
|
||
|
Add the `graphql_define_type?/1` callback, like so, to skip Ash's generation (i.e if you're defining it yourself)
|
||
|
|
||
|
```elixir
|
||
|
@impl true
|
||
|
def graphql_define_type?(_), do: false
|
||
|
```
|