Yes, we can call GraphQL in another client with http post. But here I want call in the host laravel.
+use Nuwave\Lighthouse\GraphQL;
+// use Nuwave\Lighthouse\Support\Contracts\CreatesContext;
+use Nuwave\Lighthouse\Execution\ContextFactory;
+// use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
/*
|--------------------------------------------------------------------------
| API Routes
@@ -38,20 +42,15 @@
}
}
GQL;
- $graphqlEndpoint = config('app.url') . '/graphql';
- $client = new \GuzzleHttp\Client();
- $response = $client->request('POST', $graphqlEndpoint, [
- 'headers' => [
- 'Content-Type' => 'application/json',
- ],
- 'json' => [
- 'query' => $query
- ]
- ]);
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- return ['data' => $body['data']['data']];
+ $graphQL = app(GraphQL::class);
+ $createsContext = app(ContextFactory::class);
+ $context = $createsContext->generate($request);
+ $result = $graphQL->executeQueryString($query, $context);
+
+ return ['data' => $result['data']['data']]
});