Skip to content

Latest commit

 

History

History
153 lines (127 loc) · 1.89 KB

04-query.md

File metadata and controls

153 lines (127 loc) · 1.89 KB

04 - Query

These are the queries that were used in the video. Try playing around with them till you feel comfortable. You may also check out the official docs for GraphQL query.

1. Basic Query

{
  viewer {
    # query can have comments too!
    # shape of data is maintained
    name
  }
}

2. Nested fields

{
  viewer {
    name
    # fields can refer to objects
    status {
      # requesting status fields
      emoji
      message
    }
  }
}

3. Passing arguements

{
  viewer {
    name
    # passing arguements to field
    organization(login: "wtjs") {
      name
    }
  }
}

4. Aliases

{
  # aliasing query results
  lovesMountains: user(login: "divyanshu013") {
    name
  }
  lovesBeaches: user(login: "metagrover") {
    name
  }
}

5. Fragments

{
  lovesMountains: user(login: "divyanshu013") {
    ...userFields
  }
  lovesBeaches: user(login: "metagrover") {
    ...userFields
  }
}

# creating a reusable fragment
fragment userFields on User {
  name
  bio
}

6. Operation type and name

# Add an operation type and name
query GetViewer {
  viewer {
    name
  }
}

7. Variables

# Adding a $login variable. Notice the !
query GetUser($login: String!) {
  user(login: $login) {
    name
  }
}
{
  "login": "divyanshu013"
}

8. Default variables

# Adding a $login default variable
query GetUser($login: String = "divyanshu013") {
  user(login: $login) {
    name
  }
}
{

}

9. Directives

# Using directives @include @skip
query GetUser($withBio: Boolean!) {
  user(login: "divyanshu013") {
    name
    bio @include(if: $withBio)
  }
}
{
  "withBio": true
}

10. Meta fields

# Meta fields
query GetViewer {
  viewer {
    __typename
    name
  }
}