GraphQL

Configuration

Config structs, YAML examples, and option helpers for GraphQL

YAML Configuration Example

config.yaml
extensions:
  graphql:
    endpoint: "/graphql"
    playgroundEndpoint: "/playground"
    enablePlayground: true
    enableIntrospection: true
    autoGenerateSchema: true
    schemaFile: ""

    # Query protection
    maxComplexity: 1000
    maxDepth: 15
    queryTimeout: "30s"

    # DataLoader
    enableDataLoader: true
    dataLoaderBatchSize: 100
    dataLoaderWait: "10ms"

    # Query caching
    enableQueryCache: true
    queryCacheTTL: "5m"
    maxCacheSize: 1000

    # CORS
    enableCORS: true
    allowedOrigins:
      - "*"

    # Uploads
    maxUploadSize: 10485760          # 10 MB

    # Observability
    enableMetrics: true
    enableTracing: true
    enableLogging: true
    logSlowQueries: true
    slowQueryThreshold: "1s"

The extension loads config from extensions.graphql first, falling back to graphql.

Programmatic Configuration

ext := graphql.NewExtension(
    graphql.WithEndpoint("/api/graphql"),
    graphql.WithPlayground(true),
    graphql.WithMaxComplexity(500),
    graphql.WithMaxDepth(10),
    graphql.WithDataLoader(true),
    graphql.WithQueryCache(true, 10*time.Minute),
)

Config Struct Reference

Config

FieldTypeDefaultDescription
Endpointstring"/graphql"GraphQL endpoint path
PlaygroundEndpointstring"/playground"Playground UI path
EnablePlaygroundbooltrueEnable GraphQL playground
EnableIntrospectionbooltrueEnable schema introspection
AutoGenerateSchemabooltrueAuto-generate schema from resolvers
SchemaFilestring""Path to schema file (overrides auto-generation)
MaxComplexityint1000Max query complexity score
MaxDepthint15Max query nesting depth
QueryTimeouttime.Duration30sPer-request timeout
EnableDataLoaderbooltrueEnable N+1 prevention
DataLoaderBatchSizeint100DataLoader batch size
DataLoaderWaittime.Duration10msDataLoader wait before batching
EnableQueryCachebooltrueEnable query plan caching
QueryCacheTTLtime.Duration5mQuery cache TTL
MaxCacheSizeint1000Max cached query plans
EnableCORSbooltrueEnable CORS
AllowedOrigins[]string["*"]CORS allowed origins
MaxUploadSizeint6410485760 (10 MB)Max file upload size
EnableMetricsbooltrueEnable metrics
EnableTracingbooltrueEnable tracing
LogSlowQueriesbooltrueLog slow queries
SlowQueryThresholdtime.Duration1sSlow query threshold

Option Helpers

FunctionDescription
WithEndpoint(endpoint)Set the GraphQL endpoint path
WithPlayground(enable)Toggle playground UI
WithIntrospection(enable)Toggle schema introspection
WithMaxComplexity(max)Set max query complexity
WithMaxDepth(max)Set max query depth
WithTimeout(timeout)Set query timeout
WithDataLoader(enable)Toggle DataLoader
WithQueryCache(enable, ttl)Toggle query caching with TTL
WithCORS(origins...)Set CORS allowed origins
WithMetrics(enable)Toggle metrics
WithTracing(enable)Toggle tracing
WithRequireConfig(require)Require config from ConfigManager
WithConfig(config)Provide a complete config struct

How is this guide?

On this page