Skip to content

Commit cbcbe54

Browse files
committed
Add GraphQL lang
1 parent 0123241 commit cbcbe54

File tree

2 files changed

+215
-0
lines changed

2 files changed

+215
-0
lines changed

example.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
include_once 'vendor/autoload.php';
44

5+
use Appwrite\SDK\Language\GraphQL;
56
use Appwrite\Spec\Swagger2;
67
use Appwrite\SDK\SDK;
78
use Appwrite\SDK\Language\Web;
@@ -457,6 +458,16 @@ function getSSLPage($url) {
457458
])
458459
;
459460
$sdk->generate(__DIR__ . '/examples/kotlin');
461+
462+
// GraphQL
463+
$sdk = new SDK(new GraphQL(), new Swagger2($spec));
464+
465+
$sdk
466+
->setName('GraphQL')
467+
->setDescription('Appwrite is an open-source backend as a service server that abstract and simplify complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way. Use the Flutter SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to https://appwrite.io/docs')
468+
->setLogo('https://appwrite.io/v1/images/console.png')
469+
;
470+
$sdk->generate(__DIR__ . '/examples/graphql');
460471
}
461472
catch (Exception $exception) {
462473
echo 'Error: ' . $exception->getMessage() . ' on ' . $exception->getFile() . ':' . $exception->getLine() . "\n";

src/SDK/Language/GraphQL.php

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<?php
2+
3+
namespace Appwrite\SDK\Language;
4+
5+
use Appwrite\SDK\Language;
6+
7+
class GraphQL extends Language {
8+
9+
/**
10+
* @return string
11+
*/
12+
public function getName(): string
13+
{
14+
return 'GraphQL';
15+
}
16+
17+
/**
18+
* Get Language Keywords List
19+
*
20+
* @return array
21+
*/
22+
public function getKeywords(): array
23+
{
24+
return [];
25+
}
26+
27+
/**
28+
* @return array
29+
*/
30+
public function getIdentifierOverrides(): array
31+
{
32+
return [];
33+
}
34+
35+
/**
36+
* @param $type
37+
* @return string
38+
*/
39+
public function getTypeName(array $parameter): string
40+
{
41+
$type = '';
42+
43+
switch ($parameter['type']) {
44+
case self::TYPE_INTEGER:
45+
$type = 'Int';
46+
break;
47+
case self::TYPE_STRING:
48+
$type = 'String';
49+
break;
50+
case self::TYPE_FILE:
51+
$type = 'InputFile';
52+
break;
53+
case self::TYPE_BOOLEAN:
54+
$type = 'Bool';
55+
break;
56+
case self::TYPE_ARRAY:
57+
if (!empty($parameter['array']['type'])) {
58+
$type = '[' . $this->getTypeName($parameter['array']) . ']';
59+
break;
60+
}
61+
$type = '[String]';
62+
break;
63+
case self::TYPE_OBJECT:
64+
$type = 'JSON';
65+
break;
66+
}
67+
68+
if (empty($type)) {
69+
$type = $parameter['type'];
70+
}
71+
72+
if ($parameter['required'] ?? false) {
73+
$type .= '!';
74+
}
75+
76+
return $type;
77+
}
78+
79+
/**
80+
* @param array $param
81+
* @return string
82+
*/
83+
public function getParamDefault(array $param): string
84+
{
85+
$type = $param['type'] ?? '';
86+
$default = $param['default'] ?? '';
87+
$required = $param['required'] ?? '';
88+
89+
if($required) {
90+
return '';
91+
}
92+
93+
$output = '';
94+
95+
if(empty($default) && $default !== 0 && $default !== false) {
96+
switch ($type) {
97+
case self::TYPE_OBJECT:
98+
$output .= '{}';
99+
break;
100+
case self::TYPE_NUMBER:
101+
case self::TYPE_INTEGER:
102+
$output .= '0';
103+
break;
104+
case self::TYPE_BOOLEAN:
105+
$output .= 'false';
106+
break;
107+
case self::TYPE_ARRAY:
108+
$output .= '[]';
109+
break;
110+
case self::TYPE_STRING:
111+
$output .= '""';
112+
break;
113+
}
114+
}
115+
else {
116+
switch ($type) {
117+
case self::TYPE_OBJECT:
118+
case self::TYPE_NUMBER:
119+
case self::TYPE_ARRAY:
120+
case self::TYPE_INTEGER:
121+
$output .= $default;
122+
break;
123+
case self::TYPE_BOOLEAN:
124+
$output .= ($default) ? 'true' : 'false';
125+
break;
126+
case self::TYPE_STRING:
127+
$output .= '"'.$default .'"';
128+
break;
129+
}
130+
}
131+
132+
return $output;
133+
}
134+
135+
/**
136+
* @param array $param
137+
* @return string
138+
*/
139+
public function getParamExample(array $param): string
140+
{
141+
$type = $param['type'] ?? '';
142+
$example = $param['example'] ?? '';
143+
144+
$output = '';
145+
146+
if(empty($example) && $example !== 0 && $example !== false) {
147+
switch ($type) {
148+
case self::TYPE_FILE:
149+
$output .= 'null';
150+
break;
151+
case self::TYPE_NUMBER:
152+
case self::TYPE_INTEGER:
153+
$output .= '0';
154+
break;
155+
case self::TYPE_BOOLEAN:
156+
$output .= 'false';
157+
break;
158+
case self::TYPE_STRING:
159+
$output .= "";
160+
break;
161+
case self::TYPE_OBJECT:
162+
$output .= '{}';
163+
break;
164+
case self::TYPE_ARRAY:
165+
$output .= '[]';
166+
break;
167+
}
168+
}
169+
else {
170+
switch ($type) {
171+
case self::TYPE_FILE:
172+
case self::TYPE_NUMBER:
173+
case self::TYPE_INTEGER:
174+
case self::TYPE_ARRAY:
175+
$output .= $example;
176+
break;
177+
case self::TYPE_STRING:
178+
case self::TYPE_OBJECT:
179+
$output .= '"' . $example . '"';
180+
break;
181+
case self::TYPE_BOOLEAN:
182+
$output .= ($example) ? 'true' : 'false';
183+
break;
184+
}
185+
}
186+
187+
return $output;
188+
}
189+
190+
/**
191+
* @return array
192+
*/
193+
public function getFiles(): array
194+
{
195+
return [
196+
[
197+
'scope' => 'method',
198+
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}',
199+
'template' => '/graphql/docs/example.md.twig',
200+
],
201+
];
202+
}
203+
}
204+

0 commit comments

Comments
 (0)