Skip to content

Conversation

@cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Dec 8, 2025

I had tried this previously in #57944 but now I have a slightly different reason for needing it. Here we are just passing it to the Request object, and my vision is that we would use this for essentially naming the call.

Why

The company I work for integrates with Shopify's GraphQL API. Every request hits the same endpoint, and as such, writing unit tests with stubbed HTTP responses is no fun, because your best bet is to compare against a string that's in the request... but you sort of have to know what's the most unique string in the GraphQL query. 😿

An example

public function test_it_fetches_shops_and_scopes()
{
    Http::fake(['*graphql.json' => function (Request $request) {
            // This is real, real ugly
            $queryPayload = $request->data()['query'] ?? '';
            if (str_contains($queryPayload, 'publicDisplayName')) {
                return Http::response(self::getGetShopQueryResponse());
            }
            if (str_contains($queryPayload, 'currentAppInstallation')) {
                return Http::response(self::getGetShopScopesResponse());
            }
    }]);
    $merchant = new Merchant(
        'domain' => 'my-shop',
        'api-token' => 'top-secret',
    );
    $this->app->make(MyService::class)->handle($merchant);
}

Instead, it would be great if we could just kind of "name" our requests, like this:

Http::withAttributes(['name' => 'get_shop'])
    ->post('https://some-store.myshopify.com/admin/api/2025-10/graphql.json', $data);

and then inside of the test:

    Http::fake(['*graphql.json' => function (Request $request) {
            // This is less ugly
            $requestContext = $request->attributes()['name'] ?? '';
            if ($requestContext === 'get_shop') {
                return Http::response(self::getGetShopQueryResponse());
            }
            // ...
    }]);

Alternatives

Just create a requestName string property. That definitely fits my use case most narrowly, but also, how soon before someone wants to store something else in there? Though that would be really nice because when calling $pool->as('get_shop'), that could also set the requestName. 🤷

@WendellAdriel
Copy link
Contributor

If this is only for naming a request, I'd rather have a ->name() or ->withName() instead.

Http::get('https://example.com')->name('foo');
// OR
Http::get('https://example.com')->withName('foo');

I understand your point on this enabling to add more things, but at least I don't see a use case for it.

@taylorotwell
Copy link
Member

This reminded me a lot of "attributes" on Symfony requests which are just arbitrary data values. So I renamed it to that.

@taylorotwell taylorotwell merged commit a98b935 into laravel:12.x Dec 9, 2025
69 of 70 checks passed
@cosmastech cosmastech changed the title [12.x] PendingRequest@withRequestContext() [12.x] PendingRequest@withAttributes() Dec 9, 2025
@cosmastech cosmastech deleted the request-context branch December 11, 2025 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants