首页 > 其他分享 >absinthe elixir 的graphql 框架简单试用

absinthe elixir 的graphql 框架简单试用

时间:2024-01-08 18:02:21浏览次数:28  
标签:do end absinthe graphql AbsinthePhxWeb elixir Schema

absinthe elixir 的graphql 框架

包含的特性

  • graphql 草案的完整实现
  • 插件化设计
  • 异步字段解析以及批量解析,同时支持插件化的解析支持
  • 安全
  • 包含了基于GraphiQL 的playground

项目测试

基于了phoenix 框架

  • 创建phoenix 项目
mix phx.new absinthe_phx
  • 添加sqlite ecto依赖
    默认phoenix 使用了pg,对于本地测试不是很方便(如果基于docker 就比较方便了),所以调整为了基于sqlite 的

 

{:ecto_sqlite3, "~> 0.14.0"},

调整repo adapter 配置

defmodule AbsinthePhx.Repo do
  use Ecto.Repo,
    otp_app: :absinthe_phx,
    adapter: Ecto.Adapters.SQLite3
添加graphql 实体&解析器&以及查询定义&endpoint 定义
实体
 
defmodule AbsinthePhxWeb.Schema.ContentTypes do
  use Absinthe.Schema.Notation
 
  object :post do
    field :id, :id
    field :title, :string
    field :body, :string
  end
end
解析

defmodule AbsinthePhxWeb.Resolvers.Content do
  def list_posts(_parent, _args, _resolution) do
    {:ok, [%{id: 1, title: "Hello", body: "World"}]}
  end
 
end
查询

defmodule AbsinthePhxWeb.Schema do
  use Absinthe.Schema
  import_types AbsinthePhxWeb.Schema.ContentTypes
 
  alias AbsinthePhxWeb.Resolvers
 
  query do
 
    @desc "Get all posts"
    field :posts, list_of(:post) do
      resolve &Resolvers.Content.list_posts/3
    end
 
  end
 
end
endpoint 定义(在router 中添加)

scope "/api" do
  pipe_through :api
 
  forward "/graphiql", Absinthe.Plug.GraphiQL,
    schema: AbsinthePhxWeb.Schema
 
  forward "/", Absinthe.Plug,
    schema: AbsinthePhxWeb.Schema
 
end
use Ecto.Repo,

访问&测试

  • 启动
mix phx.server

访问
http://localhost:4000/api/graphiql
效果

absinthe elixir 的graphql 框架简单试用_html

说明

absinthe star 还是很多的,比较强大,同时还提供了类似 facebook dataloader 的工具 ,对于选择elixir做为开发框架的项目值得学习下

参考资料

https://github.com/absinthe-graphql/absinthe
http://absinthe-graphql.org/
https://hexdocs.pm/absinthe/dataloader.html
https://www.phoenixframework.org/
https://hexdocs.pm/absinthe/our-first-query.html
https://hexdocs.pm/absinthe/subscriptions.html

标签:do,end,absinthe,graphql,AbsinthePhxWeb,elixir,Schema
From: https://blog.51cto.com/rongfengliang/9148252

相关文章

  • elixir apply 结合 macro 一个方便的开发技巧
    此玩法实际在Phoenixelixir项目中使用比较多,以下是一个简单的示例示例代码login.exdefmoduleLogindodefdemoappdoquotedoimportunquote(__MODULE__)endenddefdalongdoIO.puts("dalongdemo")enddefmacro__using__(which)whenis_......
  • elixir macro defoverridable 功能
    基于macro的包装函数可以使用defoverridable可以方便进行方法的重写参考使用app.exdefmoduleAppdodefmacro__using__(_options)doquotedodefinitdoIO.puts"doinit"enddeflogindoIO.puts"dologin"end......
  • wasmex webassenbly elixir 运行时
    wasmex是基于wasmtime以及rustnif开发的方便elixir运行webassembly的框架与rust的集成与rust集成使用的三方包 与mjml工具类似使用了rustler_precompiled以及rustlerrust使用的三方包 前边也说了是基于了wasmtime包装的,同时使用了wasmtimewasi一些子模块说明rustle......
  • elixir erlang 简单调用学习
    实际上基于elixir的mix进行erlang以及elixir的互调用开发处理是很方便的,mix直接就包含了构建erlang代码同时对于代码的互调用,只要使用符合语言格式要求就行了,以下是一个简单的互调用学习项目准备项目结构 ├──README.md├──lib│├──a.e......
  • rebar3 集成elixir 模块
    社区包含了一个rebar3的elixirmix插件,可以方便rebar使用elixir模块参考使用rebar.config配置{erl_opts,[debug_info]}.{deps,[%添加引用{decimal,"2.0.0"}]}.{shell,[{apps,[basic]}]}.%添加插件{plugins,[rebar_mix]}.{provider_hooks,[{......
  • elixir mjml_nif 试用
    mjml_nif是使用rustler包装的mrml基于了erlang的nif接口实现的elixir模块,同时为了方便预编译nif的使用,使用了rustler_precompiled,以下是一个简单的试用项目试用添加依赖defpdepsdo[{:mjml,"~>3.0"}]end代码集成lib/mail/rong.mail.exdefmoduleRo......
  • elixir mjml_nif 试用
    mjml_nif是使用rustler包装的mrml基于了erlang的nif接口实现的elixir模块,同时为了方便预编译nif的使用,使用了rustler_precompiled,以下是一个简单的试用项目试用添加依赖 defpdepsdo[{:mjml,"~>3.0"}]end代码集......
  • rebar3 引用本地elixir 模块
    前边简单说过基于rebar_mix使用elixir模块,但是使用的模块是三方的,如果时候我们可以需要使用自己的就可以使用本地git项目,或者搭建自己的私服git,以下是一个简单使用项目准备本地elixirmix项目一个基于mixcli创建的项目,同时进行gitinit mixnewlogingit......
  • rebar3 集成elixir 模块
    社区包含了一个rebar3的elixirmix插件,可以方便rebar使用elixir模块参考使用rebar.config配置{erl_opts,[debug_info]}.{deps,[%添加引用{decimal,"2.0.0"}]}. {shell,[{apps,[basic]}]}. %添加插件 {plugins,[......
  • 何时使用GraphQL、gRPC 和 REST
    何时使用GraphQL、gRPC和REST     在设计应用程序时,开发人员可以从各种客户端-服务器通信协议中进行选择。使用GraphQL、gRPC和REST在当代项目中相对常见。每种协议都可以提供各种优势,具体取决于您的应用需求。      一.GraphQL是一种灵活的数据请求方法,它专......