The GraphQL API is intended to be a private API. The API should only be accessible to the developers running the Mesh node and should not be exposed to the public. The API runs on a separate port from the peer-to-peer protocols and access to it can be controlled via a firewall.
Peers in the network do not use the GraphQL API and instead use a peer-to-peer PubSub mechanism (usually this is not something you need to worry about).
About GraphQL
GraphQL is a structured query language for APIs. It:
Is transport layer agnostic (e.g. HTTP, WebSockets, or calling a function).
Is type-safe and uses well-structured schemas.
Has wide support across many programming languages.
Has great tooling (including automatic doc generation and playground environments).
Features built-in support for subscriptions.
Allows clients to only receive the data that they need in the format that they need it.
Playground Environment
We have deployed a public playground environment for exploring the GraphQL API. You can access the playground at https://meshmock.spaceship.0x.org/. It supports auto-completion, syntax highlighting, and subscriptions. In addition, interactive documentation for the API can be found by clicking the "docs" button on the righthand side of the screen. See the Example Queries section below for some queries to try.
This section includes some example queries which you can copy and paste in the playground. Of course, you would typically write queries programmatically with a GraphQL client, not by manually writing them. This is just for illustrative purposes.
Getting a Specific Order
You can get the details for any order by its hash:
You can get all orders via the orders query. By default, it will return up to 100 orders at a time sorted by their hash. You can also change the number of orders returned via the limit argument.
The orders query supports many different options. Here's an example of how to get orders with a minimum expirationTimeSeconds and minimum remainingFillableAssetAmount. You can use this to exclude dust orders and orders which may expire too soon.
We recommend paginating through orders by using filters and limit. So for example, if you want to sort orders by their hash (which is the default), you first send a query without any filters:
The orders in the response will be sorted by hash (which is the default). Look at the last order you received, which in this case has a hash of 0x75d2b56b11f21235ec8faec8be9d081090678cf62f5c69fa118236d829424719. Send the next request by using the last hash you received in a filter:
This will return any orders with a hash greater than 0x75d2b56b11f21235ec8faec8be9d081090678cf62f5c69fa118236d829424719. Repeat this process, changing the hash each time, until there are no orders left.
There may be orders added or removed while you are in the process of paginating. Following this method guarantees that:
No order will be included more than once.
Any order which was present at the start of pagination and at the end of pagination will be included.
Any order which was added or removed after pagination started may or may not be included.
Query Fragments
GraphQL requires you to specify all the fields that you want included in the response. However, you can use query fragments to avoid repeating the same fields over and over. Here's an example of a query fragment that includes all the fields of an order.