Skip to content

drklrd/graphql-tryout

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL Basics

Based on the resource : Building Scalable APIs with GraphQL By Samer Buna.

query Test {                         
   graphQLHub
   github {
    user(username:"drklrd") {
      id
      company
      avatar_url
      repos{
        name
      }
    }
    
  }
}

Fields : Like 'graphQLHub','github','repo'....

Two types of fields :

  • scalar (graphQLHub,avatar_url,id,company)
  • complex (github,user,repos)
  • 'repos' represent list of objects
query ListOfCommits {
  github {
    repo(name:"graphql",ownerUsername:"facebook"){
      commits {
        message
        date
      }
    }
  }
}

What are resolver functions ? Try to find answer

Scalar fields are the basic types in GraphQl schema(they are primitive types)

Variables

query Test($currentUserName: String!) {
   graphQLHub
   github {
    user(username: $currentUserName) {
      id
      company
      avatar_url
      repos{
        name
      }
    }
    
  }
}

and in query variables :

{
  "currentUserName": "drklrd"
}

Directives:

Directives can be used to alter the GraphQL runtime execution

query Test(
  $currentUserName: String!,
  $includeRepos : Boolean!
) {
   graphQLHub
   github {
    user(username: $currentUserName) {
      id
      company
      avatar_url
      repos @include(if:$includeRepos){
        name
      }
    }
    
  }
}

and in query variables :

{
  "currentUserName": "drklrd",
  "includeRepos": false  
}

Aliases :

query Test(
  $currentUserName: String!,
  $includeRepos : Boolean!
) {
   graphQLHub
   github {
    user(username: $currentUserName) {
      githubid:id
      company
      avatar_url
      repos @include(if:$includeRepos){
        name
      }
    }
    
  }
} 

and also ...

query Test(
  $user1: String!,
  $user2 : String!
) {
   graphQLHub
   github {
    user1 : user (username: $user1) {
      githubid:id
      company
      avatar_url
    }
    user2 : user (username: $user2) {
      githubid:id
      company
      avatar_url
    }
    
  }
} 

with query variables

{
  "user1": "drklrd",
  "user2": "github"
}

Fragments

query Test(
  $user1: String!,
  $user2 : String!
) {
   graphQLHub
   github {
    user1 : user (username: $user1) {
      ...UserInfo
    }
    user2 : user (username: $user2) {
      ...UserInfo
    }
    
  }
} 

fragment UserInfo on GithubUser {
  id
  company
  avatar_url
}

Inline fragments

query Test {
  github{
    repo(name:"graphql", ownerUsername:"facebook"){
      commits{
        message
        author{
          ... on GithubUser{
            login
          }
          ... on GithubCommitAuthor{
            email
          }
        }
      }
    }
  }
}

Mutations

mutation AddResource($input : CreateLinkInput!){
  createLink(input : $input){
    linkEdge{
      node {
        id
      }
    }
  }
}

and query variables

{
  "input" : {
    "title" : "GraphQlHub",
    "url" : "https://www.graphqlhub.com",
    "clientMutationId" : 12
  }
}

Querying unions

{
  me(key:"0000"){
    email
    fullName
    activities{
      ... on Contest{
        title
      }
      ... on Name {
        label
      }
    }
    
  }
}

About

GraphQL tryout based on the resource : Building Scalable APIs with GraphQL By Samer Buna.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages