Coder Social home page Coder Social logo

ghql's Introduction

ghql

Build Status codecov.io

ghql - general purpose GraphQL client

GraphQL - http://graphql.org

Examples of GraphQL APIs:

Github Authentication

See https://developer.github.com/early-access/graphql/guides/accessing-graphql/ for getting an OAuth token.

Store the token in a env var called GITHUB_GRAPHQL_TOKEN before trying this pkg.

Install

Development version

devtools::install_github("ropensci/ghql")
library("ghql")
library("jsonlite")

initialize client

library("httr")
token <- Sys.getenv("GITHUB_GRAPHQL_TOKEN")
cli <- GraphqlClient$new(
  url = "https://api.github.com/graphql",
  headers = add_headers(Authorization = paste0("Bearer ", token))
)

load schema

Since not every GraphQL server has a schema at the base URL, have to manually load the schema in this case

cli$load_schema()

basic query

Make a Query class object

qry <- Query$new()
qry$query('myquery', 'query { }')
qry
#> <ghql: query>
#>   queries:
#>     myquery
qry$queries
#> $myquery
#>  
#>  query { }
qry$queries$myquery
#>  
#>  query { }
cli$exec(qry$queries$myquery)
#> $data
#> named list()

Gives back no result, as we didn't ask for anything :)

Get some actual data

qry$query('getdozedata', '{
  repositoryOwner(login:"sckott") {
    repositories(first: 5, orderBy: {field:PUSHED_AT,direction:DESC}, isFork:false) {
      edges {
        node {
          name
          stargazers {
            totalCount
          }
        }
      }
    }
  }
}')
qry
#> <ghql: query>
#>   queries:
#>     myquery    
#>     getdozedata
qry$queries$getdozedata
#>  
#>  {
#>   repositoryOwner(login:"sckott") {
#>     repositories(first: 5, orderBy: {field:PUSHED_AT,direction:DESC}, isFork:false) {
#>       edges {
#>         node {
#>           name
#>           stargazers {
#>             totalCount
#>           }
#>         }
#>       }
#>     }
#>   }
#> }
cli$exec(qry$queries$getdozedata)
#> $data
#> $data$repositoryOwner
#> $data$repositoryOwner$repositories
#> $data$repositoryOwner$repositories$edges
#>             node.name node.totalCount
#> 1   sckott.github.com               8
#> 2 usdaplantsapistatus               0
#> 3       usdaplantsapi              19
#> 4            rforcats              34
#> 5             soylocs               2

run a local GraphQL server

  • Copy the server.js file from this package located at inst/server.js somewhere on your machine. Can locate it on your machine like system.file("js/server.js", package = "ghql"). Or you can run the file from where it's at, up to you.
  • Make sure node is installed. If not, see https://nodejs.org
  • Run node server.js
  • Navigate to your browser - go to http://localhost:4000/graphql
  • Back in R, user that URL to connect
(cli <- GraphqlClient$new("http://localhost:4000/graphql"))
#> <ghql client>
#>   url: http://localhost:4000/graphql
xxx <- Query$new()
xxx$query('query', '{
  __schema {
    queryType {
      name, 
      fields {
        name,
        description
      }
    }
  }
}')
cli$exec(xxx$queries$query)
#> $data
#> $data$`__schema`
#> $data$`__schema`$queryType
#> $data$`__schema`$queryType$name
#> [1] "Query"
#> 
#> $data$`__schema`$queryType$fields
#>    name description
#> 1 hello            
#> 2  name 

Meta

  • Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

ghql's People

Contributors

sckott avatar stevenysw avatar

Watchers

James Cloos avatar Athanasiadis Ioannis avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.