No input field null validation on non nullable fields

Report a Dgraph Bug

What version of Dgraph are you using?

Dgraph Version
$ dgraph version
 
Using dgraph cloud (v20.11.2-rc1-23-gaf5030a5)

Have you tried reproducing the issue with the latest release?

Nope

What is the hardware spec (RAM, OS)?

Dgraph Cloud

Steps to reproduce the issue (command/config used to run Dgraph).

I have a simple schema

type Test  {
	id: ID! 
	name: String! 
}

Dgraph creates the addTest mutation but the following query is valid even though it shoundn’t be as name is non nullable

mutation MyMutation {
  addTest(input: {}) {
    numUids
  }
}

Expected behaviour and actual result.

Dgraph should throw error

This is a GraphQL issue, moving it to GraphQL topic. :rocket:

input: {} is not null. It’s an empty object.

Correct, the issue is that the name field should he required or else throw a GraphQL error

mutation MyMutation {
  addTest(input: {name:null}) {
    numUids
  }
}

Fails as expected. An empty object is not prefilled with nulls per JavaScript… it would appear that our definition follows Go’s definition of an empty object: things filled with default values.

Should we change this?

Debate (10 marks)

@chewxy if I write a query like

mutation MyMutation {
  addTest(input: {}) {
    numUids
    test {
      name
    }
  }
}

I get a graphql error from dgraph saying name is null, which means name is passed as null by default.

2 Likes

Aha, then it is a bug. @graphql FYI.

Accepting as a bug

1 Like

I just experienced this:

Ran the following mutation:

mutation{
  addTranscript(input: {name: "test"}){
    numUids
  }
}

Transcript is created.

However, the schema specifies that primaryLanguage is non-nullable, so my expectation is that the mutation should fail because there is no value specified for primaryLanguage:

image

If I removed primaryLanguage from requested return result, then I get the following result:

{
  "data": {
    "queryTranscript": [
      {
        "id": "0x4e21",
        "name": "test",
        "isPublished": null,
        "shareDescription": null,
        "shareTitle": null,
        "slug": null
      },
      {
        "id": "0x4e22",
        "name": "test",
        "isPublished": null,
        "shareDescription": null,
        "shareTitle": null,
        "slug": null
      }
    ]
  },
  "extensions": {
    "touched_uids": 12,
    "tracing": {
      "version": 1,
      "startTime": "2021-05-25T08:20:37.1343371Z",
      "endTime": "2021-05-25T08:20:37.1421826Z",
      "duration": 7845400,
      "execution": {
        "resolvers": [
          {
            "path": [
              "queryTranscript"
            ],
            "parentType": "Query",
            "fieldName": "queryTranscript",
            "returnType": "[Transcript]",
            "startOffset": 317600,
            "duration": 7473600,
            "dgraph": [
              {
                "label": "query",
                "startOffset": 425600,
                "duration": 7355200
              }
            ]
          }
        ]
      }
    }
  }
}

However, if I include it, I get the following:

{
  "errors": [
    {
      "message": "Non-nullable field 'primaryLanguage' (type String!) was not present in result from Dgraph.  GraphQL error propagation triggered.",
      "locations": [
        {
          "line": 12,
          "column": 5
        }
      ],
      "path": [
        "queryTranscript",
        0,
        "primaryLanguage"
      ]
    },
    {
      "message": "Non-nullable field 'primaryLanguage' (type String!) was not present in result from Dgraph.  GraphQL error propagation triggered.",
      "locations": [
        {
          "line": 12,
          "column": 5
        }
      ],
      "path": [
        "queryTranscript",
        1,
        "primaryLanguage"
      ]
    }
  ],
  "data": {
    "queryTranscript": [
      null,
      null
    ]
  },
  "extensions": {
    "touched_uids": 14,
    "tracing": {
      "version": 1,
      "startTime": "2021-05-25T08:22:22.1628428Z",
      "endTime": "2021-05-25T08:22:22.1710665Z",
      "duration": 8223000,
      "execution": {
        "resolvers": [
          {
            "path": [
              "queryTranscript"
            ],
            "parentType": "Query",
            "fieldName": "queryTranscript",
            "returnType": "[Transcript]",
            "startOffset": 278900,
            "duration": 7893000,
            "dgraph": [
              {
                "label": "query",
                "startOffset": 485300,
                "duration": 7677700
              }
            ]
          }
        ]
      }
    }
  }
}
Dgraph version   : v21.03.0
Dgraph codename  : rocket
Dgraph SHA-256   : b4e4c77011e2938e9da197395dbce91d0c6ebb83d383b190f5b70201836a773f
Commit SHA-1     : a77bbe8ae
Commit timestamp : 2021-04-07 21:36:38 +0530
Branch           : HEAD
Go version       : go1.16.2
jemalloc enabled : true

Using Dgraph via Docker locally.