# How to force required fields?

**URL:** <https://discuss.dgraph.io/t/how-to-force-required-fields/13950>\
**Category:** Dgraph\
**Tags:** kind:question\
**Created:** [April 30, 2021, 12:24pm UTC](https://discuss.dgraph.io/t/how-to-force-required-fields/13950 "2021-04-30T12:24:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrwunderbar666](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mrwunderbar666/32/7741_2.png) [@mrwunderbar666](https://discuss.dgraph.io/u/mrwunderbar666)\
**Post date:** [April 30, 2021, 12:24pm UTC](https://discuss.dgraph.io/t/how-to-force-required-fields/13950/1 "2021-04-30T12:24:30Z")

</div>

## What I want to do

I am experimenting with simple features of dgraph. I declared a schema with required fields. However, when I add a mutation via `play.dgraph.io/?local`, it ignores the required constraint.

## What I did

Consider the following schema:

```auto
type BlogPost {
  id: ID!
  title: String!
  content: String!
  hasAuthor: Author!
}

type Author {
  id: ID!
  name: String!
  hasBlog: [BlogPost]
}

```

According to the documentation, adding the exclamation point after the field type such as `String!` makes it a required field. Logically, every `Author` has to have a `name` and `id`.

I then submitted the schema via `curl`

I can now run mutations in my console such as the following:

```auto
{
  set {
    _:user <dgraph.type> "Author" .
    _:user <Author.name> "User1" .
  }
}

```

Which returns a success message:

```auto
{
  "data": {
    "code": "Success",
    "message": "Done",
    "queries": null,
    "uids": {
      "user": "0x4e91"
    }
...

```

But I can also run this mutation:

```auto
{
  set {
    _:user <dgraph.type> "Author" .
    _:user <name> "User2" .
  }
}

```

Which also yields a positive result:

```auto
{
  "data": {
    "code": "Success",
    "message": "Done",
    "queries": null,
    "uids": {
      "user": "0x4e92"
    }
...

```

Isn’t the whole point of the schema to enforce data consistency? If I now run a query such as:

```auto
{
  node(func: type(Author)) {
    uid
    expand(_all_)
  }
}

```

it returns both `Author`s, but the second one has no `name` field:

```auto
{
  "data": {
    "node": [
      {
        "uid": "0x4e91",
        "Author.name": "User1"
      },
      {
        "uid": "0x4e92"
      }
    ]
  }
....

```

## Dgraph metadata

> **dgraph version**
>
> 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

> **schema**
>
> \<Author.hasBlog\>: [uid] .  
> \<Author.name\>: string .  
> \<BlogPost.content\>: string .  
> \<BlogPost.hasAuthor\>: uid .  
> \<BlogPost.title\>: string .  
> \<dgraph.drop.op\>: string .  
> \<dgraph.graphql.p\_query\>: string @index(sha256) .  
> \<dgraph.graphql.schema\>: string .  
> \<dgraph.graphql.xid\>: string @index(exact) @upsert .  
> : default .  
> type {  
> Author.name  
> Author.hasBlog  
> }  
> type {  
> BlogPost.title  
> BlogPost.content  
> BlogPost.hasAuthor  
> }  
> type \<dgraph.graphql\> {  
> dgraph.graphql.schema  
> dgraph.graphql.xid  
> }  
> type \<dgraph.graphql.persisted\_query\> {  
> dgraph.graphql.p\_query  
> }

---

<div class="post-metadata">

**Author:** ![kaustubh](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/kaustubh/32/8364_2.png) [@kaustubh](https://discuss.dgraph.io/u/kaustubh)\
**Post date:** [April 30, 2021, 12:38pm UTC](https://discuss.dgraph.io/t/how-to-force-required-fields/13950/2 "2021-04-30T12:38:21Z")

</div>

GraphQL is more of an abstraction layer for interacting with your DB. Since you are interacting directly with DQL, those checks of GraphQL won’t apply (since you’re bypassing the GraphQL layer)

---

<div class="post-metadata">

**Author:** ![mrwunderbar666](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mrwunderbar666/32/7741_2.png) [@mrwunderbar666](https://discuss.dgraph.io/u/mrwunderbar666)\
**Post date:** [April 30, 2021, 12:45pm UTC](https://discuss.dgraph.io/t/how-to-force-required-fields/13950/3 "2021-04-30T12:45:18Z")

</div>

thanks for the swift reply. This starts making sense now.  
Does that mean that DQL does not support non-nullable fields?

---

<div class="post-metadata">

**Author:** ![kaustubh](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/kaustubh/32/8364_2.png) [@kaustubh](https://discuss.dgraph.io/u/kaustubh)\
**Post date:** [April 30, 2021, 12:59pm UTC](https://discuss.dgraph.io/t/how-to-force-required-fields/13950/4 "2021-04-30T12:59:42Z")

</div>

Yes that’s correct. DQL has weaker restrictions as compared to GraphQL

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [April 30, 2021, 6:06pm UTC](https://discuss.dgraph.io/t/how-to-force-required-fields/13950/5 "2021-04-30T18:06:40Z")

</div>

PS. Just noticed it was answered few moments after writing this hehehe, but here it goes.

> [@mrwunderbar666](#):
>
> [Dgraph Ratel Dashboard](http://play.dgraph.io/?local)

This has a very old version of Ratel. Please use latest or Dev.

> [@mrwunderbar666](#):
>
> But I can also run this mutation:

You can, but you shouldn’t. As you are modeling your Graph using the GraphQL Schema, you should stick to the GraphQL model. Which means you have to always put the Type Prefix in all predicates.

> [@mrwunderbar666](#):
>
> Which also yields a positive result:
> 
> ```auto
> 
> ```

It will execute cuz is its job 😛

> [@mrwunderbar666](#):
>
> Isn’t the whole point of the schema to enforce data consistency?

Yes, in GraphQL context, not DQL.

> [@mrwunderbar666](#):
>
> it returns both `Author`s, but the second one has no `name` field:

Because there’s no “name” in the “Author” type, only “Author.name”. BTW, there is any reason that you are mixing GraphQL and DQL? Looks like you are a bit lost.

> [@mrwunderbar666](#):
>
> Does that mean that DQL does not support non-nullable fields?

You can kind force it using strict

```auto
dgraph alpha -h | grep strict   
      --mutations string Set mutation mode to allow, disallow, or strict. (default "allow")

```

But it is not the same as GraphQL.
