graphql-go / graphql

An implementation of GraphQL for Go / Golang
MIT License
9.88k stars 840 forks source link

Custom DefaultResolveFn #507

Open asaf opened 5 years ago

asaf commented 5 years ago

Hey,

I have a custom type system and i have few constraints:

  1. I cannot implement Resolve on the object level as I cannot change the type system.
  2. I don't want to implement a resolver on every low level field just to resolve the custom object
  3. Default DefaultResolveFn fails to resolve the custom object.

Unless there's a better approach, I would like to provide a custom DefaultResolveFn.

Thanks.

deadletterq commented 5 years ago

Can you give a code example please?

asaf commented 5 years ago

lets say I have a custom object I want to return in resolvers:

type CustomObject struct {
    attrs map[string]interface{}
}

func (c *CustomObject) Get(key string) interface{} {
  return c.attrs[key]
}

DefaultResolveFn won't be able to resolve such an object because it's not a plain map nor a struct, i need a custom resolver to invoke obj.Get(key)

and since I can't add Resolve func on the CustomObject level (it's an external package) I need to provide a default custom resolver.

Thanks.

niondir commented 4 years ago

I have a another use case were I like to have my own DefaultResolveFn

I want to modify the DefaultResolveFn in a way that it dereferences pointer properties on my model.

Else I always end up with a lot of boilerplate in my ObjectConfig.Fields:

"deviceType": &Field{
            Type: deviceTypeType,
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                return *p.Source.(model.Hardware).DeviceType, nil
            },
        },

Or is that generally a bad Idea?