Skip to content

External Service Integration

Connection Pooling

Optimize resource usage with smart connection management.

GraphQL Client

Type-safe GraphQL operations with automatic query validation.

HTTP Client

Configurable HTTP client with retry and circuit breaker support.

Database Clients

Optimized database access with connection pooling.

Message Queues

Reliable message queue integration for async operations.

Configuration options often include settings for reliability features like retries and circuit breakers. Learn more in Retry Policies.

import { defineWorkflow } from '@identity-flow/sdk';
import { createGraphqlClient } from '@identity-flow/sdk/bindings/graphql';
const GraphqlClient = () => createGraphqlClient({
url: 'https://api.example.com/graphql',
headers: {
'Authorization': 'Bearer ${process.env.API_TOKEN}'
},
retries: {
limit: 3,
backoff: 'exponential'
},
circuitBreaker: {
failureThreshold: 5,
resetTimeout: '1 minute'
},
caching: {
ttl: '5 minutes',
staleWhileRevalidate: true
}
});
See [Retry Policies](../15-retry-policies/) for detailed configuration.
export default defineWorkflow('user-data-sync', async (flow) => {
const client = flow.use(GraphqlClient);
// Type-safe query execution
const user = await flow.do('fetch user data', async () => {
return client.request<{ user: User }>(` query GetUser($id: ID!) {
user(id: $id) {
id
name
email
profile {
avatar
bio
}
}
}
`, { id: flow.params.userId });
});
// Process results
await flow.do('update local data', async () => {
await updateUserData(user.data.user);
});
});
export default defineWorkflow('resilient-integration', async (flow) => {
const client = flow.use(HttpClient);
await flow.do(
'external api call',
{ circuitBreaker: { failureThreshold: 5, resetTimeout: '1 minute', halfOpenMaxCalls: 1 } },
async () => {
try {
return await client.get('/api/data');
} catch (error) {
if (error.code === 'RATE_LIMITED') {
// Add retry delay hint
error.retryAfter = '60 seconds';
}
throw error;
}
},
);
});
export default defineWorkflow('reliable-integration', async (flow) => {
const primary = flow.use(HttpClient);
const backup = flow.use(BackupClient);
await flow.do(
'fetch data',
{
fallback: async (error) => {
if (error.code === 'SERVICE_UNAVAILABLE') {
// Use backup service
return await backup.get('/api/data');
}
throw error;
},
},
async () => {
return await primary.get('/api/data');
},
);
});

Connection Pooling

Configure appropriate pool sizes and timeouts for optimal resource usage.

Circuit Breakers

Protect your system from cascading failures with circuit breakers.

Rate Limiting

Implement rate limiting to respect API quotas and prevent overload.

const DbClient = () =>
createDbClient({
// Connection pool settings
poolSize: 10,
minConnections: 2,
maxIdleTime: '5 minutes',
// Circuit breaker
circuitBreaker: { failureThreshold: 5, resetTimeout: '1 minute' },
// Rate limiting
rateLimit: { maxRequests: 1000, perInterval: '1 minute' },
// Monitoring
monitoring: {
metrics: true,
slowQueryThreshold: '1 second',
healthCheck: { interval: '30 seconds' },
},
});

Lazy Loading

Use bindings to defer service initialization and load dependencies only when needed.

Resource Management

Configure appropriate pool sizes and timeouts for optimal resource usage.

Error Handling

Implement proper retry strategies and circuit breakers for failing services.

Monitoring

Enable metrics and health checks for all external services.