skip to Main Content

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 appears in the project folder.

When I try a simple code like:

<?php

require_once 'vendor/autoload.php';

use GraphQLClient;
use GraphQLQuery;


// 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 "GraphQLClient" 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 / 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?

3

Answers


  1. If your useing autoload after adding new class you should run composer dump-autoload to cached list of classes

    Login or Signup to reply.
  2. As you have seen, there is no GraphQLClient (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.

    Login or Signup to reply.
  3. I understand you have the php file in the src folder. Change the path to the vendor folder

    - vendor
    - src
    -- client.php
    
    require __DIR__ . '/../vendor/autoload.php';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search