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 } }
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 }, } }