Mutations​

EdgeDB provides GraphQL mutations to perform delete, insert and update operations.

Delete​

The “delete” mutation is very similar in structure to a query. Basically, it works the same way as a query, using the filter, order, and various pagination parameters to define a set of objects to be deleted. These objects are also returned as the result of the delete mutation. Each object type has a corresponding delete_ mutation:

GraphQL

EdgeQL equivalent

Mutations - 图1Copy
  1. mutation delete_all_books {
  2. delete_Book {
  3. title
  4. synopsis
  5. author {
  6. name
  7. }
  8. }
  9. }
Mutations - 图2Copy
  1. select (
  2. delete Book
  3. ) {
  4. title,
  5. synopsis,
  6. author: {
  7. name
  8. }
  9. };
Mutations - 图3Copy
  1. mutation delete_book_spam {
  2. delete_Book(
  3. filter: {
  4. title: {
  5. eq: Spam
  6. }
  7. }
  8. ) {
  9. title
  10. synopsis
  11. }
  12. }
Mutations - 图4Copy
  1. select (
  2. delete Book
  3. filter
  4. Book.title = Spam
  5. ) {
  6. title,
  7. synopsis
  8. };
Mutations - 图5Copy
  1. mutation delete_one_book {
  2. delete_Book(
  3. filter: {
  4. author: {
  5. name: {
  6. eq:
  7. Lewis Carroll
  8. }
  9. }
  10. },
  11. order: {
  12. title: {
  13. dir: ASC
  14. }
  15. },
  16. first: 1
  17. ) {
  18. title
  19. synopsis
  20. }
  21. }
Mutations - 图6Copy
  1. select (
  2. delete Book
  3. filter
  4. Book.author.name =
  5. Lewis Carroll
  6. order by
  7. Book.title ASC
  8. limit 1
  9. ) {
  10. title,
  11. synopsis
  12. };

Insert​

The “insert” mutation exists for every object type. It allows creating new objects and supports nested insertions, too. The objects to be inserted are specified via the data parameter, which takes a list of specifications. Each such specification has the same structure as the object being inserted with required and optional fields (although if a field is required in the object, but has a default it’s optional in the insert specification):

GraphQL

EdgeQL equivalent

Mutations - 图7Copy
  1. mutation insert_books {
  2. insert_Book(
  3. data: [{
  4. title: One
  5. }, {
  6. title: Two
  7. }]
  8. ) {
  9. id
  10. title
  11. }
  12. }
Mutations - 图8Copy
  1. select {
  2. (insert Book {
  3. title := One
  4. }),
  5. (insert Book {
  6. title := Two
  7. })
  8. } {
  9. id,
  10. title
  11. };

It’s possible to insert a nested structure all at once (e.g. a new book and a new author):

GraphQL

EdgeQL equivalent

Mutations - 图9Copy
  1. mutation insert_books {
  2. insert_Book(
  3. data: [{
  4. title: Three”,
  5. author: {
  6. data: {
  7. name:
  8. Unknown
  9. }
  10. }
  11. }]
  12. ) {
  13. id
  14. title
  15. }
  16. }
Mutations - 图10Copy
  1. select (
  2. insert Book {
  3. title := Three”,
  4. author := (
  5. insert Author {
  6. name :=
  7. Unknown
  8. }
  9. )
  10. }
  11. ) {
  12. id,
  13. title
  14. };

It’s also possible to insert a new object that’s connected to an existing object (e.g. a new book by an existing author). In this case the nested object is specified using filter, order, and various pagination parameters to define a set of objects to be connected:

GraphQL

EdgeQL equivalent

Mutations - 图11Copy
  1. mutation insert_book {
  2. insert_Book(
  3. data: [{
  4. title: Four”,
  5. author: {
  6. filter: {
  7. name: {eq: Unknown”}
  8. }
  9. }
  10. }]
  11. ) {
  12. id
  13. title
  14. }
  15. }
Mutations - 图12Copy
  1. select (
  2. insert Book {
  3. title := Four”,
  4. author := (
  5. select Author
  6. filter
  7. Author.name =
  8. Unknown
  9. )
  10. }
  11. ) {
  12. id,
  13. title
  14. };

Update​

The “update” mutation has features that are similar to both an “insert” mutation and a query. On one hand, the mutation takes filter, order, and various pagination parameters to define a set of objects to be updated. On the other hand, the data parameter is used to specify what and how should be updated.

The data parameter contains the fields that should be altered as well as what type of update operation must be performed (set, increment, append, etc.). The particular operations available depend on the type of field being updated.

GraphQL

EdgeQL equivalent

Mutations - 图13Copy
  1. mutation update_book {
  2. update_Book(
  3. filter: {
  4. title: {
  5. eq: One
  6. }
  7. }
  8. data: {
  9. synopsis: {
  10. set: TBD
  11. }
  12. author: {
  13. set: {
  14. filter: {
  15. name: {
  16. eq:
  17. Unknown
  18. }
  19. }
  20. }
  21. }
  22. }
  23. ) {
  24. id
  25. title
  26. }
  27. }
Mutations - 图14Copy
  1. with
  2. Upd := (
  3. update Book
  4. filter
  5. Book.title =
  6. One
  7. set {
  8. synopsis :=
  9. TBD”,
  10. author := (
  11. select Author
  12. filter
  13. Author.name =
  14. Unknown
  15. )
  16. }
  17. )
  18. select Upd {
  19. id,
  20. title
  21. };