stash/vendor/github.com/99designs/gqlgen
SmallCoccinelle 45f700d6ea
Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443)
* Upgrade gqlgen to v0.17.2

This enables builds on Go 1.18. github.com/vektah/gqlparser is upgraded
to the newest version too.

Getting this to work is a bit of a hazzle. I had to first remove
vendoring from the repository, perform the upgrade and then re-introduce
the vendor directory. I think gqlgens analysis went wrong for some
reason on the upgrade. It would seem a clean-room installation fixed it.

* Bump project to 1.18

* Update all packages, address gqlgenc breaking changes

* Let `go mod tidy` handle the go.mod file

* Upgrade linter to 1.45.2

* Introduce v1.45.2 of the linter

The linter now correctly warns on `strings.Title` because it isn't
unicode-aware. Fix this by using the suggested fix from x/text/cases
to produce unicode-aware strings.

The mapping isn't entirely 1-1 as this new approach has a larger iface:
it spans all of unicode rather than just ASCII. It coincides for ASCII
however, so things should be largely the same.

* Ready ourselves for errchkjson and contextcheck.

* Revert dockerfile golang version changes for now

Co-authored-by: Kermie <kermie@isinthe.house>
Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
2022-04-02 18:08:14 +11:00
..
api Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
codegen Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
complexity Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
graphql Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
init-templates Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
internal Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
plugin Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
.dockerignore
.editorconfig
.gitattributes Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
.gitignore Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
.golangci.yml Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
CHANGELOG.md Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
CONTRIBUTING.md Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
LICENSE
README.md Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
RELEASE-CHECKLIST.md Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
TESTING.md Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
main.go Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00
tools.go Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443) 2022-04-02 18:08:14 +11:00

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.

  • 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"' | gofmt > tools.go
    go mod tidy
    
  3. Initialise gqlgen config and generate models

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

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.

Other Resources