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: log warning in dev when using incorrect protocol #92

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ import {
ErrorMessage,
CompleteMessage,
} from './message';
import { isObject, isAsyncIterable, areGraphQLErrors } from './utils';
import {
isObject,
isAsyncIterable,
areGraphQLErrors,
logDeveloperWarning,
} from './utils';
import { ID } from './types';

export type OperationResult =
Expand Down Expand Up @@ -398,6 +403,11 @@ export function makeServer<E = unknown>(options: ServerOptions<E>): Server<E> {
opened(socket, extra) {
if (socket.protocol !== GRAPHQL_TRANSPORT_WS_PROTOCOL) {
socket.close(1002, 'Protocol Error');
logDeveloperWarning(
`Invalid protocol: '${socket.protocol}'.
Only ${GRAPHQL_TRANSPORT_WS_PROTOCOL} is supported.
For more details see https://github.com/enisdenjo/graphql-ws/issues/83`,
);
Comment on lines +406 to +410
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to have this logDeveloperWarning. I try avoiding any logs and rely on exposing as much as possible for the users. Simply put the if != PROD console.warn right here.

Also, the indentation in backticks will be preserved. Consider inlining the message.

return async () => {
/* nothing was set up */
};
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ export function hasOwnStringProperty<
>(obj: O, prop: P): obj is O & Record<P, string> {
return baseHasOwnProperty.call(obj, prop) && typeof obj[prop] === 'string';
}

export function logDeveloperWarning(msg: string): void {
if (process.env.NODE_ENV !== 'production') {
console.warn(`[graphql-ws] ${msg}`);
}
}