graph-gophers / graphql-go

GraphQL server with a focus on ease of use
BSD 2-Clause "Simplified" License
4.64k stars 491 forks source link

Allow struct fields which are functions #598

Closed pavelnikolov closed 1 year ago

pavelnikolov commented 1 year ago

Currently the library allows field names to be overridden using the graphql reflect tag. This in turn allows for case-sensitive resolver names and resolvers with significant (even leading) _ character in the name. However, it is not possible to achieve the same with methods, especially for leading underscore. To fix this, the library needs to allow func fields, which behave exactly like method resolvers without the receiver. This would allow, for example:

type Query {
     _hello: String!
}

to be resolved by:

type resolver struct {
     Greet func(args struct{Name string}) string `graphql:"_hello"`
}

Then the actual resolver instance might look like this:

r := &resolver{
    Greet: func(args struct{Name string}) string {
        return "Hello, " + args.Name + "!"
    },
}