Skip to content
Migrating from graphql-go

Migrating from graphql-go

github.com/gql-go/graphql continues github.com/graphql-go/graphql from its last release, v0.8.1. The package name and the API are the same: migrating is a change of module path, and code written for v0.8.1 keeps compiling and behaving the same way.

Before you start

  • Go version. Releases from v0.10.0 require Go 1.27 or newer. With the default GOTOOLCHAIN=auto, the go command downloads it when needed. If you cannot upgrade Go yet, use v0.9.x, which builds with Go 1.21 and receives security fixes for the period stated in the security policy.
  • Move every import together. github.com/graphql-go/graphql and github.com/gql-go/graphql are different modules, so their types are different types: a schema built with one cannot be passed to code that imports the other. Migrate all packages in your module at once, and use github.com/gql-go/handler in place of github.com/graphql-go/handler. A third-party package that imports graphql-go/graphql has to move too before it can work with your schema.

Steps

  1. Replace the import paths in your Go files. On Linux and macOS:

    grep -rl --null --include='*.go' 'github.com/graphql-go/' . |
      xargs -0 perl -pi -e 's#github\.com/graphql-go/(graphql|handler)\b#github.com/gql-go/$1#g'

    Or use your editor’s search and replace:

    github.com/graphql-go/graphql  ->  github.com/gql-go/graphql
    github.com/graphql-go/handler  ->  github.com/gql-go/handler
  2. Add the new modules and drop the old ones from go.mod. With Go 1.27 or newer:

    go get github.com/gql-go/graphql@latest
    go get github.com/gql-go/handler@latest   # if you use the handler
    go mod tidy

    go get raises the go directive of your module to 1.27 if it is lower.

    To stay on Go 1.21, use the v0.9 line of the library and the v0.3 line of the handler, which build with it:

    go get github.com/gql-go/graphql@v0.9
    go get github.com/gql-go/handler@v0.3     # if you use the handler
    go mod tidy

    If your module has a vendor directory, update it after go mod tidy with go mod vendor, or GOWORK=off go mod vendor when the module is part of a workspace. A workspace that vendors at its root uses go work vendor (Go 1.22 or newer).

  3. Build and run your tests:

    go build ./...
    go test ./...

What stays the same

The API of v0.8.1 is kept as it is: no exported symbol is removed or changed, and the inherited test suite runs unmodified in CI. This includes usage that is not formally documented but that production code depends on, such as DefaultResolveFn resolving struct fields by their json tag, ResolveParams and ResolveInfo built by hand, Schema.AppendType after NewSchema, and panics in resolvers turned into field errors.

New behaviour is opt-in: it is enabled through configuration, and the default stays as it was. Fixes change output only where it was broken before, such as a panic, a race or a wrong result.

Changes that may need action

Rarely, a fix or a security fix changes something that working code can observe. These are all of them:

  • gql-go/handler v0.4.1 and v0.3.2: mutations run only from a POST with an application/json or application/graphql body. graphql-go/handler ran them from GET requests and form bodies too, which allowed cross-site request forgery with cookie authentication (GHSA-4gfj-j6mf-x2mh). Clients that send mutations that way must send them as POST with JSON; see Serving over HTTP.
  • v0.14.0: an input object field whose name is not a valid GraphQL name, such as first-name, makes NewSchema and AppendType return an error. Before, the field was dropped without an error, so no client could use it; rename the field (#209).
  • v0.14.0: a Schema.AppendType that returns an error leaves the schema as it was. Code that ignored the error and used the rejected type no longer finds it; check the error (#204).
  • v0.14.0: Schema.AppendType called while another AppendType is in progress on the same schema, as from a fields thunk, returns ErrAppendTypeInProgress. It could succeed before for types without interfaces; append the type after the outer call returns (#210).

Following new releases

The guides describe the latest release, and mark each feature added after v0.9.0 with the version that introduced it. The release notes list every change, grouped by type, with a link to the pull request that explains it, and Announcements covers each minor release.

Older versions

The tags v0.4.18 to v0.8.1 in this repository are inherited from graphql-go/graphql and declare that module path. They are retracted: go get warns about them and go list -m -versions hides them. To use those versions, keep importing github.com/graphql-go/graphql.

Last updated on