Using facets in the go lang dgraph client

I’m trying to figure out how to implement facets using the dgraph client for the go programming language. I have a full working example of a go project that uses dgraph, but no where in the documentation do they mention how to implement facets. Does anyone have experience in this or understand how you accomplish it? I’ve been using this page for reference: https://docs.dgraph.io/clients/

I’m assuming if absolutely required I could write the queries myself, but I was really hoping the client had some kind of built in mechanism for using them.

Facets are added via mutation. You should build your mutation accordingly. They are extra information from a predicate on a Node. So as you add an information to the predicate, at that point you should add the Facet as an extra.

If you know how to make a mutation, just add the facet to the predicate. In the mutation field.

e.g with Facet:

_: alice <car> "MA0123" (since = 2006-02-02T13: 01: 09, first = true).
node + predicate + obj + Facet

All customers are the same. Unless you’re doing this on Json. There is documentation on Facets with Json.

https://docs.dgraph.io/query-language/#facets-edge-attributes

This would be the method of writing the query myself. I think that part I have a fairly good grasp on. The problem I have is that I’m adding data to dgraph by populating structs in my go project. My assumption is that the structs are being converted into json before being sent to dgraph. So to clarify, is there no way through this method to add facet data or does it have to be done as a second mutation after the struct is sent to dgraph?

You could try a similar approach to the following one. I use it to put the language tags (en, es, pt…) in the predicate names, what was a problem that I found when using structs with the Golang Dgraph client.

One of my structs:

type Shop struct {
	Uid        string    `json:"uid,omitempty"`
	Document   string    `json:"shopDocument,omitempty"`
	Name       string    `json:"shopName,omitempty"`
	Image      string    `json:"shopImage,omitempty"`
	Street     *Street   `json:"shopStreet,omitempty"`
	Number     string    `json:"shopNumber,omitempty"`
	Complement string    `json:"shopComplement,omitempty"`
	Location   *Location `json:"shopLocation,omitempty"`
	Prices     []Price   `json:"shopPrices,omitempty"`
}

My mutation method:

func MutateSet(structData interface{}, language string, fieldsWithLanguage []string) (map[string]string, error) {
	mutation := api.Mutation{}
	json, err := json.Marshal(structData)
	if err != nil {
		return nil, err
	} else {
		json = setLanguage(json, language, fieldsWithLanguage)
		mutation.SetJson = json
		return mutate(mutation)
	}
}

The method I use to insert the language tag into the struct predicate:

func setLanguage(json []byte, language string, fieldsWithLanguage []string) []byte {
	jsonAsString := string(json)
	for _, fieldWithLanguage := range fieldsWithLanguage {
		jsonAsString = strings.Replace(jsonAsString, fieldWithLanguage, fieldWithLanguage+"@"+language, -1)
	}
	json = []byte(jsonAsString)
	return json
}

The process being executed:

var fieldsWithLanguage = []string{"shopName"}
var shop = Shop {/* some data */} 
_, err := MutateSet(shop, "en", fieldsWithLanguage)
return err

Did you have a look at this example https://godoc.org/github.com/dgraph-io/dgo#example-Txn-Mutate-Facets? Your are right that struct is converted to JSON before being sent to the server. We have some more documentation about setting facets using JSON at https://docs.dgraph.io/mutations/#facets.

That was exactly what I was looking for. Thank you for the info