Issue retrieving data via API

Hi,

I’m trying to access OpenTargets data through API but keep getting a 400 error. I’ve tried two approaches getting the same error:

  1. First, I constructed the query using the playground and then adapted it as an R script following the examples provided at the EBI OpenTargets API training course from September 2021. I got the error 400.

  2. Then, to check if the error was related to the script I had created, I downloaded the example scripts provided at that same training course, getting the same error.

I’ll post here both scripts:

EBI training example:

# Install relevant library for HTTP requests
library(httr)

# Set gene_id variable
gene_id <- "ENSG00000091831"

# Build query string
query_string = "
  query target($ensemblId: String!){
    target(ensemblId: $ensemblId){
      id
      approvedSymbol
      approvedName
      bioType
      genomicLocation{
        chromosome
        start
        end
        strand
      }
    }
  }
"

# Set base URL of GraphQL API endpoint
base_url <- "https://api.platform.opentargets.org/api/v4/graphql"

# Set variables object of arguments to be passed to endpoint
variables <- list("ensemblId" = gene_id)

# Construct POST request body object with query string and variables
post_body <- list(query = query_string, variables = variables)

# Perform POST request
r <- POST(url=base_url, body=post_body, encode='json')

# Print data to RStudio console
print(content(r)$data)

The query I’m interested in:

library(httr)
ensemblId <- "ENSG00000064012"
query_string <- "
  query search{
    target($ensemblId: String!) {
      functionDescriptions
      knownDrugs {
        uniqueDrugs
        count
        rows {
          drugType
          mechanismOfAction
          disease {
            name
          }
          drug{
            name
            id
            isApproved
          }
        }
      }
    }
  }
"

base_url <- "https://api.platform.opentargets.org/api/v4/graphql"

# Set variables object of arguments to be passed to endpoint
variables <- list("ensemblId" = ensemblId)

# Construct POST request body object with query string and variables
post_body <- list(query = query_string, variables = variables)

# Perform POST request
r <- POST(url=base_url, body=post_body, encode='json')

# Print data to RStudio console
print(content(r)$data)

Hi @Ipediez!

Thank you for reaching out! You have just pointed out an inaccuracy in our training material. The field name is not bioType instead biotype. Other than that the query looks good. Your query of interest looks like this.

You need to provide the query variable as this:
{ "ensemblId": "ENSG00000064012" }

When not sure about the query, I recommend to use our graphql playground here. This tool provides suggestions on the accessible field names and the documentation of the schema is just right there. If you want to reproduce a content of a widget, you can get the graphql request by clicking the “API query” button.