Mutation using dgo succeed but no displayed when I query it

My schema consists partly of this:

type User @secret(field: "pass") {
    	email: String! @id @search(by:[term])
    	name: String!  @search(by:[term])
    	description: String
        status: String! @search(by:[hash])
    	workPlace: String @search(by: [term])

When I ran a mutation using this code

type user struct {
	Email    string `json:"User.email"`
	Password string `json:"User.pass"`
	Name     string `json:"User.name"`
	Status   string `json:"User.status"`
}

func (c *Client) CreateUser(ctx context.Context, email, password, name string) error {
	user := user{
		Email:    email,
		Password: password,
		Name:     name,
		Status:   "Active",
	}

	d, err := json.Marshal(&user)
	if err != nil {
		return fmt.Errorf("could not marshal user %s: %v", string(d), err)
	}

	log.Printf("mutation: %s", string(d))

	txn := c.dgClient.NewTxn()
	_, err = txn.Mutate(context.Background(), &api.Mutation{SetJson: d, CommitNow: true})
	if err != nil {
		return fmt.Errorf("Could not mutate user %+v: %v", string(d), err)
	}

	return nil
}

No error is returned by a mutation but the database does not have any user.
I have tried to put like User.mail and it correctly tells me that the attribute mail does not exist on type user, but with this mutation, everything looks fine but somehow, data is not being saved.

Hi @jokicnikola07

Welcome to the forums! I hope the transition from the chatbot to the forums weren’t too difficult.

To help you solve the problem, can I also have the query you used to query for users?

Of course!
I have used it from slash admin panel

query MyQuery {
  queryUser(first: 10) {
    email
  }
}

And the result is:

{
  "data": {
    "queryUser": []
  },
  "extensions": {
    "tracing": {
      "version": 1,
      "startTime": "2020-12-01T21:05:19.116269924Z",
      "endTime": "2020-12-01T21:05:19.118114435Z",
      "duration": 1844525,
      "execution": {
        "resolvers": [
          {
            "path": [
              "queryUser"
            ],
            "parentType": "Query",
            "fieldName": "queryUser",
            "returnType": "[User]",
            "startOffset": 92404,
            "duration": 1739784,
            "dgraph": [
              {
                "label": "query",
                "startOffset": 137427,
                "duration": 1675154
              }
            ]
          }
        ]
      }
    }
  }
}

I noticed you log.Printf your mutations. Can I also have that.

2020/12/01 22:07:12 mutation: {“User.email":"jokicnikola07@gmail.com”,“User.pass”:“password”,“User.name”:“Nikola Jokic”,“User.status”:“Active”}

OK gimme some time to reproduce your error.

Thanks in advance

Your mutation is missing an important field: dgraph.type.

For example: I made this change -

type user struct {
	Email      string `json:"User.email"`
	Password   string `json:"User.pass"`
	Name       string `json:"User.name"`
	Status     string `json:"User.status"`
	DgraphType string `json:"dgraph.type"`
}

and

user := user{
		Email:      email,
		Password:   password,
		Name:       name,
		Status:     "Active",
		DgraphType: "User",
	}

such that the mutation actually reads like this:

2020/12/02 08:38:20 mutation: {"User.email":"XXX@yyy.con","User.pass":"penisisnotashortpassword","User.name":"Isocol","User.status":"Active","dgraph.type":"User"}

And when I query, I get this result:

{
  "data": {
    "queryUser": [
      {
        "email": "XXX@yyy.con",
        "name": "Isocol",
        "workPlace": null,
        "status": "Active",
        "description": null
      }
    ]
  },
  "extensions": {
    "touched_uids": 6,
    "tracing": {
      "version": 1,
      "startTime": "2020-12-01T21:40:48.926844065Z",
      "endTime": "2020-12-01T21:40:48.928158281Z",
      "duration": 1314239,
      "execution": {
        "resolvers": [
          {
            "path": [
              "queryUser"
            ],
            "parentType": "Query",
            "fieldName": "queryUser",
            "returnType": "[User]",
            "startOffset": 92806,
            "duration": 1193696,
            "dgraph": [
              {
                "label": "query",
                "startOffset": 144826,
                "duration": 1105511
              }
            ]
          }
        ]
      }
    },
    "queryCost": 1
  }
}

How I realized this/debugged this:

I connected Ratel to Slash, and then wrote the simplest DQL I can think of:

{
    q(func: type(User)) {
        expand(_all_)
    }
}

When I saw no result, I wanted to check if any data were added. I know I added data with User.name, so I wrote another DQL query:

{
    q(func: has(User.name)){
         uid
         expand(_all_)
    }
}

which showed that I had correctly made the mutations.

It was then I realized the mutation didn’t have dgraph.type.

Hope this helps.

1 Like

I will try it out. I have tried this morning to search by User.name but probably i did something wrong. I will notify you right away. Just give me a 2 minutes.

Thanks sir a lot. It works not and I understand now why. I have used Dgraph database but did not exactly understand how slash corresponds with dgraph database. I now understand a bit more. Thank you so much for debugging tip also!

Slash is just Dgraph that is hosted by us. The primary interface for Slash is GraphQL, which is easier for most people than DQL.