Using Golang: Non-nullable field 'firstName' (type String!) was not present in result from Dgraph

Hi, still trying to get started with Dgraph and I’m having an issue querying a type, when I have populated it.

I’m using Golang and my code is the following:

Insert user code:

type UserGraphQL struct {
	Xid            string     `json:"xid,omitempty"`
	EmailAddress   string     `json:"emailAddress,omitempty"`
	HashedPassword string     `json:"hashedPassword,omitempty"`
	FirstName      string     `json:"firstName,omitempty"`
	LastName       string     `json:"lastName,omitempty"`
	CompanyName    string     `json:"companyName,omitempty"`
	Role           string     `json:"role,omitempty"`
	LastLogin      *time.Time `json:"lastLogin,omitempty"`
	DType          []string   `json:"dgraph.type,omitempty"`
}

func (f *Obj) insertUser(user UserMySQL) {
	ctx := context.Background()

	txn := f.Dg.NewTxn()
	defer txn.Discard(ctx)

	u := UserGraphQL{
		Xid:            user.Id,
		EmailAddress:   user.EmailAddress,
		HashedPassword: user.HashedPassword,
		FirstName:      user.FirstName,
		LastName:       user.LastName,
		CompanyName:    user.CompanyName,
		Role:           user.Role,
		LastLogin:      user.LastLogin,
		DType:          []string{"User"},
	}

	ub, err := json.Marshal(u)
	if err != nil {
		fmt.Println("problem marshalling json")
		fmt.Println(err)
	}

	mu := &api.Mutation{
		CommitNow: true,
		SetJson:   ub,
	}

	res, err := txn.Mutate(ctx, mu)

	if err != nil {
		fmt.Println("failed to mutate")
		fmt.Println(err)
	}

	fmt.Println(res)
	fmt.Println("------------")
}

My Schema:

type User {
  id: ID!
  xid: String! @search(by: [exact])
  emailAddress: String! @search(by: [exact])
  hashedPassword: String!
  firstName: String!
  lastName: String!
  companyName: String
  role: Role
  lastLogin: DateTime
  createdAt: DateTime!
  updatedAt: DateTime
  deletedAt: DateTime
}

The query:

query User {
  queryUser {
    id
    firstName
    lastName
  }
}

The result:

"errors": [
    {
      "message": "Non-nullable field 'firstName' (type String!) was not present in result from Dgraph.  GraphQL error propagation triggered.",
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ],
      "path": [
        "queryUser",
        0,
        "firstName"
      ]
    },

One side note, if I just query for the id. I get no errors, why would this be?

Have I missing something along the way? Would appreciate if someone can point me in the right direction.

Thanks

According to your schema, the firstName is required:

But the predicate value was not found for your node when you asked for it, when you don’t ask for it it does not try to get it hence no error.

You can fix this by either mutating your data and setting that field or change your schema to make that field not required.

Also, and this is why the predicate is missing!!.. it looks like you added the data with DQL, but are trying to query it with GraphQL. You added the dgraph.type correctly but did not map the fields to the correct predicates. You really need to read:

And the linked blog article from there by the same name.

You can fix this by either mutating your data and setting that field or change your schema to make that field not required.

All the fields that are required, have the data. Or so I thought…

I changed the schema of the table and I removed all the !.

Now I see

{
        "id": "0x2",
        "emailAddress": null,
        "firstName": null,
        "lastName": null
      },

The golang program is pulling data from an existing table and mutating Dgraph.

The table schema:

https://gist.githubusercontent.com/paulm17/abad52cc33ff778553e156d7d94733d4/raw/7c369bbe19916cf6a4aaafdd92c3c4a98d101609/gistfile1.txt

A snapshot of data.

| 01FKFZGGDX40KGE8MYX5TE9MG6 | gordondavid@gmail.com | JGFyZ29uMmlkJHY9MTkkbT02NTUzNix0PTIscD0xJDkyRE4yOElwNkpXY3Q3bzB3eDVXQ1EkTzB5Y3R2K0ZaQm90dE9ObzZtQktERm1hM3VPamF0SGp4bE5Kd202amRaTQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= | Gordon    | David     |             | USER | NULL      | 2021-11-02 09:26:01.407 | 2021-11-02 09:26:01.407 | NULL      |

So from my pov, there are 10 entries. All are populated with what’s in the golang struct.

Except this is not happening, instead something is wrong here. I don’t know what because I am following the guideline. I will take another look, otherwise I’ll file an issue.

You really need to read:

Not trying to be rude, as you have helped me out in discord and here. I appreciate that. But from a new users perspective. That link seems like 90% incomplete.

Even as a new user, this should be intuitive, not this difficult. I should be able to throw up a schema, populate it with data and then query it. I’m not the only one who has voiced how difficult working with Dgraph is. If I’m having such a hard time, then I can only point to the docs. This is what I am following. It’s how I am getting the results that I am getting.

I hope you can see where I am coming from. I really want to get started with Dgraph.

I’ll follow up, when I have combed through the golang client repo again.

So the linked blog post specifically:

The last thing to know before moving on is that a DQL schema that is generated from a GraphQL schema will name predicates in a type dotted notation. Here is a schema side by side:

GraphQL Schema

     type Person {
         id: ID
         name: String!
         cars: [Car] @hasInverse(field: "owner")
     }
     type Car {
         vin: String! @id
         owner: Person! @hasInverse(field: "cars")
     }

DQL Schema

    type Person {
        Person.name string
        Person.cars [Car]
    }
    type Car {
        Car.vin string
        Car.owner Person
    }
    Person.name string @index(exact) .
    Car.vin string @index(exact) .
    Person.cars [uid] .
    name string .

Also the docs here might help:

https://dgraph.io/docs/graphql/dgraph/#mapping-graphql-to-a-dgraph-schema

Finally resolved my issue.

I found this posting:

Different format for inserting data between graphql and dgraph client - #3 by anargu

Running a mutation with the struct changed like:

EmailAddress   string     `json:"User.emailAddress,omitempty"`

Gives me:

The documentation should really point this issue out. As someone who is going through the test files and doesn’t have a similar situation where I’m altering the schema (with dql) AND then doing a mutation.

Regardless, I can take this as a learning experience and move on. :grinning:

2 Likes