Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.14] GraphQL API #772

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class AbstractApi
*
* @var string
*/
private const URI_PREFIX = '/api/v4/';
protected const URI_PREFIX = '/api/v4/';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need to change this? Since you're using static now, it should automatically use the one from the child class, no? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In PHP private means a sub-class cannot extend/override it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does it need to be overwritten? the changes you want don't need to re-use this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GrahamCampbell, GraphQL is versionless. The URL prefix is /api instead of /api/v4, so AbstractApi::prepareUriP(), needs to get /api when subclassed as \Gitlab\Api\GraphQL

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should split version out of URI_PREFIX, into a protected class? Then we override the version in GraphQL as GitLab GraphQL is versionless


/**
* The access levels for groups and projects
Expand Down Expand Up @@ -264,7 +264,7 @@ private static function prepareUri(string $uri, array $query = []): string
return null !== $value;
});

return \sprintf('%s%s%s', self::URI_PREFIX, $uri, QueryStringBuilder::build($query));
return \sprintf('%s%s%s', static::URI_PREFIX, $uri, QueryStringBuilder::build($query));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert these changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think will work. GraphQL needs a different URL prefix

}

/**
Expand Down
52 changes: 52 additions & 0 deletions src/Api/GraphQL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Gitlab API library.
*
* (c) Matt Humphrey <matth@windsor-telecom.co.uk>
* (c) Graham Campbell <hello@gjcampbell.co.uk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gitlab\Api;

class GraphQL extends AbstractApi

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have a few test cases.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should. But I have no time to work on

{
/**
* {@inheritDoc}
*/
protected const URI_PREFIX = '/api';

/**
* @param string $query
* @param array $variables
*
* @return array
*/
public function execute(string $query, array $variables = [])
{
$params = [
'query' => $query,
];
if (!empty($variables)) {
$params['variables'] = \json_encode($variables);
}

return $this->post('/graphql', $params);
}

/**
* @param string $file
* @param array $variables
*
* @return array
*/
public function fromFile(string $file, array $variables = [])
{
return $this->execute(\file_get_contents($file), $variables);
}
}
9 changes: 9 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Gitlab\Api\Deployments;
use Gitlab\Api\Environments;
use Gitlab\Api\Events;
use Gitlab\Api\GraphQL;
use Gitlab\Api\Groups;
use Gitlab\Api\GroupsBoards;
use Gitlab\Api\GroupsEpics;
Expand Down Expand Up @@ -407,6 +408,14 @@ public function wiki(): Wiki
return new Wiki($this);
}

/**
* @return GraphQL
*/
public function graphql(): GraphQL
{
return new GraphQL($this);
}

/**
* Authenticate a user for all next requests.
*
Expand Down