我正在尝试获取在特定日期之前/之后创建的用户,并且我收到此错误。
“message”:“Argument \”filter \“具有无效值$ filter。\ nIn field \”insertedAfter \“:预期类型\”NaiveDateTime \“,找到\”2018-05-13 \“。”,
错误是有道理的,因为“2018-05-13”不是NativeDateTime ...但是,我认为NativeDateTime应该解析字符串“2018-05-13”。我正在尝试应用我从书中学到的东西但是很难。我还创建了一个自定义日期标量但我基本上遇到了同样的错误。
如何在凤凰和苦艾酒的某个日期之后搜索创建的用户?
query($filter: UserFilter){
allUsers(filter: $filter){
id
firstName
lastName
}
}
values
{
"filter": {
"insertedAfter": "2018-05-13"
}
}
Ecto架构,帐户> user.ex
schema "users" do
field :avatar_img, :string
field :email, :string, null: false
field :fb_token, :string
field :first_name, :string
field :google_token, :string
field :last_name, :string
field :password, :string, null: false
field :admin_user_id, :string
timestamps()
end
types.ex
use Absinthe.Schema.Notation
use Absinthe.Ecto, repo: ElixirBlog.Repo
import_types Absinthe.Type.Custom
object :user do
field :id, non_null(:id)
field :first_name, non_null(:string)
field :last_name, non_null(:string)
field :email, non_null(:string)
field :password, non_null(:string)
field :avatar_img, :string
field :admin_user_id, :string
field :fb_token, :string
field :google_token, :string
field :inserted_at, :naive_datetime
field :updated_at, :naive_datetime
end
Absinthe架构 schema.ex
use Absinthe.Schema
import_types Elixir.Schema.Types
input_object :user_filter do
field :id, :integer
field :first_name, :string
field :last_name, :string
field :email, :string
field :inserted_before, :naive_datetime
field :inserted_after, :naive_datetime
end
query do
field :all_users, list_of(:user) do
arg :filter, :user_filter
arg :order, type: :sort_order, default_value: :asc
resolve &ElixirBlogWeb.UsersResolver.all_users/3
end
end
users_resolver.ex
alias Elixir.Account
def all_users(_root, args, _info) do
users = Account.list_users(args)
{:ok, users}
end
account.ex
def list_users(args) do
args
|> Enum.reduce(User, fn
{:order, order}, query ->
query |> order_by({^order, :first_name})
{:filter, filter}, query ->
query |> filter_with(filter)
end)
|> Repo.all
end
defp filter_with(query, filter) do
Enum.reduce(filter, query, fn
{:id, id}, query ->
from q in query, where: q.id == ^id
{:first_name, first_name}, query ->
from q in query, where: ilike(q.first_name, ^"%#{first_name}%")
{:last_name, last_name}, query ->
from q in query, where: ilike(q.last_name, ^"%#{last_name}%")
{:email, email}, query ->
from q in query, where: ilike(q.email, ^"%#{email}%")
{:inserted_before, date}, query ->
from q in query, where: q.inserted_at <= ^date
{:inserted_after, date}, query ->
from q in query, where: q.inserted_at >= ^date
end)
end
mix.exs
{:phoenix, "~> 1.3.2"},
{:phoenix_ecto, "~> 3.2"},
{:absinthe, "~> 1.4"},
{:absinthe_plug, "~> 1.4"},
{:absinthe_ecto, "~> 0.1.3"},
答案 0 :(得分:1)
我在:naive_datetime
的文档中找不到关于Absinthe
的内容。
但我们可以[在Absinthe的代码中找到它](https://github.com/absinthe-graphql/absinthe/blob/master/lib/absinthe/type/custom.ex#L29)
The DateTime appears in a JSON response as an ISO8601 formatted string.
因此我们可以使用datetime "2018-05-13 00:00:07"
。