Search code examples
laravelgraphqllaravel-lighthouse

Laravel Lighthouse GraphQL create mutation without "input:"


I'm looking a the documentation for Laravel Lighthouse and I am seeing two types of mutations.

mutation {
  createPost(input: { # <-- the "input:" I'm talking about
    title: "My new Post"
    author: {
      connect: 123
    }
  }){
    id
    author {
      name
    }
  }
}

And another mutation without the input: (found here)

mutation CreateTaskWithNotes {
  createTask( # <-- no "input:" here
    id: 45
    name: "Do something"
    notes: [
      {
        content: "Foo bar",
        link: "http://foo.bar"
      },
      {
        content: "Awesome note"
      }
    ]
  ) {
    id
  }
}

My question is: How do I get the mutations without input: to work?

I try to copy (an modify) the examples from the documentation. But if I write a mutation like this:

type Mutation {
    createTask(input: CreateTaskInput! @spread): Task! @create
}

When I try to omit input:, graphql-playground complains: "Field createTask argument input of type CreateTaskInput is required but not provided"

Now I try to change the schema to this:

type Mutation {
    createTask(CreateTaskInput! @spread): Task! @create
}

But then the server gives a ParseException.

I do prefer the syntax without input: because that is a lot less repetitive. Can anybody help?


Solution

  • If you want to write a mutation without the input, also ommit the @spread directive. So:

    type Mutation {
        createTask(
            id: ID
            name: String
        ): Task! @create
    }
    

    But I think it's a "best practices" to have that inside an input. Of course you can do wethever you want to do.