How to implement graphQL sub-query

11,213

type Agent {
    id: ID!
    name: String
}

type Author {
    id: ID!
    firstName: String
    middleInitial: String
    lastName: String
    posts: [Post]
    agent: Agent
}

// Query
 {
   authors {
     firstName
     lastName
     agent {
         name
      }
    } 
  }

Share:
11,213

Related videos on Youtube

capouch
Author by

capouch

Updated on June 09, 2022

Comments

  • capouch
    capouch over 1 year

    I'm having a block trying to figure out the syntax for a query condition that seems like it would be a pretty common one.

    The typedef below stores an ID value for the agent who represents an Author, in a one-agent-to-many (authors) relationship. I'd like to extract the name of the agent at the same time I'm getting details from the Author schema.

    type Author {
        id: ID!
        firstName: String
        middleInitial: String
        lastName: String
        posts: [Post]
        agent: ID
    

    }

    So the enclosing query (returning a list of all Authors) looks like this:

    {
      authors {
        firstName
        lastName
        agent
        }
    }
    

    This gives me back the Agent's ID. How can I use that same query to fetch the agent's name, from a typedef similar to the one above?

  • capouch
    capouch over 5 years
    I mistakenly said one-to-one; each author has an agent, but each agent has many authors.

Related