我正在尝试获取在特定日期之前/之后创建的用户,但出现此错误。

"message": "Argument\"filter\"has invalid value $filter.\nIn field\"insertedAfter\": 预期类型\"NaiveDateTime\", found\"2018-05-13\".",

该错误是有道理的,因为“2018-05-13”不是 NativeDateTime……但是,我认为 NativeDateTime 应该解析字符串“2018-05-13”。我正在尝试应用我从书中学到的知识,但很难。我还创建了一个自定义日期标量,但我基本上遇到了同样的错误。

我怎样才能用 Phoenix 和苦艾酒搜索某个日期后创建的用户?

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

类型.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

苦艾酒模式
模式文件
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

帐户.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

混合文件
  {:phoenix, "~> 1.3.2"},
  {:phoenix_ecto, "~> 3.2"},
  {:absinthe, "~> 1.4"},
  {:absinthe_plug, "~> 1.4"},
  {:absinthe_ecto, "~> 0.1.3"},

最佳答案

我在 :naive_datetime 的文档中找不到有关 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"

关于Elixir, Absinthe 如何在与苦艾酒约会后创建用户?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50341358/

10-10 17:21