Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added connectionEndpoint parameter to subscriptionOptions #113

Merged
Merged
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
1 change: 1 addition & 0 deletions packages/aws-lambda-graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ All options from Apollo Lambda Server and
- **waitForInitialization** (`optional`) - if connection is not initialised on GraphQL operation, wait for connection to be initialised or throw prohibited connection error. If `onConnect` is specified then we wait for initialisation otherwise we don't wait. (this is usefull if you're performing authentication in `onConnect`).
- **retryCount** (`number`, `optional`, `default 10`) - how many times should we try to check the connection state?
- **timeout** (`number`, `optional`, `default 50ms`) - how long should we wait (in milliseconds) until we try to check the connection state again?
- **connectionEndpoint** (`string`, `optional`) - if specified, the connection endpoint will be registered with this value as opposed to extracted from the event payload (as `${domainName}/${stage}`)

#### `createHttpHandler()`

Expand Down
12 changes: 10 additions & 2 deletions packages/aws-lambda-graphql/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ export interface ServerConfig<
*/
timeout?: number;
};

/**
* If specified, the connection endpoint will be registered with this value as opposed to extracted from the event payload
*
*/
connectionEndpoint?: string;
};
}

Expand Down Expand Up @@ -270,12 +276,14 @@ export class Server<
// based on routeKey, do actions
switch (event.requestContext.routeKey) {
case '$connect': {
const { onWebsocketConnect } = this.subscriptionOptions || {};
const { onWebsocketConnect, connectionEndpoint } =
this.subscriptionOptions || {};

// register connection
// if error is thrown during registration, connection is rejected
// we can implement some sort of authorization here
const endpoint = extractEndpointFromEvent(event);
const endpoint =
connectionEndpoint || extractEndpointFromEvent(event);

const connection = await this.connectionManager.registerConnection({
endpoint,
Expand Down
43 changes: 43 additions & 0 deletions packages/aws-lambda-graphql/src/__tests__/Server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,49 @@ describe('Server', () => {
);
});

it('registers connection with the endpoint value of connectionEndpoint option', async () => {
(connectionManager.registerConnection as jest.Mock).mockResolvedValueOnce(
{},
);
(connectionManager.setConnectionData as jest.Mock).mockResolvedValueOnce(
{},
);
const handlerWithConnectionEndpoint = new Server({
connectionManager,
eventProcessor: new MemoryEventProcessor(),
schema: createSchema(),
subscriptionManager,
subscriptions: {
connectionEndpoint: 'customdomain',
},
}).createWebSocketHandler();

await expect(
handlerWithConnectionEndpoint(
{
requestContext: {
connectionId: '1',
domainName: 'domain',
routeKey: '$connect',
stage: 'stage',
} as any,
} as any,
{} as any,
),
).resolves.toEqual(
expect.objectContaining({
body: '',
statusCode: 200,
}),
);

expect(connectionManager.registerConnection).toHaveBeenCalledTimes(1);
expect(connectionManager.registerConnection).toHaveBeenCalledWith({
endpoint: 'customdomain',
connectionId: '1',
});
});

it('refuses connection when onWebsocketConnect returns false', async () => {
(connectionManager.registerConnection as jest.Mock).mockResolvedValueOnce(
{},
Expand Down