compgen

package module
v0.0.0-...-b94d1a3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 10 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMulFieldIsWrongType = errors.New("mul field is neither Int nor List")
	ErrMulFieldIsNotExist  = errors.New("mul field is not exist")
)

Functions

func IsInt

func IsInt(typ *ast.Type) bool

func IsList

func IsList(typ *ast.Type) bool

Types

type Inputs

type Inputs struct {
	Plugin     *Plugin
	GQLGenData *codegen.Data
}

type Option

type Option func(*Plugin)

func WithDefaultComplexity

func WithDefaultComplexity(x int) Option

WithDefaultComplexity sets default complexity. default is `1`.

func WithFilename

func WithFilename(fn string) Option

WithFilename sets generated filename. default is `compgen.go`.

type Plugin

type Plugin struct {
	Filename          string
	DefaultComplexity int
}

func New

func New(opts ...Option) *Plugin

func (*Plugin) GenerateCode

func (p *Plugin) GenerateCode(cfg *codegen.Data) error

GenerateCode implements plugin.CodeGenerator

func (*Plugin) MutateConfig

func (*Plugin) MutateConfig(cfg *config.Config) error

MutateConfig implements plugin.ConfigMutator

func (*Plugin) Name

func (*Plugin) Name() string

Name implements plugin.Plugin

Directories

Path Synopsis
complexitytest
cmd
linter
relaycompmul Module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL