Add Mutations - Graphql

Add Mutations allows you to add new objects of a particular type.

We use the following schema to demonstrate some examples.

Schema:

type Author {
	id: ID!
	name: String! @search(by: [hash])
	dob: DateTime
	posts: [Post]
}
type Post {
	postID: ID!
	title: String! @search(by: [term, fulltext])
	text: String @search(by: [fulltext, term])
	datePublished: DateTime
}

Dgraph automatically generates input and return type in the schema for the add mutation.

addPost(input: [AddPostInput!]!): AddPostPayload
input AddPostInput {
	title: String!
	text: String
	datePublished: DateTime
}
type AddPostPayload {
	post(filter: PostFilter, order: PostOrder, first: Int, offset: Int): [Post]
	numUids: Int
}

Example: Add mutation on single type with embedded value

mutation {
  addAuthor(input: [{ name: "A.N. Author", posts: []}]) {
    author {
      id
      name
    }
  }
}

Example: Add mutation on single type using variables

mutation addAuthor($author: [AddAuthorInput!]!) {
  addAuthor(input: $author) {
    author {
      id
      name
    }
  }
}

Variables:

{ "auth":
  { "name": "A.N. Author",
    "dob": "2000-01-01",
    "posts": []
  }
}

Examples

You can refer to the following link for more examples.


This is a companion discussion topic for the original entry at https://dgraph.io/docs/graphql/mutations/add/