Create

Using the following type definitions for these examples:

type Post {
    id: ID! @id
    content: String!
    creator: User @relationship(type: "HAS_POST", direction: IN)
}

type User {
    id: ID! @id
    name: String
    posts: [Post] @relationship(type: "HAS_POST", direction: OUT)
}

The following create Mutations and response types will be generated for the above type definitions:

type CreatePostsMutationResponse {
    posts: [Post!]!
}

type CreateUsersMutationResponse {
    users: [User!]!
}

type Mutation {
    createPosts(input: [PostCreateInput!]!): CreatePostsMutationResponse!
    createUsers(input: [UsersCreateInput!]!): CreateUsersMutationResponse!
}

The CreateInput types closely mirror the object type definitions, allowing you to create not only the type in question, but to recurse down and perform further operations on related types in the same Mutation.

The id field will be absent from both create input types as the @id directive has been used.

Single create

A single user can be created by executing the following GraphQL statement:

mutation {
    createUsers(input: [
        {
            name: "John Doe"
        }
    ]) {
        users {
            id
            name
        }
    }
}

This will create a User with name "John Doe", and that name plus the autogenerated ID will be returned.

Nested create

A User and an initial Post can be created by executing the following:

mutation {
    createUsers(input: [
        {
            name: "John Doe"
            posts: {
                create: [
                    {
                        node: {
                            content: "Hi, my name is John!"
                        }
                    }
                ]
            }
        }
    ]) {
        users {
            id
            name
            posts {
                id
                content
            }
        }
    }
}

This will create a User with name "John Doe", an introductory Post, both of which will be returned with their autogenerated IDs.