【问题标题】:Querying in GraphQL, get the list of items associated to a category在 GraphQL 中查询,获取与类别关联的项目列表
【发布时间】:2023-03-30 02:19:01
【问题描述】:

我有一个 GraphQL Schema,它有一个项目,每个项目都有关联的接口。我可以按项目编号通过@key 查询它。但是现在,我无法获取项目关联的项目。这是一些信息:

我的 GraphQL 架构:

type Project 
  @model @key(fields: ["project_number"]) 
  {
    project_number: String!
    name: String!
    architect: String!
    interfaces: [Interface] @connection(name: "ProjectInterfaces")

  }
type Interface
  @model 
  {
    id: ID!             
    interface_name: String!
    version: String!
    release: String!    
    project: Project @connection(name: "ProjectInterfaces")          
  }

这是我的查询:

query GetProject {
  getProject(project_number:"P100") {
    project_number
    name
    architect
    interfaces{
      items{
        id
        interface_name
      }
    }
  }
}

在我使用上面的模式之前没有 key 作为 project_number 所以我有类似id:ID! 的项目。有了这个,我就可以使用相同的查询,并获取具有该 ID 的关联项目。但是现在我使用了 project_number 的密钥,它不允许我返回相关的项目。也许这是一个模式问题?我在想这可能是一个解析器问题,但我玩弄了解析器,但似乎没有任何效果。有关解析器的背景。我相信这是由于 interfaceTable 解析器。这是它自动生成的代码。我不知道具体要解决什么问题。

#set( $limit = $util.defaultIfNull($context.args.limit, 10) )
#set( $query = {
  "expression": "#connectionAttribute = :connectionAttribute",
  "expressionNames": {
      "#connectionAttribute": "interfaceProjectId"
  },
  "expressionValues": {
      ":connectionAttribute": {
          "S": "$context.source.id"
    }
  }
} )
{
  "version": "2017-02-28",
  "operation": "Query",
  "query":   $util.toJson($query),
  "scanIndexForward":   #if( $context.args.sortDirection )
    #if( $context.args.sortDirection == "ASC" )
true
    #else
false
    #end
  #else
true
  #end,
  "filter":   #if( $context.args.filter )
$util.transform.toDynamoDBFilterExpression($ctx.args.filter)
  #else
null
  #end,
  "limit": $limit,
  "nextToken":   #if( $context.args.nextToken )
"$context.args.nextToken"
  #else
null
  #end,
  "index": "gsi-ProjectInterfaces"
}

我目前还有一个名为“gsi-ProjectInterfaces”的接口表的 gsi 索引

请让我知道你们都认为可能是什么问题!谢谢!

【问题讨论】:

    标签: amazon-dynamodb graphql aws-amplify


    【解决方案1】:

    您的问题的一个解决方案可能是再次删除 @key 指令。

    然后使用listProjects查询并通过project_name过滤。

    query listProjects {
      listProjects(filter: {
        project_number:{
          eq: "some-number"
        }
      }) {
        project_number
        name
        architect
        interfaces{
          items{
            id
            interface_name
          }
        }
      }
    }
    

    这本质上是只针对一项的获取查询。虽然它的效率有点低,但这可能不是问题,具体取决于您的项目有多大。 (让它工作,让它正确,让它快 -> 快最后?)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 2012-06-08
      • 2022-01-18
      相关资源
      最近更新 更多