gqlgen README

The project's README from github.com/99designs/gqlgen. As one markdown file: README.md.

gqlgen

gqlgen Integration Coverage Status Go Report Card Go Reference Read the Docs

What is gqlgen?

gqlgen is a Go library for building GraphQL servers without any fuss.

Still not convinced enough to use gqlgen? Compare gqlgen with other Go graphql implementations

Quick start

  1. Initialise a new go module
mkdir example
cd example
go mod init example
  1. Add github.com/99designs/gqlgen to your project, as a tool dependency
go get -tool github.com/99designs/gqlgen
  1. Initialise gqlgen config and generate models
go tool gqlgen init
  1. Start the graphql server
go run server.go

More help to get started:

Reporting Issues

If you think you've found a bug, or something isn't behaving the way you think it should, please raise an issue on GitHub.

Contributing

We welcome contributions, Read our Contribution Guidelines to learn more about contributing to gqlgen

Frequently asked questions

How do I prevent fetching child objects that might not be used?

When you have nested or recursive schema like this:

type User {
	id: ID!
	name: String!
	friends: [User!]!
}

You need to tell gqlgen that it should only fetch friends if the user requested it. There are two ways to do this:

Using Custom Models

Write a custom model that omits the friends field:

type User struct {
  ID int
  Name string
}

And reference the model in gqlgen.yml:

# gqlgen.yml
models:
  User:
    model: github.com/you/pkg/model.User # go import path to the User struct above

Using Explicit Resolvers

If you want to keep using the generated model, mark the field as requiring a resolver explicitly in gqlgen.yml like this:

# gqlgen.yml
models:
  User:
    fields:
      friends:
        resolver: true # force a resolver to be generated

After doing either of the above and running generate we will need to provide a resolver for friends:

func (r *userResolver) Friends(ctx context.Context, obj *User) ([]*User, error) {
  // select * from user where friendid = obj.ID
  return friends,  nil
}

You can also use inline config with directives to achieve the same result

directive @goModel(
	model: String
	models: [String!]
) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION

directive @goField(
	forceResolver: Boolean
	name: String
	omittable: Boolean
	type: String
  autoBindGetterHaser: Boolean
  forceGenerate: Boolean
	batch: Boolean
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION

type User @goModel(model: "github.com/you/pkg/model.User") {
	id: ID! @goField(name: "todoId")
	friends: [User!]! @goField(forceResolver: true)
	# When omit_resolver_fields is enabled, this field will still be generated in the struct
	data: String! @goField(forceResolver: true, forceGenerate: true)
}

The field resolvers will be executed concurrently in separate goroutines. The degree of concurrency can be customized with the worker_limit configuration attribute.

Can I change the type of the ID from type String to Type Int?

Yes! You can by remapping it in config as seen below:

models:
  ID: # The GraphQL type ID is backed by
    model:
      - github.com/99designs/gqlgen/graphql.IntID # a go integer
      - github.com/99designs/gqlgen/graphql.ID # or a go string
      - github.com/99designs/gqlgen/graphql.UintID # or a go uint

This means gqlgen will be able to automatically bind to strings or ints for models you have written yourself, but the first model in this list is used as the default type and it will always be used when:

There isn't any way around this, gqlgen has no way to know what you want in a given context.

Why do my interfaces have getters? Can I disable these?

These were added in v0.17.14 to allow accessing common interface fields without casting to a concrete type. However, certain fields, like Relay-style Connections, cannot be implemented with simple getters.

If you'd prefer to not have getters generated in your interfaces, you can add the following in your gqlgen.yml:

# gqlgen.yml
omit_getters: true

Other Resources

gqlgen (99designs/gqlgen)

Searching Database…

10.8K ·GitHub
What is this?gqlgen is a Go library for building GraphQL servers without a lot of boilerplate, using a schema-first, code-generation approach.
Is this a framework I run, or a library I import?It is a Go library combined with a go generate based code generator: you write a GraphQL schema and small resolver functions, and gqlgen generates the boilerplate models, resolver interfaces, and execution code for you.
How do I get a GraphQL server running with it?Initialize a Go module, add github.com/99designs/gqlgen to your project as a tool dependency, run the gqlgen config/generate step to scaffold models, then start the generated server.
Do I need anything besides Go to use it?No other runtime dependencies are listed — just a Go module and gqlgen added as a tool dependency; everything else (models, resolvers) is generated from your own schema.
Is there a catch with typed IDs or nested resolvers?You can remap the ID type (e.g. String to Int) in config, but the first type listed is always used as the default for generated models and resolver arguments, with no way to override that per-context. Nested fields are fetched eagerly unless you write a custom model or mark the field for an explicit resolver.
Who is this for, and how does it compare to other Go GraphQL libraries?It targets Go developers who want type-safe, schema-first GraphQL servers instead of hand-writing boilerplate; the README links a dedicated comparison against other Go GraphQL implementations for anyone evaluating alternatives.
How does the schema-first approach actually work day to day?You write your API in the GraphQL Schema Definition Language, and gqlgen generates the matching Go models and resolver interfaces from it, so the schema stays the single source of truth.
Can I stop a query from always fetching child objects?Yes — either write a custom model that omits the field, or mark it in gqlgen.yml to require an explicit resolver, which gqlgen will call only when the field is actually requested.
Do field resolvers run sequentially or in parallel?They run concurrently in separate goroutines by default, and the degree of concurrency can be tuned with the workerlimit config attribute.
Why does gqlgen avoid map[string]interface{}?It prioritizes type safety, so generated code uses concrete Go structs and types for your schema instead of untyped maps, catching mismatches at compile time.
Creator
99designs
Size
27.2 MB
Stars
10.8K
Age
8y
Commits · 90 Days