Search code examples
phpcomposer-php

Installing vendor library using composer


I'm trying to use the https://github.com/webonyx/graphql-php/ library to make graphql requests.

But for some reason it doesn't work, and I can't figure out why.

I'm using this command to install:

composer require webonyx/graphql-php

Vendor folder with content + composer.json / composer.lock appear in the project folder.

When I try a simple code like:

<?php

require_once 'vendor/autoload.php';

use GraphQL\Client;
use GraphQL\Query;


// Replace with your actual GraphQL endpoint and API key
$graphqlEndpoint = 'https://platformurl';

// Set up the GraphQL client
$client = new Client($graphqlEndpoint);

?>

Fatal error: Uncaught Error: Class "GraphQL\Client" not found in /url/test.php:14 Stack trace: #0 {main} thrown in /url/test.php on line 14

When I check composer.json it contains this:

{
    "require": {
        "webonyx/graphql-php": "^15.7"
    }
}

I tried composer install or update. I tried a require with the direct link to graphql folder. But there is no difference.

I found that there was no src/client.php file in the graphql-php library folder, but I don't know if this is necessary or how that comes (chatGPT said this is the reason why it doesn't works)..

PHP Version is PHP 8+. What could be the reason?


Solution

  • As you have seen, there is no GraphQL\Client (or Query) classes. Wherever you got the "simple code" from is not how the package is used. I suggest looking in the examples directory and other documentation in the repository to see how it can actually be used.