Code Helper Guide for "GraphQL in Action"

Part 1

Chapter 1

Listing 1.13 - GraphQL query for the Star Wars example
{
  person(personID: 4) {
    name
    birthYear
    homeworld {
      name
    }
    filmConnection {
      films {
        title
      }
    }
  }
}

Chapter 2

Listing 2.1 - Query for person
{
  person(personID: 4) {
    name
    birthYear
  }
}
Listing 2.9 - Query for the last 10 repositories for logged-in user
{
  viewer {
    repositories(last: 10) {
      nodes {
        name
        description
      }
    }
  }
}
Listing 2.10 - Query for all GitHub-supported licenses
{
  licenses {
    name
    url
  }
}
Listing 2.11 - Query for the first 10 issues of a repository
{
  repository(owner: "facebook", name: "graphql") {
    issues(first: 10) {
      nodes {
        title
        createdAt
        author {
          login
        }
      }
    }
  }
}
Listing 2.12 - Mutation to "star" a repository
mutation {
  addStar(input: {starrableId: "MDEwOlJlcG9zaXRvcnkzODM0MjIyMQ=="}) {
    starrable {
      stargazers {
        totalCount
      }
    }
  }
}
Listing 2.13 - Query to find an id of a repository
{
  repository(name: "graphql", owner: "facebook") {
    id
  }
}
Listing 2.14 - Query for the details of one issue under a repository
query GetIssueInfo {
  repository(owner: "jscomplete", name: "graphql-in-action") {
    issue(number: 1) {
      id
      title
    }
  }
}
Listing 2.15 - Mutation to add a comment to a repository issue
mutation AddCommentToIssue {
  addComment(input: {
    subjectId: "MDU6SXNzdWUzMDYyMDMwNzk=",
    body: "Hello GraphQL"
  }) {
    commentEdge {
      node {
        createdAt
      }
    }
  }
}
Listing 2.16 - Example GraphQL introspective query
{
  __schema {
    types {
      name
      description
    }
  }
}
Listing 2.17 - Query for supported fields under a Commit object
{
  __type(name: "Commit") {
    fields {
      name
      args {
        name
      }
    }
  }
}
Listing 2.18 - Mutation to add a test Note record in your GraphFront account
mutation AddTestNote {
  createNote(
    apiKey: "YOUR GRAPHFRONT API KEY",
    input: {
      number: 1,
      body: "This is just a test",
      isMarkDown: false,
    }
  ) {
    id
  }
}
Listing 2.19 - Query for all the note records from your GraphFront account
query GetAllNotes {
  viewer(apiKey: "YOUR GRAPHFRONT API KEY") {
    notes {
      id
      number
      body
      isMarkDown
      createdAt
      updatedAt
    }
  }
}

Chapter 3

