Пример #1
0
// getFResolver returns a GraphQL field resolver for REST layer field handler
func getFResolver(fieldName string, f schema.Field) graphql.FieldResolveFn {
	s, serialize := f.Validator.(schema.FieldSerializer)
	if !serialize && f.Handler == nil {
		return nil
	}
	return func(rp graphql.ResolveParams) (interface{}, error) {
		data, ok := rp.Source.(map[string]interface{})
		if !ok {
			return nil, nil
		}
		var err error
		val := data[fieldName]
		if f.Handler != nil {
			val, err = f.Handler(rp.Context, val, rp.Args)
		}
		if err == nil && serialize {
			val, err = s.Serialize(val)
		}
		return val, err
	}
}
Пример #2
0
func getSubFieldResolver(parentField string, r *resource.Resource, f schema.Field) graphql.FieldResolveFn {
	s, serialize := f.Validator.(schema.FieldSerializer)
	return func(p graphql.ResolveParams) (data interface{}, err error) {
		parent, ok := p.Source.(map[string]interface{})
		if !ok {
			return nil, nil
		}
		var item *resource.Item
		// Get sub field resource
		item, err = r.Get(p.Context, parent[parentField])
		if err != nil {
			return nil, err
		}
		data = item.Payload
		if f.Handler != nil {
			data, err = f.Handler(p.Context, data, p.Args)
		}
		if err == nil && serialize {
			data, err = s.Serialize(data)
		}
		return data, err
	}
}