Giter Site home page Giter Site logo

ghql's Introduction

ghql

Project Status: Active โ€“ The project has reached a stable, usable state and is being actively developed. cran checks R-check codecov.io rstudio mirror downloads cran version

ghql - a GraphQL client for R

GraphQL - https://graphql.org

Examples of GraphQL APIs:

Other GraphQL R packages:

  • graphql - GraphQL query parser
  • gqlr - GraphQL server and query methods

Install

CRAN version

install.packages("ghql")

Development version

remotes::install_github("ropensci/ghql")
library("ghql")
library("jsonlite")
library("dplyr")

Package Documentation

https://docs.ropensci.org/ghql/

Meta

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

ghql's People

Contributors

ateucher avatar denironyx avatar eddelbuettel avatar friep avatar jmbarrios avatar maelle avatar mnazarov avatar mpadge avatar salim-b avatar sckott avatar stevenysw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ghql's Issues

How to use R variable in query call

I am attempting to query a GraphQL API using a non-hard-coded ID. My code is

library(ghql)
library(jsonlite)
library(httr)


client <- GraphqlClient$new(url = "http://genetics-api.opentargets.io/graphql")

qry <- Query$new()

qry$query('query', '
          {
            geneInfo(geneId: "ENSG00000137033") {
            id
            symbol
            chromosome
            start
            end
            bioType
            __typename
            }
          }')





responses <- client$exec(qry$queries$query)

Which works fine. When the ID is given to qry$query() in a non-hard-coded way:

test_gene <- 'ENSG00000137033'

test_query <- paste0(
'{
  geneInfo(geneId: "', test_gene, '") {
    id
    symbol
    chromosome
    start
    end
    bioType
    __typename
  }
}')


qry$query(paste0('query', test_query))

it breaks. I get the error:
Error in make_query(x) : argument "x" is missing, with no default

There must be a way around this, what am I missing?

Thanks!

-Kyle

Error: Invalid Token

I'm looking to establish a connection between R and a GraphQL Database via ghql, but the authorization is failing and I'm not sure why.

For

conn <- GraphqlClient$new(url = link, 
                          headers = list(Authorization = paste0("Bearer ", token)))

conn$ping()

I get a

Error in curl::curl_fetch_memory(x$url$url, handle = x$url$handle) : 
  schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN (0x80090308) - The token supplied to the function is invalid.

When accessing the GraphQL database's visual interface, however, the token is working just fine when used in the request header as

{
"Authorization": "Bearer -token-"
}

Changing the header structure or content throws the same error, so I'm suspecting the problem is not with the actual token, but the data structure or maybe its encoding? But I'm not sure why I'm getting the error, and from curl specifically. Any help appreciated.

I'm running curl 4.3 and ghql 0.1.0 under R 4.0.3

Adding comment(s) to query results in "unexpected end of document" error

According to the GraphQL spec, comments can be added to queries using # (like in R). Unfortunately, adding comments within the string results in an error "unexpected end of document" when executing the query (adding the query to the Query object works). I tried the reprex queries with and without comments in the GitHub Explorer respectively the GitLab GraphiQL Explorer where they work.

I also ran the following query (adapted from the GitLab Get started) in my zsh shell with curl and it worked.

GRAPHQL_TOKEN="mytoken"
curl "https://gitlab.com/api/graphql" --header "Authorization: Bearer $GRAPHQL_TOKEN" --header "Content-Type
: application/json" --request POST --data "{\"query\": \"query {currentUser {name}}\n #a comment\"}"

Why do I think this is an important feature given comments are not really necessary to the functionality of this package? Well, I wrote a blog post with ghql and wanted to explain the increasingly complex queries in-code (see especially the query for step 2 in the blog post).

If you think this is a good idea, I'd be happy to have a look myself and try to fix this :)

If i overlooked a function argument that would have enabled this, please let me know. ๐Ÿ™ˆ

Session Info
devtools::session_info()
#> โ”€ Session info โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
#>  setting  value                       
#>  version  R version 4.0.3 (2020-10-10)
#>  os       macOS Catalina 10.15.7      
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Europe/Berlin               
#>  date     2021-01-22                  
#> 
#> โ”€ Packages โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
#>  package     * version date       lib source                            
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.2)                    
#>  callr         3.5.1   2020-10-13 [1] CRAN (R 4.0.2)                    
#>  cli           2.2.0   2020-11-20 [1] CRAN (R 4.0.2)                    
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.2)                    
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.2)                    
#>  devtools      2.3.2   2020-09-18 [1] CRAN (R 4.0.2)                    
#>  digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.2)                    
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.2)                    
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.1)                    
#>  fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.2)                    
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)                    
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)                    
#>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.2)                    
#>  htmltools     0.5.0   2020-06-16 [1] CRAN (R 4.0.2)                    
#>  knitr         1.30    2020-09-22 [1] CRAN (R 4.0.2)                    
#>  lifecycle     0.2.0   2020-03-06 [1] CRAN (R 4.0.2)                    
#>  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.2)                    
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 4.0.2)                    
#>  pkgbuild      1.1.0   2020-07-13 [1] CRAN (R 4.0.2)                    
#>  pkgload       1.1.0   2020-05-29 [1] CRAN (R 4.0.2)                    
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.2)                    
#>  processx      3.4.4   2020-09-03 [1] CRAN (R 4.0.2)                    
#>  ps            1.4.0   2020-10-07 [1] CRAN (R 4.0.2)                    
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.2)                    
#>  R6            2.5.0   2020-10-28 [1] CRAN (R 4.0.2)                    
#>  remotes       2.2.0   2020-07-21 [1] CRAN (R 4.0.2)                    
#>  rlang         0.4.10  2020-12-30 [1] CRAN (R 4.0.2)                    
#>  rmarkdown     2.6.4   2020-12-23 [1] Github (rstudio/rmarkdown@5c0aeb0)
#>  rprojroot     2.0.2   2020-11-15 [1] CRAN (R 4.0.2)                    
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.2)                    
#>  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.2)                    
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.2)                    
#>  testthat      3.0.0   2020-10-31 [1] CRAN (R 4.0.2)                    
#>  usethis       2.0.0   2020-12-10 [1] CRAN (R 4.0.2)                    
#>  withr         2.3.0   2020-09-22 [1] CRAN (R 4.0.2)                    
#>  xfun          0.20    2021-01-06 [1] CRAN (R 4.0.2)                    
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.2)                    
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library

