Friday, May 28, 2021

multiple records and query fragments in the graphql

 //graphql query

{
  company(id"2"){
  id
  name
  description
}
}

keeping query before the graphql query{ } is optional,can be used to name your queries.

query findCompany{
  company(id"2"){
    id
    name
    description
  }
 }

we can query multiple queries in a single query,but we will get duplicate element issue in the final json result.

{
  company(id"1"){
    id
    name
    description
  }
  company(id"2"){
    id
    name
    description
  }
 }

so label them as below.

{
  applecompany(id"1"){
    id
    name
    description
  }
  googlecompany(id"2"){
    id
    name
    description
  }
 }

use of fragments to remove duplication of code:

{
  applecompany(id"1"){
    ...companyDetails
  }
  googlecompany(id"2"){
    ...companyDetails
  }
 }
 
 fragment companyDetails on Company{
   id
   name
   description
 }

No comments:

Post a Comment