Skip to main content
This page describes concepts that are shared across all Cosmo Streams handlers and apply to every available hook.

Operation Variables

If your operation defines variables, you can access them through the Variables method on the OperationContext interface.
variables := ctx.Operation().Variables()
subscription($customerId: Int!) {
  orderUpdates(customerId: $customerId) {
    id
  }
}
Example variables payload:
{
  "customerId": 123
}
Inside your handler, you can retrieve the variables using the Variables method:
variables := ctx.Operation().Variables()
customerId := variables.Get("customerId").GetInt() // = 123

Operation Arguments

If no variables are defined in the operation, or if you do not control the variables, you can access arguments directly via the OperationContext interface.
arguments := ctx.Operation().Arguments()
Consider an operation that defines multiple arguments:
subscription {
  orderUpdates(customerId: "123", status: PENDING) {
    id
    items(first: 5, sortBy: "price") { name }
  }
}
To access the customerId argument of the subscription field, use the Get method:
customerId := ctx.Operation().Arguments().Get("orderUpdates", "customerId").GetStringBytes()
Because orderUpdates is the root field, you can also resolve the field name dynamically:
// For OnPublishEvent handler
rootFieldName := ctx.PublishEventConfiguration().RootFieldName()

// For OnReceiveEvent and SubscriptionOnStart handler
rootFieldName := ctx.SubscriptionEventConfiguration().RootFieldName()

customerId := ctx.Operation().Arguments().Get(rootFieldName, "customerId").Value()
To access arguments on nested fields, such as the sortBy argument of the items field, use dot notation to reference the nested path.
sortBy := ctx.Operation().Arguments().Get("orderUpdates.items", "sortBy").GetStringBytes()
When a field uses an alias, the alias must be used as the key when accessing its arguments.
subscription {
  orderUpdates(customerId: "123", status: PENDING) {
    id
    cheapestItems: items(first: 3, sortBy: "price") { name }
    newestItems: items(first: 3, sortBy: "createdAt") { name }
  }
}
cheapestItemsSortBy := ctx.Operation().Arguments().Get("orderUpdates.cheapestItems", "sortBy").GetStringBytes()
newestItemsSortBy := ctx.Operation().Arguments().Get("orderUpdates.newestItems", "sortBy").GetStringBytes()
You can also work with more complex argument types, such as arrays and input objects.
subscription {
  orderUpdates(statuses: ["pending", "shipped"], filter: { minTotal: 100, currency: "USD" }) {
    id
  }
}
arguments := ctx.Operation().Arguments()

statuses := arguments.Get("orderUpdates", "statuses").GetArray()
filter := arguments.Get("orderUpdates", "filter").GetObject()
minTotal := filter.Get("minTotal").GetInt()
currency := filter.Get("currency").GetStringBytes()