Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement graphQL sub-query

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?

like image 818
capouch Avatar asked Jun 29 '18 18:06

capouch


1 Answers

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
      }
    } 
  }
like image 125
Zohaib Ijaz Avatar answered Oct 22 '22 11:10

Zohaib Ijaz