Skip to main content

Predefined Output Queries

These queries have a fixed structure for the response. The client cannot alter the fields that are returned; they are determined by the query itself. This section provides examples of predefined output queries using both static and parametrized approaches.

Static and Parametrized Queries

There are two approaches to constructing queries to interact with the API:

Static GraphQL Queries

A static GraphQL query is fixed and predefined in the code. It does not take any parameters and always fetches the same type of data. This method involves specific, non-reusable queries that fetch predefined data sets.

Advantages:

  • Simple to implement and debug
  • Predictable and consistent response structure

Disadvantages:

  • Limited flexibility and reusability
  • Not adaptable to different data requirements

Example: To retrieve a list of assets supported by Qytrees for a specific product type and source, you might use:

query myAssets {
assetNames(instrumentType: SPOT, source: DERIBIT)
}

Explanation:

  • query myAssets begins the query block and serves as an optional identifier. Here, myAssets is a user-defined name, while query is a keyword in GraphQL.
  • assetNames corresponds to a schema operation in Qytrees, requiring instrumentType and source as parameters. Both parameters are Enums with predefined options. This query returns the list of assets available in Qytrees for instrumentType = SPOT and source = DERIBIT. The output is predefined and cannot be altered by the user.

Running the query in the GraphQL IDE

simple_queries

Please note that the Run button is now labeled as myAssets, as specified above.

Running the Query in Python

The code was introduced in the previous page Interacting with the GraphQL API. Below is the key snippet for reference:

query = """
{
assetNames(instrumentType: SPOT, source: DERIBIT)
}
"""
try:
payload = {'query': query}
api_result = client.make_api_request(payload)
print(api_result)
except Exception as e:
print(f"An error occurred: {e}")

Parametrized Queries

GraphQL also allows for the parameterization of queries, making them reusable.

Advantages:

  • Increased flexibility and re-usability
  • Adaptable to different data requirements

Disadvantages:

  • Slightly more complex to implement
  • Requires handling of variables

Example: Transform the above example into a parametrized query:

query myAssets($instrumentType: InstrumentType!, $source: Source!) {
assetNames(instrumentType: $instrumentType, source: $source)
}

Variables JSON:

{
"instrumentType": "SPOT",
"source": "DERIBIT"
}

Explanations:

  • Variables instrumentType and source are defined in the query to allow flexibility.
  • assetNames is an operation defined in the Qytrees schema that requires InstrumentType and Source as parameters, utilizing the variables mentioned above.
  • Values are specified in a separate JSON variable, enabling query re-usability with different parameters.

Running the Query in the GraphQL IDE

simple_queries_variables

Running Parametrized Queries with Python

To execute the parametrized query using Python you can use the below code snippet:

query = """
query myAssets($instrumentType: InstrumentType!, $source: Source!) {
assetNames(instrumentType: $instrumentType, source: $source)
}
"""
variables = {
"instrumentType": "SPOT",
"source": "DERIBIT"
}
payload = {
'query': query,
'variables': variables
}
try:
api_result = client.make_api_request(payload)
print(api_result)
except Exception as e:
print(f"An error occurred: {e}")

For examples of interacting with GraphQL using other frameworks, please refer to the API Tokens section of the Developer Portal after login.