为什么我会收到与苦艾酒教程的 schema.ex 相关的 CompileError?



我正在使用 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

正如教程所述,我预计服务器将被执行,但仅显示错误消息。

谢谢!

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

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

最新更新