CarlosX2X
CarlosX2X

Reputation: 193

How to query by non-primary key in AWS-amplify graphql schema

I have a graphql schema in aws-amplify, appsync.

my schema is:

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")
  }

and I want to know if I can search a projects interfaces by project_number instead of id. I added an index on project_name, but it doesn't seem to work. My current query is

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 
      }
    }
  }
}

Please let me know if this is possible. I would like it so that I can just search by project_name, and then get all the details from interface.

Upvotes: 4

Views: 1420

Answers (1)

BatteryAcid
BatteryAcid

Reputation: 8891

So according to the Amplify docs you would have to add an @index on the field you'd like to query on. I've updated your Project model to have an index on the project_number field.

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

Then to query by project_number, use the query name set in the queryField (projectByNumber) in the index:

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

Please checkout the docs under Query employee details by employee name to get a better understanding: https://docs.amplify.aws/cli/graphql/examples-and-solutions/#2-query-employee-details-by-employee-name

Upvotes: 1

Related Questions