Listing 3.3 - Query for one organization information
query OrgInfo {
  organization(login: "facebook") {
    name
    description
    websiteUrl
  }
}
Listing 3.4 - Query for first 10 repositories under organization
query First10Repos {
  organization(login: "facebook") {
    name
    description
    websiteUrl
    repositories(first: 10) {
      nodes {
        name
      }
    }
  }
}
Listing 3.5 - Query for first 10 alphabetically-ordered repositories under organization
query orgReposByName {
  organization(login: "facebook") {
    repositories(first:10, orderBy:{field: NAME, direction: ASC}) {
      nodes {
        name
      }
    }
  }
}
Listing 3.6 - Query for top-10 most popular repositories under organization
query OrgPopularRepos {
  organization(login: "facebook") {
    repositories(first:10, orderBy:{field: STARGAZERS, direction: DESC}) {
      nodes {
        name
      }
    }
  }
}
Listing 3.7 - Query example for working with cursors under edges
query OrgRepoConnectionExample {
  organization(login: "facebook") {
    repositories(first:10, orderBy:{field: STARGAZERS, direction: DESC}) {
      edges {
        cursor
        node {
          name
        }
      }
    }
  }
}
Listing 3.8 - Query example to fetch second page of popular repositories
query OrgPopularReposPage2 {
  organization(login: "facebook") {
    repositories(
      first: 10,
      after: "Y3Vyc29yOnYyOpLNOE7OAeErrQ==",
      orderBy: {field: STARGAZERS, direction: DESC}
    ) {
      edges {
        cursor
        node {
          name
        }
      }
    }
  }
}
Listing 3.9 - Query example for meta pagination information
query OrgReposMetaInfoExample {
  organization(login: "facebook") {
    repositories(
      first: 10,
      after: "Y3Vyc29yOnYyOpLNOE7OAeErrQ==",
      orderBy: {field: STARGAZERS, direction: DESC}
    ) {
      totalCount
      pageInfo {
        hasNextPage
      }
      edges {
        cursor
        node {
          name
        }
      }
    }
  }
}
Listing 3.10 - Query example for using field arguments to search
query SearchExample {
  repository(owner: "twbs", name: "bootstrap") {
    projects(search: "v4.1", first: 10) {
      nodes {
        name
      }
    }
  }
}
Listing 3.11 - Query example for using field arguments to filter
query FilterExample {
  viewer {
    repositories(first: 10, affiliations: OWNER) {
      totalCount
      nodes {
        name
      }
    }
  }
}
Listing 3.12 - Example for using arguments to provide input values to a mutation
mutation StarARepo {
  addStar(input: {starrableId: "MDEwOlJlcG9zaXRvcnkzODM0MjIyMQ=="}) {
    starrable {
      stargazers {
        totalCount
      }
    }
  }
}
Listing 3.13 - Profile information query
query ProfileInfo {
  user(login: "samerbuna") {
    name
    company
    bio
  }
}
Listing 3.14 - Profile information query with alias
query ProfileInfoWithAlias {
  user(login: "samerbuna") {
    name
    companyName: company
    bio
  }
}
Listing 3.15 - Query for all supported directives
query AllDirectives {
  __schema {
    directives {
      name
      description
      locations
      args {
        name
        description
        defaultValue
      }
    }
  }
}
Listing 3.16 - Using variables for argument values
query OrgInfo($orgLogin: String!) {
  organization(login: $orgLogin) {
    name
    description
    websiteUrl
  }
}
Listing 3.17 - Using default values for variables
query OrgInfoWithDefault($orgLogin: String = "facebook") {
  organization(login: $orgLogin) {
    name
    description
    websiteUrl
  }
}
Listing 3.18 - Example for using the @include directive
query OrgInfo($orgLogin: String!, $fullDetails: Boolean!) {
  organization(login: $orgLogin) {
    name
    description
    websiteUrl @include(if: $fullDetails)
  }
}
Listing 3.19 - Example for using the @skip directive
query OrgInfo($orgLogin: String!, $partialDetails: Boolean!) {
  organization(login: $orgLogin) {
    name
    description
    websiteUrl @skip(if: $partialDetails)
  }
}
Listing 3.20 - Using @include and @skip together
query OrgInfo($orgLogin: String!, $partialDetails: Boolean!) {
  organization(login: $orgLogin) {
    name
    description
    websiteUrl @skip(if: $partialDetails) @include(if: false)
  }
}
Listing 3.24 - Example for a query with repeated sections
query MyRepos {
  viewer {
    ownedRepos: repositories(affiliations: OWNER, first: 10) {
      nodes {
        nameWithOwner
        description
        forkCount
      }
    }
    orgsRepos: repositories(affiliations: ORGANIZATION_MEMBER, first: 10) {
      nodes {
        nameWithOwner
        description
        forkCount
      }
    }
  }
}
Listing 3.25 - Using fragments to minimize repetitions in GraphQL operations
query MyRepos {
  viewer {
    ownedRepos: repositories(affiliations: OWNER, first: 10) {
      ...repoInfo
    }
    orgsRepos: repositories(affiliations: ORGANIZATION_MEMBER, first: 10) {
      ...repoInfo
    }
  }
}

fragment repoInfo on RepositoryConnection {
  nodes {
    nameWithOwner
    description
    forkCount
  }
}
Listing 3.34 - Inline fragment example
query InlineFragmentExample {
  repository(owner: "facebook", name: "graphql") {
    ref(qualifiedName: "master") {
      target {
        ... on Commit {
          message
        }
      }
    }
  }
}
Listing 3.35 - Example for a GraphQL union type
query RepoUnionExample {
  repository(owner: "facebook", name: "graphql") {
    issueOrPullRequest(number: 3) {
      __typename
    }
  }
}
Listing 3.36 - Using inline fragments with union types
query RepoUnionExampleFull {
  repository(owner: "facebook", name: "graphql") {
    issueOrPullRequest(number: 5) {
      ... on PullRequest {
        merged
        mergedAt
      }
      ... on Issue {
        closed
        closedAt
      }
    }
  }
}
Listing 3.37 - Query a union-type search field
query TestSearch {
  search(first: 100, query: "graphql", type: USER) {
    nodes {
      ... on User {
        name
        bio
      }
      ... on Organization {
        login
        description
      }
    }
  }
}

Part 2

Chapter 4

Chapter 5

Chapter 6

Chapter 7

Part 3

Chapter 8

Chapter 9