Using Query Variables

Learn how to use query variables in your requests to the API

Edgar Nuñez avatar
Written by Edgar Nuñez
Updated over a week ago

Query variables can used with the Omega Point API to pass in input parameters to your query, which can be particularly useful to help structure your development workflow. This article will walk through the steps to retrieve position sets from a portfolio for a date range, using query variables.

Portfolio ID

When running your own requests, please be sure to replace the portfolio ID with an appropriate ID from your own account. If you're not familiar with how to obtain a portfolio ID, please see the article on Requesting Portfolios.

Retrieving Position Sets

If we want to request a portfolio's positions sets for the date range 2017-01-01 to 2017-06-30, the GraphQL structure would look like the following:

query {
    portfolio(id: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") {
        name
        dates(from: "2017-01-01", to: "2017-06-30") {
            date
            equities {
                id {
                    ticker
                    mic
                }
            economicExposure
        }
    }
}

The above query could be used in a cURL command, as in the following example:

curl \
    -XPOST \
    -H "Content-Type:application/json" \
    -H "${queryAuth}" \
    -d '{"query": "query { portfolio (id: \"794f35ca-6175-4f52-8451-dadacb3f8117\") { name, dates (from: \"2017-01-01\", to: \"2017-06-30\") { date, equities { id { ticker, mic }, economicExposure } } } }"}' \
    https://api.ompnt.com/graphql

How Query Variables Are Used

Now let's focus on the query portion of the request. If we want to pass in variables for the portfolio ID, start date, and end date, we can alter the query structure to use portfolioId, fromDate, and toDate as follows:

query($portfolioId: String!, $fromDate: Date!, $toDate: Date!) {
    portfolio(id: $portfolioId) {
        name
        dates(from: $fromDate, to: $toDate) {
            date
            equities {
                id {
                    ticker
                    mic
                }
            economicExposure
        }
    }
}

Setting the Variables

Query variables must be in JSON format and are passed in under “variables” as part of the request. For the above query, we must set the values for portfolioId, fromDate, and toDate. The JSON needed in this case would be:

variables: {
  "portfolioId": "794f35ca-6175-4f52-8451-dadacb3f8117",
  "fromDate": "2017-01-01",
  "toDate": "2017-06-30"
}

Putting It All Together

Let's combine the query and variables to establish the following entity:

{
    "query": "query($portfolioId: String!, $fromDate: Date!, $toDate: Date!) {
        portfolio(id: $portfolioId) {
            name
            dates(from: $fromDate, to: $toDate) {
                date
                equities {
                    id {
                        ticker
                        mic
                    }
                economicExposure
            }
        }
    }",
    "variables": {
        "portfolioId": "794f35ca-6175-4f52-8451-dadacb3f8117",
        "fromDate": "2017-01-01",
        "toDate": "2017-06-30"
    }
}

To simplify our cURL command, let's store the above entity as a string in a new bash variable:

queryWithVariables='{"query": "query($portfolioId: String!, $fromDate: Date!, $toDate: Date!) { portfolio (id: $portfolioId) { name dates(from: $fromDate, to: $toDate) { date equities { id { ticker mic } economicExposure } } } }", "variables": "{\"portfolioId\": \"794f35ca-6175-4f52-8451-dadacb3f8117\", \"fromDate\": \"2017-01-01\", \"toDate\": \"2017-06-30\"}"}'

Now we can use the query string in the cURL command as follows:

curl \
    -XPOST \
    -H "Content-Type:application/json" \
    -H "${queryAuth}" \
    -d "${queryWithVariables}" \
    https://api.ompnt.com/graphql
Did this answer your question?