Skip to content

Commit e053540

Browse files
committed
Add quick graphql pages generator
1 parent f4931c6 commit e053540

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

ProcessGraphQLConfig.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
class ProcessGraphQLConfig extends Moduleconfig {
1111

12+
const generatorName = 'graphql_generate_pages';
13+
1214
public function getDefaults()
1315
{
1416
return array(
@@ -120,6 +122,74 @@ public static function isLegalTemplateName($name)
120122
return true;
121123
}
122124

125+
public function generatePages()
126+
{
127+
128+
$pageNames = [
129+
'graphql' => 'GraphQL',
130+
'graphiql' => 'GraphiQL',
131+
];
132+
133+
// copy the template files to site/templates folder
134+
try {
135+
$from = $this->config->paths->ProcessGraphQL . 'templates/';
136+
$to = $this->config->paths->templates;
137+
$this->files->copy($from, $to);
138+
$this->message(sprintf($this->_('Created template files: %s'), 'graphql.php & graphiql.php'));
139+
} catch(\Exception $e) {
140+
$this->error($e->getMessage());
141+
}
142+
143+
foreach($pageNames as $name => $title) {
144+
145+
// create the templates
146+
try {
147+
$template = $this->wire(new Template());
148+
$template->name = $name;
149+
$fieldgroup = $this->wire(new Fieldgroup());
150+
$fieldgroup->name = $template->name;
151+
if ($this->fields->get('title') instanceof Field) $fieldgroup->add('title');
152+
$fieldgroup->save();
153+
$template->fieldgroup = $fieldgroup;
154+
$template->roles = array($this->roles->getGuestRole()->id);
155+
$template->noAppendTemplateFile = true;
156+
$template->noPrependTemplateFile = true;
157+
$template->save();
158+
$this->message(sprintf($this->_('Added template and fieldgroup: %s'), $name));
159+
} catch(\Exception $e) {
160+
$this->error($e->getMessage());
161+
continue;
162+
}
163+
164+
// create the pages
165+
try {
166+
$p = $this->wire(new Page());
167+
$p->template = $this->templates->get($name);
168+
$p->name = $name;
169+
$p->parent = $this->pages->get(1); // root page
170+
$p->title = $title;
171+
$p->save();
172+
$this->message(sprintf($this->_('Created page: %s'), $name));
173+
} catch(\Exception $e) {
174+
$this->error($e->getMessage());
175+
continue;
176+
}
177+
}
178+
}
179+
180+
public function readyToGeneratePages()
181+
{
182+
$generatorName = self::generatorName;
183+
if ($this->input->post->$generatorName) $this->generatePages();
184+
if (is_file($this->config->paths->templates . 'graphql.php')) return false;
185+
if (is_file($this->config->paths->templates . 'graphiql.php')) return false;
186+
if ($this->templates->get('graphql') instanceof Template) return false;
187+
if ($this->templates->get('graphiql') instanceof Template) return false;
188+
if (!$this->pages->get('/graphql/') instanceof NullPage) return false;
189+
if (!$this->pages->get('/graphiql/') instanceof NullPage) return false;
190+
return true;
191+
}
192+
123193
/**
124194
* Build module configuration.
125195
* @return InputFields ProcessWire Inputfields form.
@@ -219,6 +289,23 @@ public function getInputFields()
219289
}
220290
$inputfields->add($f);
221291

292+
// GRAPHQL PAGES GENERATOR
293+
if ($this->readyToGeneratePages()) {
294+
$fSet = $this->modules->get('InputfieldFieldset');
295+
$fSet->label = 'GraphQL Pages Generator';
296+
$fSet->collapsed = Inputfield::collapsedYes;
297+
298+
// graphql template name
299+
$f = $this->modules->get('InputfieldSubmit');
300+
$f->label = 'Generate';
301+
$f->description = 'Generates the graphql and graphiql templates, template files and pages.';
302+
$f->attr('name', self::generatorName);
303+
$f->attr('value', 'Generate');
304+
$fSet->add($f);
305+
306+
$inputfields->add($fSet);
307+
}
308+
222309
// ADVANCED
223310
$fSet = $this->modules->get('InputfieldFieldset');
224311
$fSet->label = 'Advanced';

templates/graphiql.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php namespace ProcessWire;
2+
3+
$ProcessGraphQL = $modules->get('ProcessGraphQL');
4+
$ProcessGraphQL->GraphQLServerUrl = '/graphql/';
5+
echo $ProcessGraphQL->executeGraphiQL();

templates/graphql.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php namespace ProcessWire;
2+
3+
echo $modules->get('ProcessGraphQL')->executeGraphQL();

0 commit comments

Comments
 (0)