Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

knubbe's avatar
Level 36

Add custom property and resolver in Laravel Lighthouse query

I have a Customer model / table that I want to query using graphQl (Lighthouse). It should be 99% mapped into model but I want to add additional property into that query.

My Customer model have:

id, name, identification_number...

but as a part of query response I want to add validate property which should be resolved by api call using identification_number from model as argument.

What is the best possible solution for this?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To add a custom property and resolver in a Laravel Lighthouse query, you can follow these steps:

  1. Define the Custom Property in Your GraphQL Schema: First, you need to define the validate property in your GraphQL schema. You can do this by adding it to your Customer type definition.

    type Customer {
        id: ID!
        name: String!
        identification_number: String!
        validate: Boolean!
    }
    
  2. Create a Resolver for the Custom Property: Next, you need to create a resolver that will handle the logic for the validate property. This resolver will use the identification_number to make an API call and return the validation result.

    Create a new resolver class, for example, CustomerValidator.php:

    namespace App\GraphQL\Resolvers;
    
    use App\Models\Customer;
    
    class CustomerValidator
    {
        public function validate(Customer $customer)
        {
            // Replace this with your actual API call logic
            $identificationNumber = $customer->identification_number;
            $isValid = $this->callExternalApi($identificationNumber);
    
            return $isValid;
        }
    
        private function callExternalApi($identificationNumber)
        {
            // Simulate an API call
            // In a real scenario, you would use something like Guzzle to make the HTTP request
            // and process the response accordingly.
            return $identificationNumber === 'valid_id';
        }
    }
    
  3. Register the Resolver in Your GraphQL Schema: You need to tell Lighthouse to use your custom resolver for the validate field. You can do this by using the @field directive in your schema.

    type Customer {
        id: ID!
        name: String!
        identification_number: String!
        validate: Boolean! @field(resolver: "App\\GraphQL\\Resolvers\\CustomerValidator@validate")
    }
    
  4. Update Your GraphQL Query: Now, you can query the validate property along with other properties of the Customer model.

    query {
        customer(id: 1) {
            id
            name
            identification_number
            validate
        }
    }
    
  5. Test Your Implementation: Finally, test your implementation by running the GraphQL query and ensuring that the validate property is correctly resolved based on the identification_number.

By following these steps, you can add a custom property to your GraphQL query in Laravel Lighthouse and resolve it using an API call.

Please or to participate in this conversation.