Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions graphql/server-test/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* SuperTest GraphQL Adapter
*
* Provides a GraphQLAdapter implementation for use with graphql-server-test,
* allowing the generated ORM client to work with HTTP server testing
* while maintaining cookies across requests.
*/

import type supertest from 'supertest';
import type {
GraphQLAdapter,
GraphQLError,
QueryResult,
} from '@constructive-io/graphql-types';

/**
* GraphQL adapter that wraps a SuperTest agent for HTTP-based testing.
* This implements the GraphQLAdapter interface, allowing the SDK
* to work with the graphql-server-test infrastructure with full HTTP support.
*
* Key features:
* - Maintains cookies across requests (via SuperTest's cookie jar)
* - Supports custom headers (including CSRF tokens)
* - Full HTTP request/response cycle for testing middleware
*
* @example
* ```typescript
* import { getConnections, SuperTestAdapter } from 'graphql-server-test';
* import { createClient } from '@my-org/my-sdk';
*
* const { request, teardown } = await getConnections({ schemas: ['app_public'] });
*
* const sdk = createClient({ adapter: new SuperTestAdapter(request) });
*
* // Sign in - cookies are automatically stored
* const signInResult = await sdk.mutation.signIn({
* input: { email: 'test@example.com', password: 'password123' }
* }).execute();
*
* // Subsequent requests include cookies automatically
* const currentUser = await sdk.user.findFirst({
* select: { id: true, email: true }
* }).execute();
* ```
*/
export class SuperTestAdapter implements GraphQLAdapter {
private headers: Record<string, string> = {};

constructor(private agent: supertest.Agent) {}

async execute<T>(
document: string,
variables?: Record<string, unknown>
): Promise<QueryResult<T>> {
const response = await this.agent
.post('/graphql')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.set(this.headers)
.send({
query: document,
variables: variables ?? {},
});

const body = response.body as {
data?: T;
errors?: GraphQLError[];
};

if (body.errors && body.errors.length > 0) {
return {
ok: false,
data: null,
errors: body.errors,
};
}

return {
ok: true,
data: body.data as T,
errors: undefined,
};
}

/**
* Set headers to include in all subsequent requests.
* Useful for setting CSRF tokens or other custom headers.
*/
setHeaders(headers: Record<string, string>): void {
this.headers = { ...this.headers, ...headers };
}

/**
* Get the SuperTest agent for direct HTTP access.
* Useful for non-GraphQL requests (e.g., REST endpoints, file uploads).
*/
getAgent(): supertest.Agent {
return this.agent;
}
}
3 changes: 3 additions & 0 deletions graphql/server-test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export { createTestServer } from './server';
// Export SuperTest utilities
export { createSuperTestAgent } from './supertest';

// Export adapters
export { SuperTestAdapter } from './adapter';

// Export connection functions
export { getConnections } from './get-connections';

Expand Down
2 changes: 2 additions & 0 deletions graphql/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@pgpmjs/logger": "workspace:^",
"@pgpmjs/server-utils": "workspace:^",
"@pgpmjs/types": "workspace:^",
"cookie-parser": "^1.4.7",
"cors": "^2.8.5",
"express": "^5.2.1",
"gql-ast": "workspace:^",
Expand Down Expand Up @@ -78,6 +79,7 @@
},
"devDependencies": {
"@aws-sdk/client-s3": "^3.971.0",
"@types/cookie-parser": "^1.4.10",
"@types/cors": "^2.8.17",
"@types/express": "^5.0.6",
"@types/graphql-upload": "^8.0.12",
Expand Down
Loading