Skip to content

Compgen is a gqlgen plugin designed to simplify the generation of ComplexityRoot for gqlgen.

License

Notifications You must be signed in to change notification settings

Warashi/compgen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Compgen

Compgen is a gqlgen plugin designed to simplify the generation of ComplexityRoot for gqlgen. The generated ComplexityRoot calculates complexity using the @complexity(x: number, mul: [String!]) directive, and a configurable default fallback.

Usage

  1. Create a main.go file to use this plugin with gqlgen. Here's an example:
package main

import (
    "fmt"
    "os"
    "github.com/99designs/gqlgen/api"
    "github.com/99designs/gqlgen/codegen/config"
    "github.com/Warashi/compgen"
)

func main() {
    cfg, err := config.LoadConfigFromDefaultLocations()
    if err != nil {
        fmt.Fprintln(os.Stderr, "failed to load config", err.Error())
        os.Exit(2)
    }
    if err := api.Generate(cfg, api.AddPlugin(compgen.New(compgen.WithDefaultComplexity(1)))); err != nil {
        fmt.Fprintln(os.Stderr, err.Error())
        os.Exit(3)
    }
}
  1. Use the generated ComplexityFunc as ComplexityRoot. For example:
cfg := gql.Config{
  Resolvers: resolvers,
  Complexity: gql.ComplexityFuncs,
}
srv := handler.NewDefaultServer(gql.NewExecutableSchema(cfg))
srv.Use(extension.FixedComplexityLimit(1000))

Calculation Example

Schema

type Query {
    foo(
      after: String,
      first: Int,
      before: String,
      last: Int,
    ): FooConnection! @complexity(x: 2, mul: ["first", "last"])
    fooIds(ids: [String!]): [Foo!] @complexity(x: 5, mul: ["ids"])
}

type PageInfo {
    hasNextPage: Boolean!
    hasPreviousPage: Boolean!
    startCursor: String
    endCursor: String
}

type FooConnection {
    edges: [FooEdge!]!
    pageInfo: PageInfo!
}

type FooEdge {
    cursor: String!
    node: Foo!
}

type Foo {
  bar: String! @complexity(x: 3)
}
  1. Execute the following query:

    query {
      foo(first: 5) {
        edges {
          node {
            bar
          }
        }
      }
    }

    The complexity will be calculated as (3 + default + default + 2) * 5.

  2. Execute the follow query:

    query {
      fooIds(ids: ["a","b","c"]) {
        edges {
          node {
            bar
          }
        }
      }
    }

    The complexity will be calculated as (3 + default + default + 5) * 3.

Additional tools

A linter relaycompmul is useful when using compgen with the relay cursor connections specification. This linter will output errors when fields that follow the relay cursor connections specification and do not have a @complexity directive or are missing mul arguments of @complexity.

About

Compgen is a gqlgen plugin designed to simplify the generation of ComplexityRoot for gqlgen.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages