Can we change/alter nested mutation using Laravel events?

170 Views Asked by At

I want to alter the mutation using Laravel Creating event. I want to fetch task ID from key that is coming from front end. And then i want to add this ID in replace of key so that my task will be create automatically using lighthouse structure. Here is sample mutation

mutation
{
  createUser(input: {
    firstname: "last"
    email: "[email protected]"
    task:
    {
      create: { 
         key: 'reminder'
      }
    }
  })
  {
    id
  }
}
1

There are 1 best solutions below

0
On

My recommendation is to create a resolver for your specific situation:

mutation
{
  createUser(input: {firstname: "last", email: "[email protected]", key: "reminder"})
  {
    id
  }
}

Remember to always use double quotes " ", never use single quotes ' '

In your schema.graphql

input newUser {
    firstname: String!
    email: String!
    key: String!
}

type newUserResponse {
    ID: ID!
}

createUser(data: newUser): newUserResponse @field(resolver: "App\\GraphQL\\Mutations\\createUser")

Here's an example of the resolver: Resolver example

Also check the docs: https://lighthouse-php.com/4.9/api-reference/resolvers.html