99designs / gqlgen

go generate based graphql server library
https://gqlgen.com
MIT License
9.98k stars 1.17k forks source link

Panic if type name "Subscription" #957

Open Yamashou opened 4 years ago

Yamashou commented 4 years ago

What happened?

A panic occurs if define type of Subscription

type Subscription implements Node {
    id: ID!
    userID: ID!
    name: String!
    deadline: Int!
    createdAt: Int!
    updatedAt: Int!
}

type Query {
    findSubscription(name:String!): Subscription
}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x130447d]

goroutine 1 [running]:
github.com/99designs/gqlgen/codegen/config.(*TypeReference).IsPtr(...)
        /Users/foo/go/pkg/mod/github.com/99designs/gqlgen@v0.10.2/codegen/config/binder.go:255
github.com/99designs/gqlgen/codegen.(*builder).buildField(0xc0002dd5d0, 0xc000627600, 0xc000167c00, 0x0, 0x1b189e0, 0x0)
        /Users/foo/go/pkg/mod/github.com/99designs/gqlgen@v0.10.2/codegen/field.go:70 +0x35d
github.com/99designs/gqlgen/codegen.(*builder).buildObject(0xc0002dd5d0, 0xc0001d6900, 0x1618581, 0x6, 0x0)
        /Users/foo/go/pkg/mod/github.com/99designs/gqlgen@v0.10.2/codegen/object.go:73 +0x4ec
github.com/99designs/gqlgen/codegen.BuildData(0xc0001b8000, 0x16fbc60, 0x1b17d80, 0x0)
        /Users/foo/go/pkg/mod/github.com/99designs/gqlgen@v0.10.2/codegen/data.go:89 +0x4a7
github.com/99designs/gqlgen/api.Generate(0xc0001b8000, 0x0, 0x0, 0x0, 0x0, 0xc0000a2006)
        /Users/foo/go/pkg/mod/github.com/99designs/gqlgen@v0.10.2/api/generate.go:41 +0x276
github.com/99designs/gqlgen/cmd.glob..func1(0xc0001b6000)
        /Users/foo/go/pkg/mod/github.com/99designs/gqlgen@v0.10.2/cmd/gen.go:39 +0xa2
github.com/urfave/cli.HandleAction(0x1553e60, 0x1659b50, 0xc0001b6000, 0xc0001b6000, 0x0)
        /Users/foo/go/pkg/mod/github.com/urfave/cli@v1.22.2/app.go:525 +0x127
github.com/urfave/cli.Command.Run(0x161d37a, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1649e4f, 0x29, 0x0, ...)
        /Users/foo/go/pkg/mod/github.com/urfave/cli@v1.22.2/command.go:174 +0x521
github.com/urfave/cli.(*App).Run(0xc000102540, 0xc00000e080, 0x2, 0x2, 0x0, 0x0)
        /Users/foo/go/pkg/mod/github.com/urfave/cli@v1.22.2/app.go:276 +0x729
github.com/99designs/gqlgen/cmd.Execute()
        /Users/foo/go/pkg/mod/github.com/99designs/gqlgen@v0.10.2/cmd/root.go:40 +0x224
main.main()
        /Users/foo/go/pkg/mod/github.com/99designs/gqlgen@v0.10.2/main.go:8 +0x20

What did you expect?

No Panic and generate code

Minimal graphql.schema and models to reproduce

type Subscription {
    id: ID!
    userID: ID!
    createdAt: Int!
    updatedAt: Int!
}

versions

keithmattix commented 4 years ago

Subscription is a reserved GraphQL type, and thus, the error you're facing is likely due to the code generator failing to process the duplication. I believe an error is desired, but errors like this could stand to have a more specific message.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

parkr commented 3 years ago

A more specific message would be very helpful here. I had no trouble defining a Subscription object type in my GraphQL schema which works just fine between Apollo and our GraphQL Ruby server. When generating client code with this library, however, I receive a somewhat confusing panic, Subscription was not found (when, in fact, it's a misuse of a reserved type):

2021/06/25 13:18:14 Subscription was not found
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x12e461f]

goroutine 1 [running]:
github.com/99designs/gqlgen/codegen/config.(*TypeReference).IsPtr(...)
        /Users/parkr/go/pkg/mod/github.com/99designs/gqlgen@v0.10.2/codegen/config/binder.go:255
github.com/99designs/gqlgen/codegen.(*builder).buildField(0xc002bdbbc8, 0xc001187c80, 0xc0051f8380, 0x0, 0x1, 0xc000d8e958)
        /Users/parkr/go/pkg/mod/github.com/99designs/gqlgen@v0.10.2/codegen/field.go:70 +0x37f
github.com/99designs/gqlgen/codegen.(*builder).buildObject(0xc002bdbbc8, 0xc0051f6540, 0xc001187c00, 0x0, 0x0)
        /Users/parkr/go/pkg/mod/github.com/99designs/gqlgen@v0.10.2/codegen/object.go:73 +0x47f
github.com/99designs/gqlgen/codegen.BuildData(0xc0001f4000, 0xc0001f4000, 0x0, 0x0)
        /Users/parkr/go/pkg/mod/github.com/99designs/gqlgen@v0.10.2/codegen/data.go:89 +0x4ac

It is interesting – given that Apollo and GraphQL Ruby seem to accept this Subscription type, perhaps it is not unreasonable to support Subscription implements Node as an object type?

parkr commented 3 years ago

@vektah It looks like Subscription is just a default root type name, which means it could easily be renamed and it's perfectly valid for us to use Subscription as a non-subscription object type. https://spec.graphql.org/draft/#sec-Root-Operation-Types.Default-Root-Operation-Type-Names

Screen Shot 2021-06-28 at 11 07 27 AM

From my reading, if the Schema's subscriptionType is null, then subscriptions are not supported.

Screen Shot 2021-06-28 at 11 09 07 AM Screen Shot 2021-06-28 at 11 08 57 AM Screen Shot 2021-06-28 at 11 08 07 AM
sneko commented 3 years ago

Hi,

In my case that's different, I'm from the v0.11.3 upgrading to latest... but now I no longer have a SubscriptionResolver, it's generated as a basic struct type. So my running server won't serve subscriptions 😢

I didn't see any mention of this, do you know how can I fix this? @lwc @vektah

Thanks

EDIT: Ok it seems the behavior has changed and now it needs to be explicitly into the schema: image