-
Notifications
You must be signed in to change notification settings - Fork 231
Adding initial thoughts on Kiota JS for Graph #558
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
Merged
sebastienlevert
merged 13 commits into
microsoftgraph:dev
from
sebastienlevert:sebastienlevert/v4-js-e2e
Feb 15, 2022
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
774d226
Adding initial thoughts on Kiota JS for Graph
sebastienlevert d261c77
Revert "Adding initial thoughts on Kiota JS for Graph"
sebastienlevert 15fc99f
Adding kiota e2e
sebastienlevert 7a17e7f
Updates
sebastienlevert 798159c
Update design/kiota-e2e.md
sebastienlevert dd24ad9
Adding types from our package
sebastienlevert af9ab8a
Merge branch 'sebastienlevert/v4-js-e2e' of https://github.com/sebast…
sebastienlevert 5d3bd24
Adding more concepts to the e2e
sebastienlevert a8a9794
Merge branch 'dev' into sebastienlevert/v4-js-e2e
nikithauc 2098f52
Merge branch 'dev' into sebastienlevert/v4-js-e2e
nikithauc b3a5c02
Merge branch 'dev' into sebastienlevert/v4-js-e2e
nikithauc 68b5341
Merge branch 'microsoftgraph:dev' into sebastienlevert/v4-js-e2e
sebastienlevert 53b94a8
Updates to the packages + Reformatingnm
sebastienlevert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
# End-to-end usage of kiota-generated SDKs in Javascript | ||
|
||
While working with the Kiota + Javascript teams, it became clear that we needed more context and provide a better vision on how we should be using a kiota-generated SDK. This design document aims at providing clarity and a better understanding on how developers will be using our SDKs and the underlying kiota packages. | ||
|
||
## Status | ||
|
||
| Date | Version | Author | Status | | ||
| ------------------- | ------- | ---------------- | ------ | | ||
| February 15th, 2022 | v0.2 | Sébastien Levert | Draft | | ||
| January 17th, 2022 | v0.1 | Sébastien Levert | Draft | | ||
|
||
## Constraints | ||
|
||
Before we jump into the end-to-end walk-through, it's important to set some constraints. | ||
|
||
| Type | Environment | Description | | ||
| --------- | ----------- | ------------------------------------------------------------------------------------------------- | | ||
| Platforms | Node | Current and Previous LTS (14 / 16) | | ||
| Platforms | Web | Edge, Chrome, Firefox and Safari (Latest released version + immediate previous version) | | ||
| Modules | Node | ESM + CJS | | ||
| Modules | Web | ESM | | ||
| Types | All | Typings / Models should be available for both the core and the service libraries for Graph models | | ||
|
||
## Table of Contents | ||
|
||
- [NodeJS e2e using the Service library](#node-service-library) | ||
- [NodeJS e2e using the Core library](#node-core-library) | ||
|
||
## <a id="node-service-library"></a> NodeJS e2e using the Service library | ||
|
||
### Concept | ||
|
||
This example is utilizing both the core and the service library. That means that as a developer, we want the full fluent API, allowing me discover APIs via the intellisence. Because the service library leverages the core library, we also want to highlight that we can leverage the core components directly (like the `.api()` method). | ||
|
||
### Install the Microsoft Graph Javascript Service Library | ||
|
||
There will be two versions of the service library for the Graph Javascript SDK. The default one will bring the representation of the v1.0 of Microsoft Graph. The second one will bring the representation of the beta of Microsoft Graph. This will be the only difference between these two libraries, as all the handcrafted code (delivering authentication, middlewares, tasks, etc.) will happen in the core library. | ||
|
||
The Microsoft Graph Javascript Service Library includes the following packages: | ||
|
||
- `@microsoft/msgraph-sdk-javascript` ([npm](https://www.npmjs.com/package/@microsoft/msgraph-sdk-javascript-core)) - The service library for making fluent calls to Microsoft Graph v1.0. | ||
- `@microsoft/msgraph-sdk-javascript-beta` ([npm](https://www.npmjs.com/package/@microsoft/msgraph-sdk-javascript-type)) - The service library for making fluent calls to Microsoft Graph beta. | ||
|
||
You can use [npm](https://www.npmjs.com) to install the Microsoft Graph Javascript Service Library : | ||
|
||
```shell | ||
npm install @microsoft/msgraph-sdk-javascript --save | ||
``` | ||
|
||
### Importing the right functionalities from the Graph Javascript Service Library | ||
|
||
```typescript | ||
// App.ts | ||
|
||
import { Client } from "@microsoft/msgraph-sdk-javascript"; | ||
import { User, Message, BodyType } from "@microsoft/msgraph-sdk-javascript/models"; | ||
|
||
// The authentication providers would get re-exported from @microsoft/msgraph-sdk-javascript-core/authentication | ||
import { AzureIdentityAuthenticationProvider } from "@microsoft/msgraph-sdk-javascript/authentication"; | ||
import { DeviceCodeCredential } from "@azure/identity"; | ||
``` | ||
|
||
### Setting up the app to work with the Graph Javascript Service Library | ||
|
||
```typescript | ||
const deviceCodeCredentials = new DeviceCodeCredential({ | ||
tenantId: "b61f9af1-d6cf-4cc0-a6f6-befb38bc00ed", | ||
clientId: "bde251a6-0ef9-42a8-a40b-9ad9bb594b2c", | ||
}); | ||
|
||
const scopes = ["User.Read", "Mail.Send"]; | ||
|
||
const graphClient = Client.init({ | ||
authenticationTokenProvider: new AzureIdentityAuthenticationProvider(deviceCodeCredentials, scopes), | ||
}); | ||
``` | ||
|
||
### Calling Microsoft Graph via the Graph Javascript Service Library | ||
|
||
The core value of the Microsoft Graph Javascript Service Library is the availability of the Fluent API. This provides developers with typechecking and discoverability when building on Microsoft Graph. This Fluent API is made available via the Request Builder concept and highlights the full spectrum of capabilities on Microsoft Graph. | ||
|
||
Models are also available in this package and should reflect the underlying version of Graph we are targeting. Developers using our types should do it in a way that will be familiar to how they are used to with other SDKs and APIs. We should provide an easy-to-use model that feels natural in the Javascript world. This means we should not be forcing developers to use Classes and / or other structures that will make their code less natural to them. | ||
|
||
We have an open issue about the above topic that can be followed here : [TypeScript - Use of interface models instead of class models](https://github.com/microsoft/kiota/issues/1013) | ||
|
||
```typescript | ||
const me: User | undefined = await getMe(); | ||
const meRaw: User | undefined = await getMeRaw(); | ||
await sendMail(); | ||
await sendMailRaw(); | ||
|
||
// The types returned by the fluent API should be the same as the .api() area. It should also be the same (or at least very similar) as the current @microsoft/microsoft-graph-types to offer seamless migration. | ||
async function getMe(): Promise<User | undefined> { | ||
return await graphClient.me.get(); | ||
} | ||
|
||
// Allowing raw calls (using the .api() method instead of the full fluent API) is important for migration purposes and cases we don't know the resource beforehands (thinking Graph Explorer, mgt-get, etc.) | ||
sebastienlevert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
async function getMeRaw(): Promise<User | undefined> { | ||
return await graphClient.api("/me").get<User>(); | ||
} | ||
|
||
// Sending an email via the fluent API | ||
async function sendMail(): Promise<void> { | ||
const message: Message = { | ||
baywet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
subject: "Hello Graph TypeScript SDK!", | ||
body: { | ||
contentType: BodyType.Html, | ||
content: "<bold>Hello Graph TypeScript SDK!</bold>", | ||
}, | ||
toRecipients: [ | ||
{ | ||
emailAddress: { | ||
address: "admin@m365x263716.onmicrosoft.com", | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
return await client.me.sendMail.post(message); | ||
} | ||
|
||
// Sending the email via the .api() method | ||
async function sendMailRaw(): Promise<void> { | ||
const message: Message = { | ||
subject: "Hello Graph TypeScript SDK!", | ||
body: { | ||
contentType: BodyType.Html, | ||
content: "<bold>Hello Graph TypeScript SDK!</bold>", | ||
}, | ||
toRecipients: [ | ||
{ | ||
emailAddress: { | ||
address: "admin@m365x263716.onmicrosoft.com", | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
return await client.api("/me/sendMail").post({ | ||
message: message, | ||
}); | ||
} | ||
``` | ||
|
||
## <a id="node-core-library"></a> NodeJS e2e using the Core library | ||
|
||
### Concept | ||
|
||
This example brings very similar concepts, but utilizing only the core library. That means that as a developer, we only care about the core capabilities (authentication, serialization, etc.) but we don't need / want the fluent API. This also highlights that types are still important in this case (similarly to how they were important with the v3 of the SDK using our Typings). | ||
sebastienlevert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Install the Microsoft Graph Javascript Core SDK | ||
|
||
The Microsoft Graph Javascript Core SDK includes the following packages: | ||
|
||
- `@microsoft/msgraph-sdk-javascript-core` ([npm](https://www.npmjs.com/package/@microsoft/msgraph-sdk-javascript))- The core library for making calls to Microsoft Graph. | ||
- `@microsoft/msgraph-sdk-javascript-type` ([npm](https://www.npmjs.com/package/@microsoft/msgraph-sdk-javascript-type)) - The Typescript types for the Microsoft Graph entities. | ||
|
||
You can use [npm](https://www.npmjs.com) to install the Microsoft Graph Javascript SDK: | ||
|
||
```Shell | ||
npm install @microsoft/msgraph-sdk-javascript-core --save | ||
npm install @microsoft/msgraph-sdk-javascript-types --save-dev | ||
``` | ||
|
||
As a developer, the only package you really need is the `@microsoft/msgraph-sdk-javascript-core` one. This will deliver the expected capabilities (authentication, middlewares, tasks, etc.) and should be used in a comprehensive way for existing NodeJS developers. The types are recommended but optional. | ||
|
||
### Importing the right functionalities from the Graph Javascript Core SDK | ||
|
||
```typescript | ||
import { Client } from "@microsoft/msgraph-sdk-javascript-core"; | ||
import { User, Message, BodyType } from "@microsoft/msgraph-sdk-javascript-core-types"; | ||
import { AzureIdentityAuthenticationProvider } from "@microsoft/msgraph-sdk-javascript-core/authentication"; | ||
import { DeviceCodeCredential } from "@azure/identity"; | ||
``` | ||
|
||
### Setting up the app to work with the Graph Javascript Core SDK | ||
|
||
There should not be a difference between using the core library and the service library. The setup code should remain the same to deliver a simplified developer experience. | ||
|
||
```typescript | ||
const deviceCodeCredentials = new DeviceCodeCredential({ | ||
tenantId: "b61f9af1-d6cf-4cc0-a6f6-befb38bc00ed", | ||
clientId: "bde251a6-0ef9-42a8-a40b-9ad9bb594b2c", | ||
}); | ||
|
||
const scopes = ["User.Read", "Mail.Send"]; | ||
|
||
const graphClient = Client.init({ | ||
authenticationTokenProvider: new AzureIdentityAuthenticationProvider(deviceCodeCredentials, scopes), | ||
}); | ||
``` | ||
|
||
### Calling Microsoft Graph via the Graph Javascript Core SDK | ||
|
||
Calling the API should be done in a familiar way and should most importantly return the same models that the service library returns. That means that I can build on top of either modules without the need to redefine the types that I'll be receiving back from the API. | ||
|
||
Developers using our types should do it in a way that will be familiar to how they are used to with other SDKs and APIs. We should provide an easy-to-use model that feels natural in the Javascript world. This means we should not be forcing developers to use Classes and / or other structures that will make their code less natural to them. | ||
|
||
We have an open issue about the above topic that can be followed here : [TypeScript - Use of interface models instead of class models](https://github.com/microsoft/kiota/issues/1013) | ||
|
||
```typescript | ||
const me: User = await getMe(); | ||
await sendMail(); | ||
|
||
async function getMe(): Promise<User | undefined> { | ||
return await graphClient.api("/me").get<User>(); | ||
} | ||
|
||
async function sendMail(): Promise<void> { | ||
const message: Message = { | ||
subject: "Hello Graph TypeScript SDK!", | ||
body: { | ||
contentType: BodyType.Html, | ||
content: "<bold>Hello Graph TypeScript SDK!</bold>", | ||
}, | ||
toRecipients: [ | ||
{ | ||
emailAddress: { | ||
address: "admin@m365x263716.onmicrosoft.com", | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
return await client.api("/me/sendMail").post({ | ||
message: message, | ||
}); | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.