【问题标题】:How to query by non-primary key in AWS-amplify graphql schema如何通过 AWS-amplify graphql 模式中的非主键查询
【发布时间】:2019-11-30 20:04:14
【问题描述】:

我在 aws-amplify、appsync 中有一个 graphql 架构。

我的架构是:

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

  }
type Interface
  @model 
  {
    id: ID!             
    interface_name: String!
    version: String!
    release: String!              
    source_feature: String!
    MFT_feature_id: String!
    state: String!
    source_application: String!     
    source_env: String!               
    source_go_live: String!              
    source_payload: String! 
    source_payload_format: String!
    source_payload_volume: String!              
    source_protocol: String! 
    target_application: String!       
    target_env: String!
    target_go_live: String!
    target_payload: String!
    target_payload_format: String!
    target_payload_volume: String!             
    target_protocol: String!
    frequency: String!                
    authentication: String!
    payload_security: String!
    transport_security: String!
    network_paths: String!
    project: Project @connection(name: "ProjectInterfaces")
  }

我想知道我是否可以通过 project_number 而不是 id 搜索项目接口。我在 project_name 上添加了一个索引,但它似乎不起作用。我当前的查询是

query GetProject {
  getProject(project_number:"P200") {
    name
    architect
    interfaces{
      items{
        interface_name
        version
        release              
        source_feature
        MFT_feature_id
        state
        source_application     
        source_env             
        source_go_live          
        source_payload
        source_payload_format
        source_payload_volume         
        source_protocol
        target_application  
        target_env
        target_go_live
        target_payload
        target_payload_format 
        target_payload_volume       
        target_protocol
        frequency                
        authentication
        payload_security
        transport_security
        network_paths 
      }
    }
  }
}

如果可能,请告诉我。我想要它,这样我就可以通过 project_name 搜索,然后从界面中获取所有详细信息。

【问题讨论】:

标签: indexing amazon-dynamodb graphql aws-amplify


【解决方案1】:

因此,根据 Amplify 文档,您必须在要查询的字段上添加 @index。我已经更新了您的 Project 模型,以便在 project_number 字段上有一个索引。

type Project 
  @model 
  {
    id: ID!
    project_number: String! @index(name: "byProjectName", queryField: "projectByNumber", sortKeyFields: ["id"])
    name: String!
    architect: String!
    interfaces: [Interface] @connection(name: "ProjectInterfaces")
  }

然后按project_number查询,使用索引中queryField(projectByNumber)中设置的查询名称:

query projectByNumber($projectNumber: String!) {
  projectByNumber(project_number: $projectNumber) {
    items {
      id
      project_number
      name
      architect
      ...
    }
  }
}

请查看按员工姓名查询员工详细信息下的文档以更好地理解:https://docs.amplify.aws/cli/graphql/examples-and-solutions/#2-query-employee-details-by-employee-name

【讨论】:

    猜你喜欢
    • 2019-09-07
    • 2020-10-26
    • 2021-01-25
    • 1970-01-01
    • 2021-01-14
    • 2020-01-17
    • 1970-01-01
    • 2020-07-06
    • 2022-07-30
    相关资源
    最近更新 更多