예제 #1
0
파일: types.go 프로젝트: rs/rest-layer
func getSubResourceResolver(r *resource.Resource) graphql.FieldResolveFn {
	return func(p graphql.ResolveParams) (interface{}, error) {
		parent, ok := p.Source.(map[string]interface{})
		if !ok {
			return nil, nil
		}
		lookup, page, perPage, err := listParamResolver(r, p, nil)
		if err != nil {
			return nil, err
		}
		// Limit the connection to parent's owned
		lookup.AddQuery(schema.Query{
			schema.Equal{
				Field: r.ParentField(),
				Value: parent["id"],
			},
		})
		list, err := r.Find(p.Context, lookup, page, perPage)
		if err != nil {
			return nil, err
		}
		result := make([]map[string]interface{}, len(list.Items))
		for i, item := range list.Items {
			result[i] = item.Payload
		}
		return result, nil
	}
}
예제 #2
0
파일: query.go 프로젝트: rs/rest-layer
func (t types) getListQuery(idx resource.Index, r *resource.Resource, params url.Values) *graphql.Field {
	return &graphql.Field{
		Description: fmt.Sprintf("Get a list of %s", r.Name()),
		Type:        graphql.NewList(t.getObjectType(idx, r)),
		Args:        listArgs,
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			lookup, page, perPage, err := listParamResolver(r, p, params)
			if err != nil {
				return nil, err
			}
			list, err := r.Find(p.Context, lookup, page, perPage)
			if err != nil {
				return nil, err
			}
			result := make([]map[string]interface{}, len(list.Items))
			for i, item := range list.Items {
				result[i] = item.Payload
			}
			return result, nil
		},
	}
}