Created on 2021-01-22 by the reprex package (v0.3.0)

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

# without comment - works 
qry_gh <- Query$new()
qry_gh$query('mydata', '{
  repositoryOwner(login:"friep") {
    repositories(first: 5, orderBy: {field:PUSHED_AT,direction:DESC}, isFork:false) {
      edges {
        node {
          name
          stargazers {
            totalCount
          }
        }
      }
    }
  }
}')
(x <- con_gh$exec(qry_gh$queries$mydata))
#> [1] "{\"data\":{\"repositoryOwner\":{\"repositories\":{\"edges\":[{\"node\":{\"name\":\"correlaid-utils\",\"stargazers\":{\"totalCount\":2}}},{\"node\":{\"name\":\"correlaid_md-ii\",\"stargazers\":{\"totalCount\":0}}},{\"node\":{\"name\":\"vaccc19de\",\"stargazers\":{\"totalCount\":4}}},{\"node\":{\"name\":\"vaccc19de_dashboard\",\"stargazers\":{\"totalCount\":6}}},{\"node\":{\"name\":\"hugo-website\",\"stargazers\":{\"totalCount\":0}}}]}}}}\n"
jsonlite::fromJSON(x)
#> $data
#> $data$repositoryOwner
#> $data$repositoryOwner$repositories
#> $data$repositoryOwner$repositories$edges
#>             node.name node.totalCount
#> 1     correlaid-utils               2
#> 2     correlaid_md-ii               0
#> 3           vaccc19de               4
#> 4 vaccc19de_dashboard               6
#> 5        hugo-website               0

# with comment(s) - unexpected end of document
qry_gh$query('mydata_with_comment', '{
  repositoryOwner(login:"friep") {
    # lets get all the repositories
    repositories(first: 5, orderBy: {field:PUSHED_AT,direction:DESC}, isFork:false) {
      edges {
        node {
          name
          stargazers {
            totalCount
          }
        }
      }
    }
  }
}')

(x <- con_gh$exec(qry_gh$queries$mydata_with_comment))
#> [1] "{\"errors\":[{\"message\":\"Unexpected end of document\"}]}\n"
jsonlite::fromJSON(x)
#> $errors
#>                      message
#> 1 Unexpected end of document


# GITLAB
token_gl <- Sys.getenv("GITLAB_GRAPHQL_TOKEN")
con_gl <- GraphqlClient$new(
  url = "https://gitlab.com/api/graphql",
  headers = list(Authorization = paste0("Bearer ", token_gl))
)

# without comment - works
qry_gl <- Query$new()
qry_gl$query('user_gitlab', '{
  user(username:"friep") {
      id
      name
      username
  }
}')
(x <- con_gl$exec(qry_gl$queries$user_gitlab))
#> [1] "{\"data\":{\"user\":{\"id\":\"gid://gitlab/User/2373431\",\"name\":\"Frie\",\"username\":\"friep\"}}}"
jsonlite::fromJSON(x)
#> $data
#> $data$user
#> $data$user$id
#> [1] "gid://gitlab/User/2373431"
#> 
#> $data$user$name
#> [1] "Frie"
#> 
#> $data$user$username
#> [1] "friep"

# with comment - again, unexpected document
qry_gl$query('user_gitlab_comments', '{
  # lets get the current user
  user(username:"friep") {
      id
      name
      username
  }
}')
(x <- con_gl$exec(qry_gl$queries$user_gitlab_comments))
#> [1] "{\"errors\":[{\"message\":\"Unexpected end of document\",\"locations\":[]}]}"
jsonlite::fromJSON(x)
#> $errors
#>                      message locations
#> 1 Unexpected end of document      NULL

