-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolver Api with dynamic argument parsing #36
Comments
This is a very interesting approach, but I'm not sure if it's the best way to do this and if we should include it directly within the go-graphql library. I personally use a slightly different approach to handle this using a simple Map struct with functions for parsing each key into a specific type of value. type Map map[string]interface{}
// String returns the value at 'key' as a string
func (m Map) String(key string, defaultValue ...string) string {
if len(defaultValue) == 0 {
return m[key].(string)
}
value, ok := m[key].(string)
if !ok {
value = defaultValue[0]
}
return value
}
// Uint32 returns the value at 'key' as a uint32
func (m Map) Uint32(key string, defaultValue ...uint32) uint32 {
if len(defaultValue) == 0 {
return m[key].(uint32)
}
value, ok := m[key].(uint32)
if !ok {
value = defaultValue[0]
}
return value
}
resolvers["Example/resolver"] = func(params *graphql.ResolveParams) (interface{}, error) {
tx := helpers.LoadTransaction(params)
args := model.Map(params.Args)
println(args.String("key1")) // Fetch the value of argument 'key1' as a string, will crash on nil assertion if the argument is not passed
println(args.Uint32("key2", uint32(42))) // Fetch value of argument 'key2' as an uint32. If it does not exist use the value 42
return nil, nil
}
The biggest issue I've had with my approach is that it will crash while trying to access a nil value if you don't pass an input argument and haven't mentioned a default value. The main reason I don't want to include it in the library is that it needs to include a way to parse all the basic types you could read out from the input. One disadvantage with your approach would be that you can't specify default values easily without verbose if checks for each field. |
@atrniv Thnxs for the response, perhaps for now it is better to leave as an external package. Am sure the basic resolver argument-parsing mechanism I'm using in the I'm happy with any thoughts and/or contributions anyone provides. : ) |
Was wondering if there is any appetite for enhancing the resolver api by enabling dynamic argument parsing for faster/safer coding.
Below is a rough (but working) example of how it could work...aided by some minimal external packages I quickly pulled together...
Any comments? Takers?
resolvers.go
The text was updated successfully, but these errors were encountered: