ms_graph_api
An unofficial, lightweight wrapper around the
Microsoft Graph SDK for PHP,
pulled-in via Composer, for Drupal 8.x.
Use Case
This module is intended to provide an API for other modules to easily obtain
either a site-wide "default" instance of the MS Graph API client using service
injection, or to be able to instantiate an API client using any number of
custom keys.
This module does not provide site builders or end users with any additional
functionality. Install it only if you are developing a custom module or are
using a module that indicates it depends on this module.
Usage
Installation
It is recommended to install this via Composer so that the required version of
the Graph API PHP SDK can be pulled in.
composer require 'drupal/ms_graph_api'
Registration and Configuration
See the README for instructions on how to register your Drupal site with Azure and configure keys.
Obtaining Graph Instances
Afterwards, you can obtain the default, authenticated API graph by getting the
ms_graph_api.graph service and then calling any of the methods on the object
you get back that are
documented for the PHP library.
For example, in procedural code and hooks:
use Microsoft\Graph\Model;
$graph = \Drupal::service('ms_graph_api.graph');
$user =
$graph
->createRequest("GET", "/me")
->setReturnType(Model\User::class)
->execute();
echo "Hello, I am {$user->getGivenName()}.";
Getting a MS Graph API Graph for a Custom Key
If your site needs to work with multiple Azure subscriptions, you do not need
to configure the default key. Instead, define custom keys (of type
"MS Graph API Key") for each of the different deployments, obtain the Graph
API graph factory service, and construct a different API client for each custom
key.
For example:
use Microsoft\Graph\Model;
$graph_factory = \Drupal::service('ms_graph_api.graph.factory');
$graph = $client_factory->buildGraphFromKeyId('my_graph_api_key');
$user =
$graph
->createRequest("GET", "/me")
->setReturnType(Model\User::class)
->execute();
echo "Hello, I am {$user->getGivenName()}.";