Microsoft Graph provides a unified programmability model that you can
use to build apps for organizations and consumers that interact with the
data of millions of users. You can easily access Microsoft Graph also
from SharePoint Framework solutions.
Microsoft Graph exposes REST APIs and client libraries to access data on
the following Microsoft services like Bookings, Calendar, Delve, Excel,
Microsoft 365 compliance eDiscovery, Microsoft Search, OneDrive,
OneNote, Outlook/Exchange, People (Outlook contacts), Planner,
SharePoint, Teams, To Do, Workplace Analytics. for more details refer to
Graph API.
How to test API in graph explorer?
Before creating an SPFx web part implementation with Graph API we can
explore APIs in Graph
Explorer.
At here To access your own data you have to log in with your O365
Credentials.
Whenever you sign in it will ask for Permission so you have to Accept
it.
In the left panel, you can find multiple sample queries by category so
you can check them and run them.
If you are testing any API and get an error like this:
Forbidden - 403 - You need to consent to the permissions on the Modify permissions (Preview) tab.
Then select the category in which you want to set permission and
select the Consent button.
3 After clicking on the Consent it will open a popup so check the
checkbox and Accept it.
Then select the run query and it will retrieve the results. that’s
it :)
Now lets’ move to the SPFx implementation.
Implementation
Open a command prompt Move to the path where you want to create a project Create a project directory using:
md GraphAPIDemo
Move to the above-created directory using:
cd GraphAPIDemo
Now execute the below command to create an SPFx solution:
yo @microsoft/sharepoint
It will ask some questions, as shown below,

After a successful installation, we can open a project in any source
code tool. Here, I am using the VS code, so I will execute the command:
code.
Now will create a demo to read messages of a user. for more details
refer to
Microsoft Graph.
So first of all we have to set permission as per our endpoint in
package-solution.json. Now the question is which permission we have
to add so there are multiple ways.
1. In the graph explorer
For eg. we have to use messages endpoint so first, we will test it in
graph explorer. so while we running the query there is an permission
tab under the run query button so after the response checks the
permission tab. It shows all required permissions for the endpoint as
below.

2. Official documentation for endpoint
You can also find all the details in the official document for all the
endpoints.
Implementation
1. In the package-solution.json file we will add permission.
4. Move to the**{WebpartName}Webpart.ts**
Import MSGraphClient module
Create an OnInit() to initialize the Graph Configuration for the current
context.
In the render() set the graphClient property