0

我正在使用 howtographql.com 网站为 Elixir 做苦艾酒教程。在某些时候(https://www.howtographql.com/graphql-elixir/2-queries/),在运行 graphql 服务器的最后一步时,出现错误。

执行的命令:

$ iex -S mix phx.server

输出:

Erlang/OTP 22 [erts-10.5] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] [dtrace]

warning: found quoted keyword "test" but the quotes are not required. Note that keywords are always atoms, even when quoted. Similar to atoms, keywords made exclusively of Unicode letters, numbers, underscore, and @ do not require quotes
  mix.exs:57

Compiling 4 files (.ex)

== Compilation error in file lib/community_web/schema.ex ==
** (CompileError) lib/community_web/schema.ex:17: undefined function field/3
    (elixir) lib/kernel/parallel_compiler.ex:229: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

这就是我的lib/community_web/schema.ex样子:

  1 defmodule CommunityWeb.Schema do
  2   use Absinthe.Schema
  3 
  4   alias CommunityWeb.NewsResolver
  5 
  6   object :link do
  7     field :id, non_null(:id)
  8     field :url, non_null(:string)
  9     field :description, non_null(:string)
 10   end
 11 
 12   query do
 13     field :all_links, non_null(list_of(non_null(:link)))
 14   end
 15 end
 16 
 17 field :all_links, non_null(list_of(non_null(:link))) do
 18   resolve &NewsResolver.all_links/3
 19 end

这就是我的lib/community_web/resolvers/news_resolver.ex样子:

  1 defmodule CommunityWeb.NewsResolver do
  2   alias Community.News
  3 
  4   def all_links(_root, _args, _info) do
  5     links = News.list_links()
  6     {:ok, links}
  7   end
  8 end

正如教程所述,我期待服务器将被执行,但只显示错误消息。

谢谢!

4

1 回答 1

0

你的第 17-19lib/community_web/schema.ex行不属于。field/3只能在苦艾酒对象中使用,并且您在模块之外拥有它。我的猜测是您打算将do块放在第 13 行的场地上。

我会尝试用第 17-19 行替换第 13 行,看看是否有帮助。

于 2019-09-24T19:37:16.520 回答