How to Build an Invite System into Your App Using Laravel and GraphQL

API Exsolve.

Invitation system is another interaction option where users can send a unique code or link to someone in your app. For example, you are developing a social platform or product and want people to share keys for it. Such a system improves the user experience and can improve the quality of your services.

Laravel is a powerful tool for creating backends

Laravel has long established itself as an excellent framework for PHP developers. Its convenient syntax, clear code structure, and a huge number of built-in capabilities make it an ideal choice for creating APIs for applications. In our case we use routes, controllers, models and integration with GraphQL.

We will also use GraphQL in the project: unlike the traditional REST API, it allows us to get exactly the data we need, without redundant data. We will use GraphQL to communicate between our client and server to communicate invitation and user information. Queries via GraphQL are more flexible and powerful, and their configuration for Laravel is simple and logical.

Step 1: Setting up Laravel

Let's assume you're already familiar with the basics of installing Laravel and working with migrations and models. We'll skip the basic steps and go straight to creating a model for the invitations. For those who have no experience with Laravel, the project has a guide for a quick start in the official documentation.

The Invite model will contain fields to store information about the sender, the recipient, and the status of the invitation. First, let's create a migration:

php artisan make:model Invite -m

Add fields to it:

Schema::create('invites', function (Blueprint $table) {
    $table->id();
    $table->foreignId('sender_id')->constrained('users')->onDelete('cascade');
    $table->string('invitee_phone');
    $table->string('invite_code')->unique();
    $table->enum('status', ['pending', 'accepted', 'declined'])->default('pending');
    $table->timestamps();
});

Now we have a framework that will allow us to store information about invitations. This is important because next we will work with this model to create and send messages.

Step 2: GraphQL Queries for Invites

Now that we have a model, we need to integrate it with GraphQL. If you haven't yet connected GraphQL to Laravel, I recommend using the laravel-graphq library – it will make your life much easier.

First, let's create several mutations through which we will create new invitations:

use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Mutation;
use App\Models\Invite;

class CreateInviteMutation extends Mutation
{
    protected $attributes = [
        'name' => 'createInvite'
    ];

    public function type(): Type
    {
        return Type::boolean();
    }

    public function args(): array
    {
        return [
            'invitee_phone' => ['name' => 'invitee_phone', 'type' => Type::string()],
        ];
    }

    public function resolve($root, $args)
    {
        $invite = Invite::create([
            'sender_id' => auth()->id(),
            'invitee_phone' => $args['invitee_phone'],
            'invite_code' => Str::random(10),
        ]);

        return $invite ? true : false;
    }
}

This mutation will add a new entry to the invitation table and generate a unique code, which we will later send via SMS.

Step 3: Integration with Exolve API

Now comes the fun part – sending messages via SMS. For this we will use the Exolve API. If you are not yet familiar with it, I recommend contacting documentation — everything is quite simple and clear there.

In our case, we need to send a POST request with the invitee’s phone number and message text. To do this, we will write a service that will be responsible for integration with the Exolve API.

Let's create a new service class:

namespace App\Services;

use Illuminate\Support\Facades\Http;

class SmsService
{
    protected $apiUrl="https://api.exolve.com/send";

    public function sendInvite($phone, $message)
    {
        $response = Http::post($this->apiUrl, [
            'phone' => $phone,
            'message' => $message,
        ]);

        return $response->successful();
    }
}

Now we need to call this service at the time of creating the invitation in order to send it via SMS. We can do this directly in our GraphQL mutation:

use App\Services\SmsService;

class CreateInviteMutation extends Mutation
{
    public function resolve($root, $args)
    {
        $invite = Invite::create([
            'sender_id' => auth()->id(),
            'invitee_phone' => $args['invitee_phone'],
            'invite_code' => Str::random(10),
        ]);

        $smsService = new SmsService();
        $smsService->sendInvite($args['invitee_phone'], 'Ваш код приглашения: ' . $invite->invite_code);

        return $invite ? true : false;
    }
}

As you can see, this is a simple and convenient way to integrate with Exolve. In this option, we send a unique code via SMS, but we can take, for example, links instead.

Conclusion

Integrating an invitation system using Laravel and GraphQL is not only functional but also flexible. You can add new types of messages, change the distribution logic, or integrate other APIs. In this example we used Exsolve for sending SMS, but in general the logic is universal for other essentially similar projects.

We hope this example will help simplify the user experience. If you are interested in this topic, then leave your questions in the comments, I will explain in more detail.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *