A specialized GraphQL client for Evolve and its derived platforms that provides robust server-side and browser-side GraphQL operations.
-
Advanced Request Management
- Before request hooks with access to fetch options
- After response hooks with access to response data
- OpenTelemetry tracing integration
-
Flexible Document Handling
- GraphQL document parsing
- Support for both string and AST document nodes
- Operation name detection
-
Performance Optimizations
- Automatic Persisted Queries (APQ)
- Custom document ID generation
- Fallback to POST if APQ fails
-
Enhanced Fetch Integration
- Proper Content-Type headers
- Structured error handling
- Default fetch options
- Client-side GraphQL operations (Coming soon)
# Using npm
npm install @labdigital/evolve-graphql-client
# Using yarn
yarn add @labdigital/evolve-graphql-client
# Using pnpm
pnpm add @labdigital/evolve-graphql-client
# Using bun
bun add @labdigital/evolve-graphql-client
import { createServerClient } from 'evolve-graphql-client/server';
import { gql } from 'graphql-tag';
const client = createServerClient({
endpoint: 'https://your-graphql-api.com/graphql',
});
// Using a typed document node
const GET_USER = gql`
query GetUser($id: ID!) {
user(id: $id) {
id
name
email
}
}
`;
// Execute the query
const response = await client.fetch({
document: GET_USER,
variables: { id: '123' },
});
console.log(response.user);
import { createServerClient } from 'evolve-graphql-client/server';
const client = createServerClient({
endpoint: 'https://your-graphql-api.com/graphql',
// Hook executed before each request
onRequest: async (fetchOptions) => {
// Add authentication header
fetchOptions.headers.set('Authorization', `Bearer ${getToken()}`);
return fetchOptions;
},
// Hook executed after each response
onResponse: async (response) => {
// Log response headers for debugging
console.log('Response headers:', Object.fromEntries(response.headers.entries()));
},
// Control persisted operations
alwaysIncludeQuery: process.env.NODE_ENV === 'development',
disablePersistedOperations: false,
// Set default fetch options
defaultFetchOptions: {
credentials: 'include',
cache: 'no-store',
},
});
The server client intelligently handles GraphQL operations based on type:
-
For Mutations:
- Always sends a POST request with the full query
-
For Queries:
- First attempts an APQ GET request using document ID
- Falls back to POST if
PersistedQueryNotFound
error occurs
-
POST requests (non-APQ):
- Includes full query if
alwaysIncludeQuery
is true or nodocumentId
is available - Otherwise sends only documentId, variables, and extensions
- Includes full query if
-
GET requests (Persisted or APQ):
- Adds documentId and variables to search parameters
- Includes extensions if needed based on configuration
This library has full TypeScript support and integrates with the GraphQL typed document node specification.