联合 Apollo 图 - 跨子图连接实体(Elixir/Absinthe)

问题描述 投票:0回答:1

我正在使用 Elixir/Phoenix 和 Absinthe 在 Apollo 中定义联合图。我已经浏览了 Odyssey Voyage I 课程,但似乎无法拼凑出我需要的内容。这是我的场景:

我有两个服务(或子图)。一个是 ProjectLocation 子图,另一个是 Inspection 子图。一个 ProjectLocation 可以有 1 到多个检查。我需要的是查询 ProjectLocation 并根据检查参数将其返回,并包含所有检查。我将模块分为类型、突变、查询、解析器,然后将它们全部导入到一个架构文件中。

项目位置子图类型

defmodule MyProject.Schema.Types.ProjectLocation do


use Absinthe.Schema.Notation

  object :project_location do
    field :id, :id
    field :project_id, :id
    field :address, :string
  end

  input_object :input_project_location_params do
    field :id, :id
    field :project_id, :id
    field :address, :string
  end
end

项目位置子图查询

defmodule MyProject.Schema.Queries.ProjectLocation do
  use Absinthe.Schema.Notation

  alias MyProject.Schema.Resolvers

  object :project_location_queries do

    @desc "Find ProjectLocation"
    field :project_location, type: :project_location do
      arg(:project_location_id, non_null(:id))
      resolve(&Resolvers.ProjectLocation.get_project_location/3)
    end
    
  end
end

检查子图类型

defmodule MyInspectionProject.Schema.Types.Inspection do
    use Absinthe.Schema.Notation

    object :inspection do
      field :id, :id
      field :project_id, :id
      field :inspection_location, :id //This is the project location id (bad naming on my part)
      field :status, :string
    end

    @desc "Inspection"
    input_object :input_inspection_params do
      field :id, :id
      field :project_id, :id
      field :inspection_location, :id
      field :status, :string
    end
  end

检查子图查询

defmodule MyInspectionProject.Schema.Queries.Inspection do
    use Absinthe.Schema.Notation

    alias MyInspectionProject.Schema.Resolvers

    object :inspection_queries do
      @desc "Find Inspection"
      field :inspection, type: :inspection do
        arg(:inspection, non_null(:input_inspection_params))
        resolve(&Resolvers.Inspection.get_inspection/3)
      end
    end
  end

使用 React 前端,我们将为项目位置编写查询,如下所示:

const GET_PROJECT_LOCATIONS = gql`
  query ProjectLocations($projectId: ID!) {
    projectLocations(projectId: $projectId) {
      id
      projectId
      address
    }
  }
`;

我想我的问题是如何设置类型并编写查询以预加载和查询检查作为“子查询”?是否像在 ProjectLocation 子图中设置检查类型并按照此方式在某处编写查询一样简单?

const GET_PROJECT_LOCATIONS = gql`
  query ProjectLocations($projectId: ID!) {
    projectLocations(projectId: $projectId) {
      id
      projectId
      address
      inspection($status: STRING){
         id
         project_id
         inspection_location
         status
      }
    }
  }
`;

如果是这样,传递到此 GraphQL 调用中的变量对象会是什么样子。还忘了提及我正在使用 React 中 Apollo 客户端库的 useLazyQuery 钩子。

reactjs graphql elixir apollo absinthe
1个回答
0
投票

如果检查

status
也是根查询的参数(第二个参数,在projectID之后),那么graphql将是有效的。请参阅此处的示例:https://elixirforum.com/t/why-do-you-have-to-re-declare-types-in-graphql-queries-when-you-use-variables/52723/10

对你来说它看起来像这样:

  query ProjectLocations($projectId: ID!, $status: STRING) {
    projectLocations(projectId: $projectId) {

还要在

:inspection
对象定义中添加
project_location
字段。在那里,您可以通过使用解析函数写入字段来处理
status
参数。它看起来像这样:

field :inspection, :inspection do
  resolve(fn project_location, args, resolution ->
    inspection = get_inspection(project_location, args)
    {:ok, inspection}
  end)
end
© www.soinside.com 2019 - 2024. All rights reserved.