Skip to content

Commit 95deb76

Browse files
committed
Disable most of the built in pagefile fields and add option to enable them manually.
1 parent 01ce7cc commit 95deb76

File tree

4 files changed

+130
-92
lines changed

4 files changed

+130
-92
lines changed

ProcessGraphQLConfig.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace ProcessWire;
22

33
use \ProcessWire\GraphQL\Type\InterfaceType\PageInterfaceType;
4+
use \ProcessWire\GraphQL\Type\InterfaceType\PageFileInterfaceType;
45

56
require_once $this->config->paths->site . 'modules/ProcessGraphQL/vendor/autoload.php';
67

@@ -9,7 +10,7 @@ class ProcessGraphQLConfig extends Moduleconfig {
910
public function getDefaults()
1011
{
1112
return array(
12-
'maxLimit' => 100,
13+
'maxLimit' => 50,
1314
'debug' => false,
1415
'legalTemplates' => [],
1516
'legalFields' => [],
@@ -21,6 +22,11 @@ public function getDefaults()
2122
'name',
2223
'httpUrl',
2324
],
25+
'legalPageFileFields' => [
26+
'url',
27+
'httpUrl',
28+
'description',
29+
],
2430
'fullWidthGraphiql' => false,
2531
);
2632
}
@@ -35,15 +41,15 @@ public function getInputFields()
3541
$f->label = 'Max Limit';
3642
$f->description = 'Set the maximum value for `limit` selector field.';
3743
$f->required = true;
38-
$f->columnWidth = 35;
44+
$f->columnWidth = 50;
3945
$inputfields->add($f);
4046

4147
// GraphiQL full width
4248
$f = $this->modules->get('InputfieldCheckbox');
4349
$f->attr('name', 'fullWidthGraphiQL');
4450
$f->label = 'Full width GraphiQL';
4551
$f->description = 'Check this if you want GraphiQL on the backend to stretch to full width.';
46-
$f->columnWidth = 30;
52+
$f->columnWidth = 50;
4753
$inputfields->add($f);
4854

4955
// legalTemplates
@@ -78,13 +84,24 @@ public function getInputFields()
7884
$f->optionColumns = 4;
7985
$f->attr('name', 'legalPageFields');
8086
$f->label = 'Legal Page Fields';
81-
$f->description = 'Choose which built in page fields you wish to be available via GraphQL api.';
87+
$f->description = 'Choose which built in `Page` fields you wish to be available via GraphQL api.';
8288
$f->notes = 'Be careful with fields like `parents` & `children` that will allow user to construct deeply nested queries that might be very expensive for your server to fulfill.';
8389
foreach (PageInterfaceType::getPageFields() as $fieldName => $fieldClassName) {
8490
$f->addOption($fieldName);
8591
}
8692
$inputfields->add($f);
8793

94+
// legalPageFileFields
95+
$f = $this->modules->get('InputfieldCheckboxes');
96+
$f->optionColumns = 4;
97+
$f->attr('name', 'legalPageFileFields');
98+
$f->label = 'Legal PageFile Fields';
99+
$f->description = 'Choose which built in `PageFile` fields you wish to be available via GraphQL api.';
100+
foreach (PageFileInterfaceType::getPageFileFields() as $fieldName => $fieldClassName) {
101+
$f->addOption($fieldName);
102+
}
103+
$inputfields->add($f);
104+
88105
return $inputfields;
89106
}
90107

src/Settings.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ public static function getLegalPageFields()
4444
return self::module()->legalPageFields;
4545
}
4646

47+
public static function getLegalPageFileFields()
48+
{
49+
return self::module()->legalPageFileFields;
50+
}
51+
4752
}

src/Type/InterfaceType/PageFileInterfaceType.php

Lines changed: 102 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use ProcessWire\Pageimage;
99
use ProcessWire\GraphQL\Type\Object\PageFileType;
1010
use ProcessWire\GraphQL\Type\Object\PageImageType;
11+
use ProcessWire\GraphQL\Settings;
1112

