• Stars
    star
    9,814
  • Rank 3,539 (Top 0.08 %)
  • Language
    Go
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

go generate based graphql server library

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.

  • gqlgen is based on a Schema first approach โ€” You get to Define your API using the GraphQL Schema Definition Language.
  • gqlgen prioritizes Type safety โ€” You should never see map[string]interface{} here.
  • gqlgen enables Codegen โ€” We generate the boring bits, so you can focus on building your app quickly.

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
    
  2. Add github.com/99designs/gqlgen to your project's tools.go

    printf '// +build tools\npackage tools\nimport (_ "github.com/99designs/gqlgen"\n _ "github.com/99designs/gqlgen/graphql/introspection")' | gofmt > tools.go
    
    go mod tidy
    
  3. Initialise gqlgen config and generate models

    go run github.com/99designs/gqlgen init
    
    go mod tidy
    
  4. 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) 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)
}

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:

  • Generating models based on schema
  • As arguments in resolvers

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

More Repositories

1

aws-vault

A vault for securely storing and accessing AWS credentials in development environments
Go
8,336
star
2

colorific

Automatic color palette detection
Python
699
star
3

keyring

Go library providing a uniform interface across a range of secure credential stores
Go
554
star
4

iamy

A cli tool for importing and exporting AWS IAM configuration to YAML files
Go
239
star
5

phumbor

A minimal PHP helper for Thumbor
PHP
120
star
6

gqlgen-contrib

Add-ons for gqlgen
Go
85
star
7

cmdstalk

beanstalkd broker; run jobs as unix commands.
Go
73
star
8

aws-ecr-gc

Garbage collector for Amazon ECR docker registry
Go
48
star
9

http-signatures-php

Sign and verify PSR-7 HTTP messages in PHP.
PHP
44
star
10

goodies

A grab-bag of useful Go helper libraries
Go
32
star
11

ergo

A lightweight php5 library for request/response routing, controllers and http interaction.
PHP
29
star
12

httpsignatures-go

A golang http-signatures library
Go
28
star
13

clouddetect

Go package for resolving IP addresses back to published cloud provider public ranges
Go
27
star
14

gqlgen-workshop

Source for a workshop on generating GraphQL servers with gqlgen
Go
24
star
15

thumbor_botornado

Thumbor S3 backend based on botornado async client.
Python
21
star
16

http-signatures-ruby

Sign and verify HTTP messages in Ruby.
Ruby
21
star
17

basicauth-go

HTTP basic auth middleware for golang
Go
19
star
18

telemetry

Golang application monitoring middleware
Go
18
star
19

http-signatures-guzzlehttp

Guzzle 6 support for 99designs http-signatures library
PHP
12
star
20

simpletest

Unit testing, mock objects and web testing framework for PHP built around test cases.
PHP
11
star
21

ecs-upload-task

Upload an ecs task definition and optionally deploy it
Go
11
star
22

httpforwarder

Asynchronously forward HTTP requests
Go
10
star
23

protobuf-playground

Playground and generated docs for Protobuf files
TypeScript
8
star
24

php-desk

PHP client for desk.com API
PHP
7
star
25

dddk

Datadog Development Kit
TypeScript
7
star
26

smartling

Smartling is a library and cli tool for managing Smartling projects
Go
6
star
27

capistrano-rightscale

A Capistrano extension that maps Rightscale tags to Roles
Ruby
4
star
28

rightsignature-php

PHP wrapper around the RightSignature API
PHP
4
star
29

relax

A library for consuming simple REST services in PHP5.
PHP
3
star
30

silex-assets

An asset pipeline for Silex orchestrated by make
PHP
3
star
31

sera

A library for queuing and executing php5 tasks
PHP
3
star
32

http-signatures-guzzle

Guzzle 3 support for 99designs http-signatures library
PHP
2
star
33

cliph

A PHP5.3 library for testable CLI applications
PHP
2
star
34

scopecheck

Go linter checking access to a captured closure var when a closer one is available
Go
2
star
35

twirfony

Symfony support for Twirp
PHP
2
star
36

ergo-http

Http utils extracted from https://github.com/99designs/ergo
PHP
1
star
37

dddk-init

Scaffolding for dddk
TypeScript
1
star
38

sipht

A PHP client for the Sift Science REST API
PHP
1
star