Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Returning a Query in the Payload of a Mutation got some errors #2763

Closed
sonatard opened this issue Aug 21, 2023 · 0 comments
Closed

Returning a Query in the Payload of a Mutation got some errors #2763

sonatard opened this issue Aug 21, 2023 · 0 comments

Comments

@sonatard
Copy link
Contributor

sonatard commented Aug 21, 2023

What happened?

schema {
    query: Query
    mutation: Mutation
}

type Query {
    id: Int!
}

type Mutation {
    userCreate: UserCreatePayload!
}
type UserCreatePayload {
    query: Query!
}
$ gqlgen
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x10 pc=0x1029845c0]

goroutine 1 [running]:
github.com/99designs/gqlgen/codegen/config.(*TypeReference).IsPtr(...)
        /Users/xxx/go/src/github.com/99designs/gqlgen/codegen/config/binder.go:225
github.com/99designs/gqlgen/codegen.(*builder).buildField(0x14008d4f7c0, 0x140379bb4d0, 0x140002e2480)
        /Users/xxx/go/src/github.com/99designs/gqlgen/codegen/field.go:78 +0x410
github.com/99designs/gqlgen/codegen.(*builder).buildObject(0x14008d4f7c0, 0x140002dad00)
        /Users/xxx/go/src/github.com/99designs/gqlgen/codegen/object.go:77 +0x56c
github.com/99designs/gqlgen/codegen.BuildData(0x14000252400, {0x140450d32c0, 0x2, 0x2})
        /Users/xxx/go/src/github.com/99designs/gqlgen/codegen/data.go:115 +0x2d0
github.com/99designs/gqlgen/api.Generate(0x14000252400, {0x0, 0x0, 0x102ec4f90?})
        /Users/xxx/go/src/github.com/99designs/gqlgen/api/generate.go:90 +0x704
main.glob..func2(0x14000254580?)
        /Users/xxx/go/src/github.com/99designs/gqlgen/main.go:187 +0xa8
github.com/urfave/cli/v2.(*Command).Run(0x14000254580, 0x140001dacc0, {0x140001a2080, 0x1, 0x1})
        /Users/xxx/go/pkg/mod/github.com/urfave/cli/v2@v2.25.5/command.go:274 +0x730
github.com/urfave/cli/v2.(*App).RunContext(0x1400025c3c0, {0x102be2bc0?, 0x102f0f140}, {0x140001a2080, 0x1, 0x1})
        /Users/xxx/go/pkg/mod/github.com/urfave/cli/v2@v2.25.5/app.go:332 +0x518
github.com/urfave/cli/v2.(*App).Run(...)
        /Users/xxx/go/pkg/mod/github.com/urfave/cli/v2@v2.25.5/app.go:309
main.main()
        /Users/xxx/go/src/github.com/99designs/gqlgen/main.go:227 +0x228

The error occurs because after executing the bindField function, in the case of a Query, f.TypeReference becomes nil.

gqlgen/codegen/field.go

Lines 69 to 75 in d6270e4

if err = b.bindField(obj, &f); err != nil {
f.IsResolver = true
if errors.Is(err, config.ErrTypeNotFound) {
return nil, err
}
log.Println(err.Error())
}

The reason f.TypeReference becomes nil is because Query cannot be found in b.cfg.Models.

if len(b.cfg.Models[schemaType.Name()].Model) == 0 {
return nil, fmt.Errorf("%s was not found", schemaType.Name())
}

The reason Query cannot be found in b.cfg.Models is likely because it is being excluded as shown below.
However, simply modifying this code to not exclude Query will result in many errors.

if schemaType == cfg.Schema.Query || schemaType == cfg.Schema.Mutation || schemaType == cfg.Schema.Subscription {
continue
}

What did you expect?

generate success.

versions

  • go run github.com/99designs/gqlgen version v0.17.36
  • go version go version go1.21.0 darwin/arm64
@sonatard sonatard changed the title Returning a Query in the Payload of a Mutation can result in an error. Returning a Query in the Payload of a Mutation got some errors Aug 21, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants