Warashi / compgen

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

License:MIT License


Languages

Language:Go 100.0%