# inline comments also don't work :( 
qry_gl$query('user_gitlab_comments_2', '{
  user(username:"friep") {
    id
    name # inline comment
    username
  }
}')
(x <- con_gl$exec(qry_gl$queries$user_gitlab_comments_2))
#> [1] "{\"errors\":[{\"message\":\"Unexpected end of document\",\"locations\":[]}]}"
jsonlite::fromJSON(x)
#> $errors
#>                      message locations
#> 1 Unexpected end of document      NULL

Created on 2021-01-22 by the reprex package (v0.3.0)

Mutation support?

I was now using the ghql library for some queries and they are working nicely. I don't find a related class for mutation operations...

Is there also support for GraphQL mutations?

Alternatively use GITHUB_PAT

GITHUB_PAT has been adopted by git2r and usethis as the environment setting for a GitHub PAT. It might be useful to integrate that into ghql (even if only optionally) as many users will already have this set. At present, they will also need to set GITHUB_GRAPHQL_TOKEN, potentially to the same value.

(*) My understanding is that both of these environment variables intend to store the same authentication, so reducing duplication seems worthwhile. There may (for all I know) be reasons to store these separately, so perhaps adding some logic to search for one or the other (giving one precedence) is still worthwhile.

This would have a flow-on effect to packages which also use ghql, e.g. ghrecipes (@maelle) and stellar (@mpadge).

exec returning charlist instead of object

From the example in the main README, response

I'm getting the following char vector. Is there a step I'm missing? Have the docs drifted behind expected usage now?

# looks like a valid response, albeit in unexpected form
> cli$exec(qry$queries$getdozedata)
[1] "{\"data\":{\"repositoryOwner\":{\"repositories\":{\"edges\":[{\"node\":{\"name\":\"Headstart\",\"stargazers\":{\"totalCount\":94}}},{\"node\":{\"name\":\"mregions\",\"stargazers\":{\"totalCount\":11}}},{\"node\":{\"name\":\"getlandsat\",\"stargazers\":{\"totalCount\":32}}},{\"node\":{\"name\":\"request\",\"stargazers\":{\"totalCount\":31}}},{\"node\":{\"name\":\"discgolf\",\"stargazers\":{\"totalCount\":7}}}]}}}}\n"

str(cli$exec(qry$queries$getdozedata))
 chr "{\"data\":{\"repositoryOwner\":{\"repositories\":{\"edges\":[{\"node\":{\"name\":\"Headstart\",\"stargazers\":{"| __truncated__

But looks like a legitimate set of data (albeit an escaped string).

My info as follows:

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] httr_1.3.1      jsonlite_1.5    ghql_0.0.4.9210

loaded via a namespace (and not attached):
[1] compiler_3.5.1 R6_2.2.2       curl_3.2      

How to get headers from connection execution?

So when using the github graqphql api a query returns in the headers information such as x-ratelimit-used which is important information when it comes to rate limiting.
I'd like to know how I can get this information such that I do not get rate limited.
Best,

help with headers and --data-binary?

Wonderful package. Can you give some guidance on how to present a query with headers as given in this curl excerpt

-H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: https://pharos-api.ncats.io' --data-binary

It isn't at all clear to me how to pass the --data-binary element.

I wrote the headers as

hlist =  list(
`Accept-Encoding`=c("gzip", "deflate", "br"),
`Content-Type`="application/json",
`Accept`="application/json",
`Connection`="keep-alive",
`DNT`="1",
`Origin`="https://pharos-api.ncats.io/graphql")

but server returns 500 on

library(ghql)
hlist =  list(
`Accept-Encoding`=c("gzip", "deflate", "br"),
`Content-Type`="application/json",
`Accept`="application/json",
`Connection`="keep-alive",
`DNT`="1",
`Origin`="https://pharos-api.ncats.io/graphql")


con = GraphqlClient$new(url="https://pharos-api.ncats.io/graphql", headers=hlist)

con$load_schema(schema_file="pharos_introspectionSchema.json")

qry = Query$new()

qry$query('myd', '{
interactingProteins{
  targets(filter: { associatedTarget: "PPBP" }) {
    targets(top:5) {
      name
      sym
      ppiTargetInteractionDetails {
        dataSources:ppitypes
        score
        interaction_type
        evidence
        p_ni
        p_int
        p_wrong
      }
    }
  }
}
}')

qry
xx = con$exec(qry$queries$myd)

Helper method to get all nodes and edges

One use case for GraphQL is visualizing the underlying graph, and this is typically done by extracting all the nodes and edges and then using a package such as igraph. While this can be done with standard R methods (see my attempts for example here), but this could maybe also done by helper methods provided by ghql. Would that be useful?

Variable support

GraphQL support variable definition as part of a query. It is consider a best practice to be explicit about what part of a query is dynamic, as we can see in https://graphql.org/learn/queries/#variables.
Issue #17 can use this characteristic as follows:

qry$query('getGeneInfo', '
  query getGeneInfo($genId: String!) {
    geneInfo(geneId: $genId) {
    id
    symbol
    chromosome
    start
    end
    bioType
    __typename
  }
}')

and variable data

{
  "genId": "ENSG00000137033"
}

Variable data can be build as a json encode of a named list object and added to the exec query method.

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.