How to use GraphQL with Python, Javascript, and Ruby

In the last few articles, we talked argued using GraphQL APIs for blockchain data and how to create your first blockchain GraphQL query. In this article, we will show how to call GraphQL APIs using different programming languages. 

However, you should always use our GraphQL playground to create and test your queries before embedding in your code.

Basics

Before starting, let’s cover some basics.

We will be using the following GraphQL query in all our examples.

query{
bitcoin{
blocks{
count
}
}
}

Now let’s see how to call GraphQL APIs using different programming languages.

GraphQL APIs using cURL

cURL allows using HTTP methods directly through our terminal. It is best for quick testing of any API request.

curl \
-X POST \
-H "Content-Type: application/json" \
--data '{ "query": "{ bitcoin { blocks {count} } }" }' \
https://graphql.bitquery.io/

GraphQL APIs using Python

Python is a beginner-friendly programming language. The example below shows how to call a GraphQL API using python.

GraphQL Python Libraries 

The libraries below are the most widely used libraries using GraphQL with python.

  • Graphene — Graphene is an opinionated Python library for building GraphQL schemas/types fast and easily.
  • Ariadne — Ariadne is a Python library for implementing GraphQL servers using a schema-first approach.
  • Strawberry — Strawberry is a new GraphQL library for Python 3, inspired by data classes.

GraphQL APIs using Javascript

Javascript is one of the most adopted programming languages among developers all over the world. You can call a post request to get results from GraphQL APIs.

GraphQL Javascript(NodeJS) Libraries

The following are the most popular javascript libraries for GraphQL.

  • GraphQL.js — The JavaScript reference implementation for GraphQL, a query language for APIs created by Facebook.
  • graphql-request — Minimal GraphQL client supporting Node and browsers for scripts or simple apps.
  • Apollo client — Apollo Client is a fully-featured caching GraphQL client with integrations for React, Angular, and more. It allows you to build UI components that fetch data via GraphQL easily.

GraphQL APIs using Ruby

Ruby is also one of the most simple programming languages. The following example shows how to call a GraphQL API using ruby. 

GraphQL Ruby Libraries

You can use the libraries below to use GraphQL with ruby.

  • graphql-ruby — Ruby implementation of Facebook’s GraphQL.
  • graphql-client — A Ruby library for declaring, composing, and executing GraphQL queries.
  • graphql-batch — A query batching executor for the graphql gem.
  • agoo — Ruby web server that implements Facebook’s GraphQL.
  • GQLi — A GraphQL client and DSL. Allowing to write queries in native Ruby.

Additional Resources

To see the examples of other programming languages, check the official website of GraphQL. In addition, to learn more about more GraphQL tools and libraries, check Awesome GraphQL.

If you have any questions or need help with your blockchain investigation, just hop on our Telegram channel. Also, let us know if you are looking for blockchain data APIs.

You might also be interested in:

About Bitquery

Bitquery is a set of software tools that parse, index, access, search, and use information across blockchain networks in a unified way. Our products are:

  • Digital Assets API provides index information related to all major cryptocurrencies, coins, and tokens.
  • DEX API provides real-time deposits and transactions, trades, and other related data on different DEX protocols like Uniswap, Kyber Network, Airswap, Matching Network, etc.

If you have any questions about our products, ask them on our Telegram channel or email us at hello@bitquery.io. Also, subscribe to our newsletter below, we will keep you updated with the latest in the cryptocurrency world.