Skip to content

Instantly share code, notes, and snippets.

@nilshartmann
Last active March 21, 2025 18:56
Show Gist options
  • Save nilshartmann/46249c757500579bb73403f83d69229f to your computer and use it in GitHub Desktop.
Save nilshartmann/46249c757500579bb73403f83d69229f to your computer and use it in GitHub Desktop.
GraphQL-Query-Stories
  • Try to run a query that returns the first ten stories and requests the following fields:
    • ID, title, excerpt, publication date, who wrote the story, and the first ten comments for each story
query {
  stories(first: 10) {
    nodes {
      id
      title
      excerpt
      createdAt
      writtenBy {
        user {
          name
        }
      }
      comments(first: 10) {
        nodes {
          id
          content
        }
      }
    }
  }
}

Can you extend the query to return the ten newest stories?

(add orderBy)

query {
  stories(orderBy: {field: createdAt, direction: desc}, first: 10) {
    nodes {
      id
      title
      excerpt
      createdAt
      writtenBy {
        user {
          name
        }
      }
      comments(first: 10) {
        nodes {
          id
          content
        }
      }
    }
  }
}

Member-Daten mit Fragment abfragen

  • Create a fragment (Author) that selects a Member's ID, as well as the name and id of the associated user
    • Use this fragment in both the stories and the comments to query member information
    • In the stories, also query the member's skills
fragment Author on Member {
  id user { name id }
}

query {
  stories(orderBy: {field: createdAt, direction: desc}, first: 10) {
    nodes {
      id
      title
      excerpt
      createdAt
      writtenBy {
        ...Author
        skills
      }
      comments(first: 10) {
        nodes {
          id
          content
          
          writtenBy { ...Author }
        }
      }
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment