Graftcode Context library
Graftcode Context provides a standardized way to access request context (headers and metadata) during Graftcode invocations. The library is available for multiple platforms and programming languages.
Overview
The RequestContext class provides a thread-safe (or context-aware) singleton that allows you to retrieve headers and custom metadata anywhere in your code during the request lifecycle.
Note
On the server side, headers are automatically set by Graftcode Gateway which hosts your code. On the client side (in Grafts), headers can be set using the GraftConfig class.
This is essential for accessing authentication tokens, correlation IDs, tenant information, and other request-scoped data propagated through Graftcode service calls.
Setting Headers in Grafts (Client Libraries)
Grafts are client libraries generated by Graftcode that allow you to call remote services. Unlike server-side code where headers are set automatically by Graftcode Gateway, in Grafts you need to set headers explicitly using the GraftConfig class.
The GraftConfig class is automatically generated as part of every Graft package by Graftcode Engine. It provides two methods for setting headers:
GraftConfig.setHeaders
Sets headers globally for all subsequent Graft invocations. Use this when you want to set headers once (e.g., at application startup or after authentication) and have them applied to all calls.
GraftConfig.invokeWithHeaders
Sets headers only for a specific function invocation. The headers are scoped to that single call and do not affect other invocations. Use this when you need different headers for different calls or want to temporarily override global headers.
Examples by Technology
import { GraftConfig, MyService } from '@graft/nuget-MyService';// Set headers globallyGraftConfig.setHeaders({'Authorization': 'Bearer token123','X-Correlation-Id': 'abc-123'});// Or set headers for a specific invocationconst result = GraftConfig.invokeWithHeaders(() => MyService.doSomething(),{ 'Authorization': 'Bearer different-token' });
Available Libraries
| Technology | Package Name | Package URL |
|---|---|---|
| Node.js | graftcode-context | npmjs.com/package/graftcode-context |
| .NET | Graftcode.Context | nuget.org/packages/Graftcode.Context |
| Java | com.graftcode:graftcode-context | central.sonatype.com/artifact/com.graftcode/graftcode-context |
| Python | graftcode-context | pypi.org/project/graftcode-context |
| PHP | graftcode/graftcode-context | packagist.org/packages/graftcode/graftcode-context |
| Ruby | graftcode-context | rubygems.org/gems/graftcode-context |
Installation
Install the library for your Receiver runtime from the public package registry:
npm install graftcode-context
| Technology | Package | Minimum runtime |
|---|---|---|
| Node.js / TypeScript | graftcode-context | Node.js >= 22.0.0 |
| .NET | Graftcode.Context | .NET Standard 2.1 or .NET 8.0 |
| Java / JVM | com.graftcode:graftcode-context | Java 8+ |
| Python | graftcode-context | Python >= 3.9 |
| PHP | graftcode/graftcode-context | PHP >= 7.4 or >= 8.0 |
| Ruby | graftcode-context | Ruby >= 3.1.0 |
Usage
Read headers and metadata from the current request context anywhere in your Receiver code during a Graftcode invocation:
import { RequestContext } from 'graftcode-context';const headers = RequestContext.current.getHeaders();const authToken = headers['Authorization'];const correlationId = headers['X-Correlation-Id'];const tenantId = headers['X-Tenant-Id'];
| Technology | Mechanism |
|---|---|
| .NET | [ThreadStatic] on RequestContext.Current |
| Java / JVM | ThreadLocal |
| Python | contextvars.ContextVar (async-safe with asyncio) |
| Node.js, PHP, Ruby | Context-aware singleton for the active request |