msgraph-sdk-php icon indicating copy to clipboard operation
msgraph-sdk-php copied to clipboard

Better support for developers who prefer URLs over the fluent API

Open Ndiritu opened this issue 1 year ago • 6 comments

Sample v1 snippet


// v1.x
$response = $graphClient->createRequest('GET', '/users/userId/messages')
                        ->setReturnType(Model\Message::class)
                        ->execute();

Perhaps make models a separate package? This way developers can use graph core and the models package only. (Full service lib would also take a dependency on the models)

Developer uses native HTTP client and handles deserialization themselves


$guzzleClient = GraphClientFactory::create();
$response = $guzzleClient->request('GET', '/users/userId/messages');
//introduce a new "deserialize" method in models that accepts decoded JSON (a dictionary)?
$messages = (new MessageCollectionResponse())->deserialize(json_decode($response->getBody(), true));

Ndiritu avatar Jul 20 '22 04:07 Ndiritu

cc: @nikithauc @sebastienlevert @ddyett as I hear similar thoughts floating around regarding TS/JS SDKs.

zengin avatar Jul 20 '22 04:07 zengin

We are looking at this functionality from a global SDK perspective. Something that will take "time" but should get there at some point.

JavaScript has been using this approach since day one with a .api("/me") approach and has been very successful (though, there was no service library). We would love to bring a similar concept to all languages, using our middleware pipeline and error management, etc. to provide this value.

In this case, models would need to be separate and you could use core + models to get to this result!

Adding @darrelmiller for visibility

sebastienlevert avatar Jul 20 '22 15:07 sebastienlevert

@sebastienlevert @Ndiritu We have had our JS/TS community asking for a fluent experience for 4 years. Using paths is a fallback, not a preference. It is also useful for making requests when a complete path is returned from the server in a response. The motivation to provide a path based API has not been "because some people prefer it". It is because in some cases our fluent API is not sufficient. We should always consider the path based API a backup option to the strongly typed request factory experience. Constructing URLs can be fraught with errors, especially when it comes to character encoding. We should always consider the strongly typed experience our north star.

darrelmiller avatar Jul 21 '22 01:07 darrelmiller

Thanks for providing context @darrelmiller. I'll revisit this in case we receive relevant customer feedback.

Ndiritu avatar Jul 27 '22 06:07 Ndiritu

I'm trying to convert my application to the new fluent API. I specifically want to download profile photos for all the users in my organization, and was able to do individual photos using

$photoContent = $this->graphServiceClient->usersById($user->id)->photo()->content()->get()->wait();

But in previous versions I could make an array of requests and send them all at once like

$data = $this->graph
            ->createRequest('POST', '/$batch')
            ->attachBody(json_encode(['requests' => $requests]))
            ->execute();

I can't for the life of me figure out how to do this now. Is this even possible with the fluent API? I am tempted to go back to the old API and lock my composer.json at msgraph-sdk-php RC4 so my old code works.

Any advice on how I could approach this would be appreciated!

jdlien avatar Jul 29 '22 00:07 jdlien

@jdlien your issue is addressed in #957

Ndiritu avatar Jul 29 '22 05:07 Ndiritu