标签: absinthe

我怎样才能在Absinthe中使用时间戳?(凤凰1.3)

我有凤凰1.3 +苦艾酒的问题.

我试试这段代码:

schema "urls" do
  field :path, :string

  timestamps()
 end

object :url do
  field :id, :id
  field :path, :string
  field :inserted_at, :datetime
end
Run Code Online (Sandbox Code Playgroud)

它可以使用id,path但它失败inserted_at了:

Inserted_at :datetime is not defined in your schema.          

Types must exist if referenced.   


lib/absinthe/schema.ex:230: Absinthe.Schema.__after_compile__/2     
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6                     
(elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
Run Code Online (Sandbox Code Playgroud)

elixir phoenix-framework absinthe

15
推荐指数
1
解决办法
1474
查看次数

如何在 Absinthe GraphQL 请求中接受 JSON

我试图在我的 graphql 实现中接收一串 JSON,但是我定义的用于处理 JSON 的自定义标量不断出现错误。

我已经定义了一个自定义标量来正确地将 JSON 序列化为 elixir 映射。在我的代码到达自定义标量的解析阶段之前,我收到错误数据类型无效。我正在尝试使用https://github.com/absinthe-graphql/absinthe/wiki/Scalar-Recipes#json-using-jason来创建标量,但是我已经修改为使用 Poison 而不是 Jason。

我的 absinthe mutator 使用我创建的 :json 标量类型。

@desc "Update user"
field :update_user, type: :user do
  arg(:email, :string)
  arg(:password, :string)
  arg(:first_name, :string)
  arg(:last_name, :string)
  arg(:age, :integer)
  arg(:client_store, :json)

  resolve(handle_errors(&Resolvers.User_Resolver.update_user/3))
end
Run Code Online (Sandbox Code Playgroud)

我的标量定义和 graphql 模式定义

    scalar :json, name: "Json" do
        description("""
        The `Json` scalar type represents arbitrary json string data, represented as UTF-8
        character sequences. The Json type is most often used to represent a free-form
        human-readable json …
Run Code Online (Sandbox Code Playgroud)

elixir phoenix-framework absinthe

5
推荐指数
1
解决办法
1077
查看次数

使用 cookie 授权带有长生不老药和苦艾酒的 graphql 订阅

我正在尝试使用 cookie 使用长生不老药和苦艾酒进行授权/身份验证 graphql 订阅,我使用了以下链接:

https://nts.strzibny.name/graphql-subscriptions-with-elixir-and-absinth/

我正在尝试对订阅正确主题的用户进行身份验证,但我无权访问订阅连接中的 cookie。为什么?

在我看到以下链接后:

https://hexdocs.pm/absinthe_phoenix/Absinthe.Phoenix.Socket.html

在我的 user_socket.ex 中,我将 user_id 作为查询参数传递,这有效,但根本不安全......我可以传递我想要的 id ??!!

有人能帮我吗?

 @moduledoc false

 use Phoenix.Socket

 use Absinthe.Phoenix.Socket,
   schema: MyAppGraphQL.Schema

 ## Channels
 # channel "room:*", MyAppWeb.RoomChannel

 # Socket params are passed from the client and can
 # be used to verify and authenticate a user. After
 # verification, you can put default assigns into
 # the socket that will be set for all channels, ie
 #
 #     {:ok, assign(socket, :user_id, verified_user_id)}
 #
 # To …
Run Code Online (Sandbox Code Playgroud)

sockets elixir graphql absinthe absinthe-subscription

5
推荐指数
0
解决办法
349
查看次数

Elixir、苦艾酒 如何在与苦艾酒约会后创建用户?

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

"message": "参数 \"filter\" 的值 $filter 无效。\n在字段 \"insertedAfter\" 中:预期类型 \"NaiveDateTime\",找到 \"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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

类型.ex

 use Absinthe.Schema.Notation
 use Absinthe.Ecto, repo: ElixirBlog.Repo
 import_types …
Run Code Online (Sandbox Code Playgroud)

elixir phoenix-framework graphql absinthe

5
推荐指数
1
解决办法
1355
查看次数

如何让 Absinthe 和 Dataloader 协同工作?

我有一个 GraphQL API,可以使用传统的解析函数正常工作。我的目标是消除N+1问题。

为此,我决定使用数据加载器。我已经完成了这些步骤以使应用程序运行:


  1. 我将这两个函数添加到我的上下文模块中:
defmodule Project.People do
  # CRUD...

  def data, do: Dataloader.Ecto.new(Repo, query: &query/2)

  def query(queryable, _params) do
    queryable
  end
end
Run Code Online (Sandbox Code Playgroud)
  1. 我将context/1和添加plugins/0到架构模块并更新了查询解析器:
defmodule ProjectWeb.GraphQL.Schema do
  use Absinthe.Schema

  import Absinthe.Resolution.Helpers, only: [dataloader: 1]

  alias ProjectWeb.GraphQL.Schema
  alias Project.People

  import_types(Schema.Types)

  query do
    @desc "Get a list of all people."
    field :people, list_of(:person) do
      resolve(dataloader(People))
    end

    # Other queries...
  end

  def context(context) do
    loader =
      Dataloader.new()
      |> Dataloader.add_source(People, People.data())

    Map.put(context, :loader, loader)
  end

  def plugins, do: [Absinthe.Middleware.Dataloader | …
Run Code Online (Sandbox Code Playgroud)

elixir ecto graphql absinthe dataloader

5
推荐指数
1
解决办法
1420
查看次数

Graphql Absinthe Elixir基于权限的可访问字段

定义并非所有用户都可以访问的字段的正确方法是什么?

例如,普通用户可以查询用户并找出其他用户的句柄,但是只有管理员用户才能找到其电子邮件地址。用户类型将其定义为字段,但可能无法访问。一般用户可以看到的类型是否应该单独设置?您将如何定义它?

抱歉,如果不清楚,我只是不掌握词汇。

elixir phoenix-framework graphql absinthe

3
推荐指数
1
解决办法
746
查看次数

苦艾酒 - 如何将_session放入解析器函数中?

我正在使用苦艾酒并且有突变迹象。当用户发送有效凭据时,我想通过put_session.

我面临的问题是我无法conn从解析器函数中访问。这告诉我,我不应该从解析器内更新连接的属性。

用苦艾酒可以做到这一点吗?有哪些替代解决方案?

elixir session-cookies phoenix-framework graphql absinthe

3
推荐指数
1
解决办法
1079
查看次数

凤凰城同一张桌子上的多对多

使用 Ecto v2.2.6、Phoenix 1.3

我有一张人表(碰巧彼此有关系),还有一张人的关系表。关系表在一列中列出父项的 id,在另一列中列出子项的 id。

mix phx.gen.json Accounts Person persons name:string age:integer
mix phx.gen.json Accounts Relationship relationships parent_id:references:persons child_id:references:persons
Run Code Online (Sandbox Code Playgroud)

现在,我将在关系模式中添加belongs_to 关系(目前看起来像这样)

  schema "relationships" do
    field :parent_id, :id
    field :child_id, :id
    timestamps()
  end
Run Code Online (Sandbox Code Playgroud)

但是如果没有显式设置 id 的能力,它看起来像这样(这似乎不起作用)

  schema "relationships" do
    belongs_to :person UserRelations.Accounts.Person 
    belongs_to :person UserRelations.Accounts.Person 
    timestamps()
  end
Run Code Online (Sandbox Code Playgroud)

如何编写关系模式以便捕获这些belongs_to关系?

编辑

根据建议,我已经像这样设置了架构:

  schema "relationships" do
    belongs_to :parent UserRelations.Accounts.Person 
    belongs_to :child UserRelations.Accounts.Person 
    timestamps()
  end
Run Code Online (Sandbox Code Playgroud)

我也尝试对 Person 架构做一些类似的事情:

  schema "persons" do
    field :age, :integer
    field :name, :string
    many_to_many :parent_of, UserRelations.Accounts.Person, join_through: …
Run Code Online (Sandbox Code Playgroud)

many-to-many elixir ecto phoenix-framework absinthe

2
推荐指数
1
解决办法
879
查看次数

如何使用 Elixir 中的苦艾酒在查询中的嵌套项目上使用参数?

我试图找出如何使用苦艾酒在查询中的嵌套项目上使用参数。

我试图完成的是:

{
  users(order: ASC) {
    id
    email
    clients(order: DESC) {
      id
      email
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的架构和类型:

  query do
    @desc "Get all users"
    field :users, list_of(:user) do
      arg :order, type: :sort_order, default_value: :asc
      resolve &Resolvers.users/2
    end
  end

  @desc "A user"
  object :user do
    field :id, :id
    field :email, :string
    field :clients, list_of(:user)
  end  
Run Code Online (Sandbox Code Playgroud)

和解析器:

  def users(_, args, _) do
    args
    |> Enum.reduce(User, fn
      {:order, order}, query ->
        query |> order_by({^order, :email})
    end)
    |> Repo.all |> Repo.preload([:clients])
  end
Run Code Online (Sandbox Code Playgroud)

所以我的问题是我应该如何以及在哪里为客户放置排序参数?通过上面的例子我得到一个错误:

"message": "Unknown …
Run Code Online (Sandbox Code Playgroud)

elixir phoenix-framework graphql absinthe

2
推荐指数
1
解决办法
1566
查看次数

[网络错误]:类型错误:网络请求失败

我第一次尝试阿波罗。我的后端服务器是 Phoenix 框架(长生不老药)。并在http://localhost:4000/api 中运行 所以我尝试在我的代码中使用 apollo 进行第一个查询。

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import ApolloClient from 'apollo-boost';
import { gql } from 'apollo-boost';
const client = new ApolloClient(
  {
    uri: 'http://localhost:4000/api',
  }
);
client.query({
   query: gql`
     { 
        users {
          name
          email
        } 
     `}
}).then(result => console.log(result));
Run Code Online (Sandbox Code Playgroud)

但我有一个错误。

[Network error]: TypeError: Network request failed

    node_modules/expo/build/logs/LogSerialization.js:166:14 in _captureConsoleStackTrace
    node_modules/expo/build/logs/LogSerialization.js:41:24 in serializeLogDataAsync
    ... 9 more stack frames from framework internals

[Unhandled promise rejection: Error: Network error: …
Run Code Online (Sandbox Code Playgroud)

elixir apollo phoenix-framework react-native absinthe

1
推荐指数
1
解决办法
3165
查看次数