2022-07-25 11:20:04 +12:00
< ? php
namespace Tests\E2E\Services\GraphQL ;
use Tests\E2E\Client ;
use Tests\E2E\Scopes\ProjectCustom ;
use Tests\E2E\Scopes\Scope ;
use Tests\E2E\Scopes\SideClient ;
2023-01-16 22:25:40 +13:00
use Utopia\Database\Helpers\ID ;
2022-07-25 11:20:04 +12:00
2022-09-22 20:29:42 +12:00
class BatchTest extends Scope
2022-08-22 21:13:29 +12:00
{
2022-07-25 11:20:04 +12:00
use ProjectCustom ;
use SideClient ;
public function testArrayBatchedQueries ()
{
$projectId = $this -> getProject ()[ '$id' ];
2022-09-21 19:03:28 +12:00
$query1 = 'query { localeListCountries { total countries { code } } }' ;
$query2 = 'query { localeListContinents { total continents { code } } }' ;
2022-07-25 11:20:04 +12:00
$graphQLPayload = [
[ 'query' => $query1 ],
[ 'query' => $query2 ],
];
$response = $this -> client -> call ( Client :: METHOD_POST , '/graphql' , \array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $projectId ,
], $this -> getHeaders ()), $graphQLPayload );
2022-10-14 14:33:42 +13:00
$this -> assertIsArray ( $response [ 'body' ][ 0 ][ 'data' ]);
$this -> assertIsArray ( $response [ 'body' ][ 1 ][ 'data' ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 0 ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 1 ]);
$this -> assertArrayHasKey ( 'localeListCountries' , $response [ 'body' ][ 0 ][ 'data' ]);
$this -> assertArrayHasKey ( 'localeListContinents' , $response [ 'body' ][ 1 ][ 'data' ]);
$this -> assertEquals ( 194 , $response [ 'body' ][ 0 ][ 'data' ][ 'localeListCountries' ][ 'total' ]);
$this -> assertEquals ( 7 , $response [ 'body' ][ 1 ][ 'data' ][ 'localeListContinents' ][ 'total' ]);
2022-07-25 11:20:04 +12:00
}
public function testArrayBatchedQueriesOfSameType ()
{
$projectId = $this -> getProject ()[ '$id' ];
2022-09-21 19:03:28 +12:00
$query = 'query { localeListCountries { total countries { code } } }' ;
2022-07-25 11:20:04 +12:00
$graphQLPayload = [
[ 'query' => $query ],
[ 'query' => $query ],
];
$response = $this -> client -> call ( Client :: METHOD_POST , '/graphql' , \array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $projectId ,
], $this -> getHeaders ()), $graphQLPayload );
2022-10-14 14:33:42 +13:00
$this -> assertIsArray ( $response [ 'body' ][ 0 ][ 'data' ]);
$this -> assertIsArray ( $response [ 'body' ][ 1 ][ 'data' ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 0 ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 1 ]);
$this -> assertArrayHasKey ( 'localeListCountries' , $response [ 'body' ][ 0 ][ 'data' ]);
$this -> assertArrayHasKey ( 'localeListCountries' , $response [ 'body' ][ 1 ][ 'data' ]);
$this -> assertEquals ( 194 , $response [ 'body' ][ 0 ][ 'data' ][ 'localeListCountries' ][ 'total' ]);
$this -> assertEquals ( 194 , $response [ 'body' ][ 1 ][ 'data' ][ 'localeListCountries' ][ 'total' ]);
2022-07-25 11:20:04 +12:00
}
public function testArrayBatchedMutations ()
{
$projectId = $this -> getProject ()[ '$id' ];
$email = 'tester' . \uniqid () . '@example.com' ;
$graphQLPayload = [[
'query' => ' mutation CreateAccount ( $userId : String ! , $email : String ! , $password : String ! , $name : String ) {
accountCreate ( userId : $userId , email : $email , password : $password , name : $name ) {
name
}
} ' ,
'variables' => [
2022-09-22 13:53:41 +12:00
'userId' => ID :: unique (),
2022-07-25 11:20:04 +12:00
'email' => $email ,
'password' => 'password' ,
'name' => 'Tester 1' ,
],
],
[
'query' => ' mutation CreateTeam ( $teamId : String ! $name : String ! ) {
teamsCreate ( teamId : $teamId , name : $name ) {
name
}
} ' ,
'variables' => [
2022-09-22 13:53:41 +12:00
'teamId' => ID :: unique (),
2022-07-25 11:20:04 +12:00
'name' => 'Team 1' ,
],
]];
$response = $this -> client -> call ( Client :: METHOD_POST , '/graphql' , \array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $projectId ,
], $this -> getHeaders ()), $graphQLPayload );
2022-10-14 14:33:42 +13:00
$this -> assertIsArray ( $response [ 'body' ][ 0 ][ 'data' ]);
$this -> assertIsArray ( $response [ 'body' ][ 1 ][ 'data' ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 0 ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 1 ]);
$this -> assertArrayHasKey ( 'accountCreate' , $response [ 'body' ][ 0 ][ 'data' ]);
$this -> assertArrayHasKey ( 'teamsCreate' , $response [ 'body' ][ 1 ][ 'data' ]);
$this -> assertEquals ( 'Tester 1' , $response [ 'body' ][ 0 ][ 'data' ][ 'accountCreate' ][ 'name' ]);
$this -> assertEquals ( 'Team 1' , $response [ 'body' ][ 1 ][ 'data' ][ 'teamsCreate' ][ 'name' ]);
2022-07-25 11:20:04 +12:00
}
public function testArrayBatchedMutationsOfSameType ()
{
$projectId = $this -> getProject ()[ '$id' ];
$email1 = 'tester' . \uniqid () . '@example.com' ;
$email2 = 'tester' . \uniqid () . '@example.com' ;
$query = ' mutation CreateAccount ( $userId : String ! , $email : String ! , $password : String ! , $name : String ) {
accountCreate ( userId : $userId , email : $email , password : $password , name : $name ) {
2022-12-08 16:08:57 +13:00
_id
2022-07-25 11:20:04 +12:00
}
} ' ;
$graphQLPayload = [
[
'query' => $query ,
'variables' => [
2022-09-22 13:53:41 +12:00
'userId' => ID :: unique (),
2022-07-25 11:20:04 +12:00
'email' => $email1 ,
'password' => 'password' ,
'name' => 'Tester 1' ,
],
],
[
'query' => $query ,
'variables' => [
2022-09-22 13:53:41 +12:00
'userId' => ID :: unique (),
2022-07-25 11:20:04 +12:00
'email' => $email2 ,
'password' => 'password' ,
'name' => 'Tester 2' ,
],
],
];
$response = $this -> client -> call ( Client :: METHOD_POST , '/graphql' , \array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $projectId ,
], $this -> getHeaders ()), $graphQLPayload );
2022-10-14 14:33:42 +13:00
$this -> assertIsArray ( $response [ 'body' ][ 0 ][ 'data' ]);
$this -> assertIsArray ( $response [ 'body' ][ 1 ][ 'data' ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 0 ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 1 ]);
$this -> assertArrayHasKey ( 'accountCreate' , $response [ 'body' ][ 0 ][ 'data' ]);
$this -> assertArrayHasKey ( 'accountCreate' , $response [ 'body' ][ 1 ][ 'data' ]);
2022-07-25 11:20:04 +12:00
}
2022-08-22 21:13:29 +12:00
public function testArrayBatchedMixed ()
{
2022-07-25 11:20:04 +12:00
$projectId = $this -> getProject ()[ '$id' ];
$email = 'tester' . \uniqid () . '@example.com' ;
$graphQLPayload = [
2022-09-21 19:03:28 +12:00
[ 'query' => 'query { localeListCountries { total countries { code } } }' ],
[ 'query' => 'query { localeListContinents { total continents { code } } }' ],
2022-07-25 11:20:04 +12:00
[
'query' => ' mutation CreateAccount ( $userId : String ! , $email : String ! , $password : String ! , $name : String ) {
accountCreate ( userId : $userId , email : $email , password : $password , name : $name ) {
name
}
} ' ,
'variables' => [
2022-09-22 13:53:41 +12:00
'userId' => ID :: unique (),
2022-07-25 11:20:04 +12:00
'email' => $email ,
'password' => 'password' ,
'name' => 'Tester 1' ,
]
],
];
$response = $this -> client -> call ( Client :: METHOD_POST , '/graphql' , \array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $projectId ,
], $this -> getHeaders ()), $graphQLPayload );
2022-10-14 14:33:42 +13:00
$this -> assertIsArray ( $response [ 'body' ][ 0 ][ 'data' ]);
$this -> assertIsArray ( $response [ 'body' ][ 1 ][ 'data' ]);
$this -> assertIsArray ( $response [ 'body' ][ 2 ][ 'data' ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 0 ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 1 ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 2 ]);
$this -> assertArrayHasKey ( 'localeListCountries' , $response [ 'body' ][ 0 ][ 'data' ]);
$this -> assertArrayHasKey ( 'localeListContinents' , $response [ 'body' ][ 1 ][ 'data' ]);
$this -> assertArrayHasKey ( 'accountCreate' , $response [ 'body' ][ 2 ][ 'data' ]);
$this -> assertEquals ( 194 , $response [ 'body' ][ 0 ][ 'data' ][ 'localeListCountries' ][ 'total' ]);
$this -> assertEquals ( 7 , $response [ 'body' ][ 1 ][ 'data' ][ 'localeListContinents' ][ 'total' ]);
$this -> assertEquals ( 'Tester 1' , $response [ 'body' ][ 2 ][ 'data' ][ 'accountCreate' ][ 'name' ]);
2022-07-25 11:20:04 +12:00
}
2022-08-22 21:13:29 +12:00
public function testArrayBatchedMixedOfSameType ()
{
2022-07-25 11:20:04 +12:00
$projectId = $this -> getProject ()[ '$id' ];
$email = 'tester' . \uniqid () . '@example.com' ;
2022-09-21 19:03:28 +12:00
$query = 'query { localeListCountries { total countries { code } } }' ;
2022-07-25 11:20:04 +12:00
$graphQLPayload = [
[ 'query' => $query ],
[ 'query' => $query ],
[
'query' => ' mutation CreateAccount ( $userId : String ! , $email : String ! , $password : String ! , $name : String ) {
accountCreate ( userId : $userId , email : $email , password : $password , name : $name ) {
2022-12-08 16:08:57 +13:00
_id
2022-07-25 11:20:04 +12:00
}
} ' ,
'variables' => [
2022-09-22 13:53:41 +12:00
'userId' => ID :: unique (),
2022-07-25 11:20:04 +12:00
'email' => $email ,
'password' => 'password' ,
'name' => 'Tester 1' ,
]
],
];
$response = $this -> client -> call ( Client :: METHOD_POST , '/graphql' , \array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $projectId ,
], $this -> getHeaders ()), $graphQLPayload );
2022-10-14 14:33:42 +13:00
$this -> assertIsArray ( $response [ 'body' ][ 0 ][ 'data' ]);
$this -> assertIsArray ( $response [ 'body' ][ 1 ][ 'data' ]);
$this -> assertIsArray ( $response [ 'body' ][ 2 ][ 'data' ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 0 ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 1 ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ][ 2 ]);
$this -> assertArrayHasKey ( 'localeListCountries' , $response [ 'body' ][ 0 ][ 'data' ]);
$this -> assertArrayHasKey ( 'localeListCountries' , $response [ 'body' ][ 1 ][ 'data' ]);
$this -> assertArrayHasKey ( 'accountCreate' , $response [ 'body' ][ 2 ][ 'data' ]);
$this -> assertEquals ( 194 , $response [ 'body' ][ 0 ][ 'data' ][ 'localeListCountries' ][ 'total' ]);
$this -> assertEquals ( 194 , $response [ 'body' ][ 1 ][ 'data' ][ 'localeListCountries' ][ 'total' ]);
2022-12-08 16:08:57 +13:00
$this -> assertArrayHasKey ( '_id' , $response [ 'body' ][ 2 ][ 'data' ][ 'accountCreate' ]);
2022-07-25 11:20:04 +12:00
}
public function testQueryBatchedQueries ()
{
$projectId = $this -> getProject ()[ '$id' ];
$query = '
query {
2022-09-21 19:03:28 +12:00
localeListCountries { total countries { code } }
localeListContinents { total continents { code } }
2022-07-25 11:20:04 +12:00
}
' ;
$graphQLPayload = [
[ 'query' => $query ],
];
$response = $this -> client -> call ( Client :: METHOD_POST , '/graphql' , \array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $projectId ,
], $this -> getHeaders ()), $graphQLPayload );
$this -> assertIsArray ( $response [ 'body' ][ 'data' ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ]);
2022-09-21 19:03:28 +12:00
$this -> assertArrayHasKey ( 'localeListCountries' , $response [ 'body' ][ 'data' ]);
$this -> assertArrayHasKey ( 'localeListContinents' , $response [ 'body' ][ 'data' ]);
$this -> assertEquals ( 194 , $response [ 'body' ][ 'data' ][ 'localeListCountries' ][ 'total' ]);
$this -> assertEquals ( 7 , $response [ 'body' ][ 'data' ][ 'localeListContinents' ][ 'total' ]);
2022-07-25 11:20:04 +12:00
}
public function testQueryBatchedQueriesOfSameType ()
{
$projectId = $this -> getProject ()[ '$id' ];
$query = '
query {
2022-09-21 19:03:28 +12:00
localeListCountries { total countries { code } }
localeListCountries { total countries { code } }
2022-07-25 11:20:04 +12:00
}
' ;
$graphQLPayload = [
[ 'query' => $query ],
];
$response = $this -> client -> call ( Client :: METHOD_POST , '/graphql' , \array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $projectId ,
], $this -> getHeaders ()), $graphQLPayload );
$this -> assertIsArray ( $response [ 'body' ][ 'data' ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ]);
2022-09-21 19:03:28 +12:00
$this -> assertArrayHasKey ( 'localeListCountries' , $response [ 'body' ][ 'data' ]);
$this -> assertEquals ( 194 , $response [ 'body' ][ 'data' ][ 'localeListCountries' ][ 'total' ]);
2022-07-25 11:20:04 +12:00
}
public function testQueryBatchedMutations ()
{
$projectId = $this -> getProject ()[ '$id' ];
$email = 'tester' . \uniqid () . '@example.com' ;
$graphQLPayload = [
'query' => ' mutation CreateAndLogin ( $userId : String ! , $email : String ! , $password : String ! , $name : String ) {
accountCreate ( userId : $userId , email : $email , password : $password , name : $name ) {
name
}
2024-02-08 00:08:05 +13:00
accountCreateEmailPasswordSession ( email : $email , password : $password ) {
2022-07-25 11:20:04 +12:00
expire
}
} ' ,
'variables' => [
2022-09-22 13:53:41 +12:00
'userId' => ID :: unique (),
2022-07-25 11:20:04 +12:00
'email' => $email ,
'password' => 'password' ,
'name' => 'Tester' ,
],
];
$response = $this -> client -> call ( Client :: METHOD_POST , '/graphql' , \array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $projectId ,
], $this -> getHeaders ()), $graphQLPayload );
$this -> assertIsArray ( $response [ 'body' ][ 'data' ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ]);
$this -> assertArrayHasKey ( 'accountCreate' , $response [ 'body' ][ 'data' ]);
2024-02-08 00:08:05 +13:00
$this -> assertArrayHasKey ( 'accountCreateEmailPasswordSession' , $response [ 'body' ][ 'data' ]);
2022-07-25 11:20:04 +12:00
$this -> assertEquals ( 'Tester' , $response [ 'body' ][ 'data' ][ 'accountCreate' ][ 'name' ]);
}
public function testQueryBatchedMutationsOfSameType ()
{
$projectId = $this -> getProject ()[ '$id' ];
$email1 = 'tester' . \uniqid () . '@example.com' ;
$email2 = 'tester' . \uniqid () . '@example.com' ;
$graphQLPayload = [
'query' => ' mutation CreateAndLogin ( $email1 : String ! , $email2 : String ! , $password : String ! , $name1 : String , $name2 : String ) {
accountCreate ( userId : " unique() " , email : $email1 , password : $password , name : $name1 ) {
name
}
accountCreate ( userId : " unique() " , email : $email2 , password : $password , name : $name2 ) {
name
}
} ' ,
'variables' => [
2022-09-22 13:53:41 +12:00
'userId' => ID :: unique (),
2022-07-25 11:20:04 +12:00
'email1' => $email1 ,
'email2' => $email2 ,
'name1' => 'Tester 1' ,
'name2' => 'Tester 2' ,
'password' => 'password' ,
],
];
$response = $this -> client -> call ( Client :: METHOD_POST , '/graphql' , \array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $projectId ,
], $this -> getHeaders ()), $graphQLPayload );
$this -> assertEquals ( 'Fields "accountCreate" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.' , $response [ 'body' ][ 'errors' ][ 0 ][ 'message' ]);
}
public function testQueryBatchedMutationsOfSameTypeWithAlias ()
{
$projectId = $this -> getProject ()[ '$id' ];
$email1 = 'tester' . \uniqid () . '@example.com' ;
$email2 = 'tester' . \uniqid () . '@example.com' ;
$graphQLPayload = [
'query' => ' mutation CreateAndLogin ( $email1 : String ! , $email2 : String ! , $password : String ! , $name1 : String , $name2 : String ) {
account1 : accountCreate ( userId : " unique() " , email : $email1 , password : $password , name : $name1 ) {
name
}
account2 : accountCreate ( userId : " unique() " , email : $email2 , password : $password , name : $name2 ) {
name
}
} ' ,
'variables' => [
2022-09-22 13:53:41 +12:00
'userId' => ID :: unique (),
2022-07-25 11:20:04 +12:00
'email1' => $email1 ,
'email2' => $email2 ,
'name1' => 'Tester 1' ,
'name2' => 'Tester 2' ,
'password' => 'password' ,
],
];
$response = $this -> client -> call ( Client :: METHOD_POST , '/graphql' , \array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $projectId ,
], $this -> getHeaders ()), $graphQLPayload );
$this -> assertIsArray ( $response [ 'body' ][ 'data' ]);
$this -> assertArrayNotHasKey ( 'errors' , $response [ 'body' ]);
$this -> assertArrayHasKey ( 'account1' , $response [ 'body' ][ 'data' ]);
$this -> assertArrayHasKey ( 'account2' , $response [ 'body' ][ 'data' ]);
$this -> assertEquals ( 'Tester 1' , $response [ 'body' ][ 'data' ][ 'account1' ][ 'name' ]);
$this -> assertEquals ( 'Tester 2' , $response [ 'body' ][ 'data' ][ 'account2' ][ 'name' ]);
}
2022-08-22 21:13:29 +12:00
}