تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Posting GraphQL via HTTP0:00

Okay, now let's take a look at how we can make requests from a separate Laravel app acting as the front end. So I have a blank Laravel app here, which we'll make use of. And as of version 7, Laravel ships with an HTTP client that we can make use of. So let's do that. So into our code, let's make use of that HTTP client. So we can say $response equals HTTP::post request to this endpoint here. So let's grab that. Let's paste it in here. And remember, we need to pass JSON for the body of the request.

Let's paste it in here. And remember, we need to pass JSON for the body of the request. And to do that, we can pass in an array as a second argument here. So remember, the key is query, and the value is our GraphQL query or mutation. So let's make this a string. And within here, we can paste in our query here. So let's grab it from here. Let's start with the query, and then we'll do the mutation next. So let me comment this back in. And let's copy that, let's paste that in here.

So let me comment this back in. And let's copy that, let's paste that in here. And then we just reformat this. Okay. And now we can say, or for now, let's just type in dump response JSON. And let's make sure to import HTTP here. And let's try that out in the browser. And we do get the correct information here. If we drill down, we'll see we have data, posts, and here are our first 10 posts here. Cool.

Passing Posts to View1:33

If we drill down, we'll see we have data, posts, and here are our first 10 posts here. Cool. So let's just pass that into the view and output it in the browser. So we'll pass that in here. And we'll say, posts is, say, response JSON. And it's an array, and the key was data. And then we have posts, and then we have data again. Okay. And now we should be able to loop over that. So let me just get rid of this.

And now we should be able to loop over that. So let me just get rid of this. Let's go to welcome.blade.php. And down here, we can just loop over it. So I'll just put it here. So let's just make an unordered list here. And we can say foreach $posts as $post, we can say listItem, and we can say $post->title. And now hopefully this should work. Sorry, it has to be array syntax. So let's just change this to array syntax here.

Switching to Mutation3:04

And then we'll just update the query here to a mutation. So we can get rid of this. Let's copy the mutation we have in here. So let's comment this out. Let's bring this back. Let's copy it. And let's paste it in here. And let me just update this. Let's say hello from Laravel. And that should be it.

Let's say hello from Laravel. And that should be it. Let's just return the response here. So response JSON. And we'll just check the database to make sure it worked. So let's go to /create. And we have an error here. I think we have an extra curly here, which we don't need. Let's try that one more time. And we do get the correct data back.

Let's try that one more time. And we do get the correct data back. So let's just double check in here. Let's refresh. And there it is. Cool. So pretty straightforward. We can just make use of the HTTP client. And you should easily be able to make queries to your GraphQL backend.

And you should easily be able to make queries to your GraphQL backend.

Use Laravel’s HTTP client to make GraphQL requests

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