Request headers¶
Some Graph endpoints only work when you send a particular header. Advanced
queries on directory objects are the usual case: they need
ConsistencyLevel: eventual alongside $count=true, and without it Graph answers
with an error about an unsupported query.
Both getGraphData() and sendGraphData() take an array of headers as their last
argument.
Reading¶
public function getGraphData(
string $endpoint,
string $type = 'GET',
bool $raw = FALSE,
bool|string $version = FALSE,
bool|string $returnType = FALSE,
array $headers = [],
): mixed
A call that walks the manager chain of a user, which needs both the beta endpoint and the consistency header:
$headers = [
'ConsistencyLevel' => 'eventual',
];
$data = $this->graphService->getGraphData(
'/users/' . $user_id . '?$expand=manager($levels=50;$select=id,displayName)&$count=true',
'GET',
FALSE,
'beta',
FALSE,
$headers
);
Note the two FALSE values before $headers. They are $raw and $returnType,
and they have to be there because PHP has no way to skip a positional argument.
Named arguments are tidier if you are on PHP 8 anyway:
$data = $this->graphService->getGraphData(
'/users/' . $user_id . '?$expand=manager($levels=50;$select=id,displayName)&$count=true',
version: 'beta',
headers: ['ConsistencyLevel' => 'eventual'],
);
Writing¶
sendGraphData() has the payload in second position and the headers at the end
again:
public function sendGraphData(
string $endpoint,
mixed $data = [],
string $type = 'POST',
bool $raw = FALSE,
bool|string $version = FALSE,
bool|string $returnType = FALSE,
array $headers = [],
): mixed
Older examples¶
If you are copying code from a 5.x site or from an older version of this page,
check the argument count. The signature used to carry an extra parameter between
$version and $returnType, so a call written as
$this->o365Graph->getGraphData($endpoint, 'GET', FALSE, 'beta', [], FALSE, $headers);
now passes seven arguments to a method that takes six, and the empty array lands
on $returnType:
TypeError: Drupal\o365\GraphService::getGraphData(): Argument #5 ($returnType)
must be of type string|bool, array given
Drop the [] and you are back to the current signature.