I have a Book table which has_many BookTitle.
the Book Ecto schema is:
schema "book" do
field :absolute_id, :integer
field :basename, :string
field :uuid, Ecto.UUID
has_many :chapter, Allscripture.Chapter
has_many :booktitle, Allscripture.BookTitle
end
BookTitle(book_title.ex):
schema "booktitle" do
field :language_id, :string
field :localizedname, :string
field :uuid, Ecto.UUID
field :book_id, :id
end
and my absinthe schema is(schema.ex):
object :book do
field :uuid, :string
field :languageId, :string
field :title, :string
field :localizedTitle, :string
end
it does not match the postgresql db schema because the API is meant to return a join of information from Book and BookTitle.
this is my graphql query(schema.ex):
alias Allscripture.{Book, BookTitle, Repo}
query do
field :books, list_of(:book) do
arg :language, :string
resolve fn
_, %{language: lang}, _ when is_binary(lang) ->
query = from book in Book,
join: title in BookTitle,
on: book.id == title.book_id,
where: title.language_id == ^lang,
select: %{uuid: book.uuid,
languageId: title.language_id,
title: book.basename,
localizedTitle: title.localizedname},
order_by: book.absolute_id
{:ok, Repo.all(query)}
...
I used an explicit join to join BookTitle to Book because my efforts with preload were a dead-end. Anyway, when I do a graphql query (localhost:4000/graphiql):
{
books(language: "pt") {
languageId
localizedTitle
title
uuid
}
}
Absinthe gives me an error array:
{
"errors": [
{
"message": "Cannot query field \"languageId\" on type \"Book\". Did you mean \"languageId\"?",
"locations": [
{
"line": 3,
"column": 0
}
]
},
{
"message": "Cannot query field \"localizedTitle\" on type \"Book\". Did you mean \"localizedTitle\"?",
"locations": [
{
"line": 4,
"column": 0
}
]
}
]
}
when testing the ecto query in iex, it seems to return the correct data set.
What is the correct way to tell Absinthe that it shouldn’t depend upon the database schema but the database query response to validate the graphql query response? Or am I doing things wrong?
Michael