Skip to content

createServer

Creates and configures a Socket.IO GraphQL server with live query support.

import { createServer } from '@requence/socketql/server'
function createServer<Context>(options: ServerOptions<Context>): {
server: IoServer
namespace: Namespace
liveQueryStore: ExtendedLiveQueryStore
attach: (baseServer: ServerInstance) => void
addSchema: (opts: { typeDefs: TypeSource; resolvers: IResolvers }) => void
generateIntrospection: () => Promise<IntrospectionQuery>
}
import { createServer } from '@requence/socketql/server/bun'
function createServer<Context>(options: ServerOptions<Context>): {
server: IoServer
namespace: Namespace
liveQueryStore: ExtendedLiveQueryStore
handler: () => { fetch, websocket, idleTimeout }
addSchema: (opts: { typeDefs: TypeSource; resolvers: IResolvers }) => void
generateIntrospection: () => Promise<IntrospectionQuery>
}
OptionTypeDefaultDescription
pathstring'/ws/'Socket.IO server path
transportsstring[]['websocket']Allowed transports
graphqlNamespacestring'graphql'Socket.IO namespace for GraphQL
extendContext(ctx) => Context() => ({})Extend GraphQL context per connection
onConnect(socket) => voidCalled when a client connects
onDisconnect(socket, reason) => voidCalled when a client disconnects
liveQueryStoreLiveQueryStoreOptions{}Redis URL and channel for distributed live queries
formatError(error) => GraphQLErrorTransform GraphQL errors before sending
maxUploadSizenumberMax upload size in bytes
transformSchemaSchemaTransformeridentityTransform the schema after creation
pingIntervalnumber25000Socket.IO ping interval (ms)
pingTimeoutnumber20000Socket.IO ping timeout (ms)
redisUrlstringRedis URL for Socket.IO adapter
wrapExecute(execute, context) => resultpassthroughWrap GraphQL execution (e.g. for tracing)

Shared across both entry points:

PropertyTypeDescription
serverIoServerThe raw Socket.IO server instance
namespaceNamespaceThe GraphQL namespace
liveQueryStoreExtendedLiveQueryStoreLive query store for invalidation
addSchema(opts) => voidRegister type definitions and resolvers
generateIntrospection() => Promise<...>Generate introspection result

Node.js only:

PropertyTypeDescription
attach(server) => voidAttach to a Node.js HTTP/HTTPS/HTTP2 server

Bun only:

PropertyTypeDescription
handler() => { fetch, websocket, idleTimeout }Returns Bun-ready config to spread into Bun.serve() or export default
handleRequest(req, server) => Promise<Response>Handle a single Socket.IO request directly
withServer(fallback) => (req, server) => ResponseIntercepts SocketQL requests, delegates the rest to the fallback

Every resolver receives a context with these properties:

PropertyTypeDescription
namespaceNamespaceThe Socket.IO namespace
socketSocketThe current client’s socket
queriedFieldsQueriedFieldsParsed field selection helpers
unauthorized(msg) => neverThrow an unauthorized GraphQL error
loader(load, name?) => DataLoaderGet or create a batched DataLoader
liveQueryStoreExtendedLiveQueryStoreLive query invalidation