Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit fa9d376

Browse files
committed
fix(GraphQL): fix GraphQL requests without variables
1 parent 995e90d commit fa9d376

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/Core/Request/GraphQL/GraphQLQueryRequest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@ public function buildResponse(ResponseInterface $response)
4545
public function httpRequest()
4646
{
4747
$body = [
48-
'query' => $this->query,
49-
'variables' => $this->variables,
50-
'operationName' => $this->operationName
48+
'query' => $this->query
5149
];
50+
if (count($this->variables) > 0) {
51+
$body['variables'] = $this->variables;
52+
}
53+
if (!is_null($this->operationName)) {
54+
$body['operationName'] = $this->operationName;
55+
}
5256
return new JsonRequest(HttpMethod::POST, $this->getPath(), $body);
5357
}
5458

tests/integration/GraphQL/GraphQLQueryTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,22 @@ public function testGraphQLEndpoint()
7676
$result['masterData']['staged']['name']
7777
);
7878
}
79+
80+
public function testWithoutVariables()
81+
{
82+
$request = GraphQLQueryRequest::of();
83+
84+
$query = <<<GRAPHQL
85+
query Sphere {
86+
products {
87+
count
88+
}
89+
}
90+
GRAPHQL;
91+
92+
$request->query($query);
93+
94+
$response = $request->executeWithClient($this->getClient());
95+
$this->assertFalse($response->isError());
96+
}
7997
}

0 commit comments

Comments
 (0)