1213
class PageFileInterfaceType extends AbstractInterfaceType {
1314

@@ -23,94 +24,12 @@ public function getDescription()
2324

2425
public function build($config)
2526
{
26-
$config->addField('url', [
27-
'type' => new StringType(),
28-
'description' => 'URL to the file on the server.',
29-
'resolve' => function ($value) {
30-
return (string) $value->url;
31-
}
32-
]);
33-
34-
$config->addField('httpUrl', [
35-
'type' => new StringType(),
36-
'description' => 'The web accessible URL (with scheme and hostname) to this Pagefile.',
37-
'resolve' => function ($value) {
38-
return (string) $value->httpUrl();
39-
}
40-
]);
41-
42-
$config->addField('URL', [
43-
'type' => new StringType(),
44-
'description' => "Same as 'url' property but with browser cache busting query
45-
string appended that represents the file's modification time.",
46-
'resolve' => function ($value) {
47-
return (string) $value->URL;
48-
}
49-
]);
50-
51-
$config->addField('basename', [
52-
'type' => new StringType(),
53-
'description' => 'The filename without the path.',
54-
'resolve' => function ($value) {
55-
return (string) $value->basename;
56-
}
57-
]);
58-
59-
$config->addField('description', [
60-
'type' => new StringType(),
61-
'description' => 'The description of the file.',
62-
'resolve' => function ($value) {
63-
return (string) $value->description;
64-
}
65-
]);
66-
67-
$config->addField('ext', [
68-
'type' => new StringType(),
69-
'description' => "File’s extension.",
70-
'resolve' => function ($value) {
71-
return (string) $value->ext;
72-
}
73-
]);
74-
75-
$config->addField('filesize', [
76-
'type' => new IntType(),
77-
'description' => 'File size (number of bytes).',
78-
'resolve' => function ($value) {
79-
return (integer) $value->filesize;
80-
}
81-
]);
82-
83-
$config->addField('modified', [
84-
'type' => new IntType(),
85-
'description' => 'Unix timestamp of when Pagefile (file, description or tags) was last modified.',
86-
'resolve' => function ($value) {
87-
return (integer) $value->modified;
88-
}
89-
]);
90-
91-
$config->addField('mtime', [
92-
'type' => new IntType(),
93-
'description' => 'Unix timestamp of when file (only) was last modified.',
94-
'resolve' => function ($value) {
95-
return (integer) $value->mtime;
96-
}
97-
]);
98-
99-
$config->addField('created', [
100-
'type' => new IntType(),
101-
'description' => 'Unix timestamp of when file was created.',
102-
'resolve' => function ($value) {
103-
return (integer) $value->created;
104-
}
105-
]);
106-
107-
$config->addField('filesizeStr', [
108-
'type' => new StringType(),
109-
'description' => 'File size as a formatted string, i.e. “123 Kb”.',
110-
'resolve' => function ($value) {
111-
return (string) $value->filesizeStr;
112-
}
113-
]);
27+
$fields = self::getPageFileFields();
28+
$legalPageFileFields = Settings::getLegalPageFileFields();
29+
foreach ($fields as $fieldName => $fieldConfig) {
30+
if (!in_array($fieldName, $legalPageFileFields)) continue;
31+
$config->addField($fieldName, $fieldConfig);
32+
}
11433
}
11534

11635
public function resolveType($opject)
@@ -119,4 +38,99 @@ public function resolveType($opject)
11938
return new PageFileType();
12039
}
12140

41+
public static function getPageFileFields()
42+
{
43+
return [
44+
'url' => [
45+
'type' => new StringType(),
46+
'description' => 'URL to the file on the server.',
47+
'resolve' => function ($value) {
48+
return (string) $value->url;
49+
}
50+
],
51+
52+
'httpUrl' => [
53+
'type' => new StringType(),
54+
'description' => 'The web accessible URL (with scheme and hostname) to this Pagefile.',
55+
'resolve' => function ($value) {
56+
return (string) $value->httpUrl();
57+
}
58+
],
59+
60+
'URL' => [
61+
'type' => new StringType(),
62+
'description' => "Same as 'url' property but with browser cache busting query
63+
string appended that represents the file's modification time.",
64+
'resolve' => function ($value) {
65+
return (string) $value->URL;
66+
}
67+
],
68+
69+
'basename' => [
70+
'type' => new StringType(),
71+
'description' => 'The filename without the path.',
72+
'resolve' => function ($value) {
73+
return (string) $value->basename;
74+
}
75+
],
76+
77+
'description' => [
78+
'type' => new StringType(),
79+
'description' => 'The description of the file.',
80+
'resolve' => function ($value) {
81+
return (string) $value->description;
82+
}
83+
],
84+
85+
'ext' => [
86+
'type' => new StringType(),
87+
'description' => "File’s extension.",
88+
'resolve' => function ($value) {
89+
return (string) $value->ext;
90+
}
91+
],
92+
93+
'filesize' => [
94+
'type' => new IntType(),
95+
'description' => 'File size (number of bytes).',
96+
'resolve' => function ($value) {
97+
return (integer) $value->filesize;
98+
}
99+
],
100+
101+
'modified' => [
102+
'type' => new IntType(),
103+
'description' => 'Unix timestamp of when Pagefile (file, description or tags) was last modified.',
104+
'resolve' => function ($value) {
105+
return (integer) $value->modified;
106+
}
107+
],
108+
109+
'mtime' => [
110+
'type' => new IntType(),
111+
'description' => 'Unix timestamp of when file (only) was last modified.',
112+
'resolve' => function ($value) {
113+
return (integer) $value->mtime;
114+
}
115+
],
116+
117+
'created' => [
118+
'type' => new IntType(),
119+
'description' => 'Unix timestamp of when file was created.',
120+
'resolve' => function ($value) {
121+
return (integer) $value->created;
122+
}
123+
],
124+
125+
'filesizeStr' => [
126+
'type' => new StringType(),
127+
'description' => 'File size as a formatted string, i.e. “123 Kb”.',
128+
'resolve' => function ($value) {
129+
return (string) $value->filesizeStr;
130+
}
131+
],
132+
];
133+
}
134+
135+
122136
}

src/Type/Object/PageImageType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ public function build($config)
2626

2727
$config->addfield('width', [
2828
'type' => new IntType(),
29+
'description' => 'The width of the image.',
2930
'resolve' => function ($value) {
3031
return (integer) $value->width;
3132
}
3233
]);
3334

3435
$config->addfield('height', [
3536
'type' => new IntType(),
37+
'description' => 'The height of the image.',
3638
'resolve' => function ($value) {
3739
return (integer) $value->height;
3840
}

0 commit comments

Comments
 (0)