Query GraphQL APIs in Solidity
Beyond HTTP GET and POST, the Witnet Web Oracle also gives your smart contracts the ability to query GraphQL APIs with full data integrity thanks to its multi-layered decentralization model.
GraphQL APIs are just a special type of HTTP POST API. As such, you can query any GraphQL API using the HTTP POST functionality of the Witnet Web Oracle:
HTTP POST Requests in SolidityHowever, for the sake of cleaner code and making things easier, the witnet-requests
JavaScript library includes GraphQL as a type of data source. Please read below for an example on how to use it.
How to query GraphQL APIs from your Solidity smart contracts
The process of defining and performing GraphQL queries in Solidity using the Witnet oracle is very similar to that of HTTP GET or POST requests. Please make sure that you are familiar with how that works:
HTTP GET Requests in SolidityThe only difference exists when specifying the data sources. Here is an example of a GraphQL data source:
This example is using the Sushiswap Polygon subgraph as hosted by TheGraph to fetch the spot price of the VSQ/DAI price pair.
These are the arguments of HttpPostSource
:
URL to query, as a String
The GraphQL query, as a String
Headers, as a JavaScript object (optional)
There is nothing special or specific to the compilation, instantiation and deployment of HTTP POST requests. If you need help with that, please check out the guide for HTTP GET requests.
Last updated