Fully-featured GraphQL Server with focus on easy setup, performance & great developer experience
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core server creation and configuration functionality for setting up GraphQL endpoints with comprehensive options for plugins, CORS, context, endpoints, and performance tuning.
Main function for creating a GraphQL Yoga server instance with type-safe configuration options.
/**
* Creates a GraphQL Yoga server instance with comprehensive configuration options
* @param options - Server configuration options
* @returns Server adapter instance ready for deployment
*/
function createYoga<
TServerContext extends Record<string, any> = {},
TUserContext extends Record<string, any> = {},
>(
options: YogaServerOptions<TServerContext, TUserContext>
): YogaServerInstance<TServerContext, TUserContext>;
type YogaServerInstance<
TServerContext extends Record<string, any>,
TUserContext extends Record<string, any>,
> = ServerAdapter<TServerContext, YogaServer<TServerContext, TUserContext>>;Comprehensive configuration interface covering all aspects of server behavior.
interface YogaServerOptions<TServerContext, TUserContext> {
/** GraphQL schema or schema factory function */
schema?: YogaSchemaDefinition<TServerContext, TUserContext>;
/** Array of plugins to extend server functionality */
plugins?: Array<Plugin<TUserContext & TServerContext & YogaInitialContext, TServerContext, TUserContext>>;
/** CORS configuration or false to disable */
cors?: CORSOptions;
/** Context factory function for user context */
context?:
| ((initialContext: YogaInitialContext & TServerContext) => Promise<TUserContext> | TUserContext)
| Promise<TUserContext>
| TUserContext;
/** GraphQL endpoint path (default: '/graphql') */
graphqlEndpoint?: string;
/** Health check endpoint path (default: '/health') */
healthCheckEndpoint?: string;
/** Enable landing page for unhandled routes */
landingPage?: boolean | LandingPageRenderer;
/** GraphiQL configuration (default: true) */
graphiql?: GraphiQLOptionsOrFactory<TServerContext> | boolean;
/** Custom GraphiQL render function */
renderGraphiQL?: (options?: GraphiQLOptions) => PromiseOrValue<BodyInit>;
/** Request batching configuration (default: false) */
batching?: BatchingOptions;
/** Parser and validation caching configuration */
parserAndValidationCache?: boolean | ParserAndValidationCacheOptions;
/** Error masking configuration for production (default: true) */
maskedErrors?: boolean | Partial<YogaMaskedErrorOpts>;
/** Logging configuration (default: true) */
logging?: boolean | YogaLogger | LogLevel;
/** Custom fetch API implementation */
fetchAPI?: Partial<FetchAPI>;
/** Enable multipart request support (default: true) */
multipart?: boolean;
/** Server instance identifier */
id?: string;
/** Additional allowed parameter names in request body */
extraParamNames?: string[];
}Core server class that handles GraphQL request processing.
/**
* Core GraphQL Yoga server class
*/
class YogaServer<
TServerContext extends Record<string, any>,
TUserContext extends Record<string, any>,
> implements ServerAdapterBaseObject<TServerContext> {
/** Envelop instance for plugin execution */
readonly getEnveloped: GetEnvelopedFn<TUserContext & TServerContext & YogaInitialContext>;
/** Server logger instance */
logger: YogaLogger;
/** GraphQL endpoint path */
readonly graphqlEndpoint: string;
/** Fetch API implementation */
fetchAPI: FetchAPI;
/** Server version */
readonly version: string;
/** Request handler for processing HTTP requests */
readonly handle: ServerAdapterRequestHandler<TServerContext>;
}Configuration options for request batching functionality.
/**
* Configuration for GraphQL request batching
*/
type BatchingOptions =
| boolean
| {
/** Maximum number of operations in a batch (default: 10) */
limit?: number;
};Usage Examples:
import { createYoga, createSchema } from 'graphql-yoga';
// Basic server setup
const yoga = createYoga({
schema: createSchema({
typeDefs: `
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello: () => 'Hello from Yoga!'
}
}
})
});
// Advanced server configuration
const advancedYoga = createYoga({
schema: mySchema,
cors: {
origin: ['http://localhost:3000'],
credentials: true
},
context: ({ request }) => ({
user: getUserFromRequest(request)
}),
graphqlEndpoint: '/api/graphql',
graphiql: {
title: 'My GraphQL API'
},
batching: {
limit: 10
},
parserCache: true,
validationCache: true,
maskedErrors: {
isDev: process.env.NODE_ENV !== 'production'
},
logging: 'info'
});
// Server with custom plugins
const pluginYoga = createYoga({
schema: mySchema,
plugins: [
useAuth(),
useRateLimit(),
useCustomPlugin()
]
});Cross-Origin Resource Sharing configuration options.
/**
* CORS configuration options
*/
type CORSOptions =
| {
/** Allowed origins (strings or array) */
origin?: string[] | string;
/** Allowed HTTP methods */
methods?: string[];
/** Allowed request headers */
allowedHeaders?: string[];
/** Headers exposed to the client */
exposedHeaders?: string[];
/** Allow credentials in requests */
credentials?: boolean;
/** Preflight response max age in seconds */
maxAge?: number;
}
| false; // Disable CORSFunction signature for creating user context from initial context.
/**
* Context factory function type
*/
type ContextFactory<TServerContext, TUserContext> = (
initialContext: YogaInitialContext & TServerContext
) => Promise<TUserContext> | TUserContext;