Skip to content

Commit

Permalink
Update readme with new scaffolding api
Browse files Browse the repository at this point in the history
  • Loading branch information
Uncle Cheese committed Dec 23, 2016
1 parent ff24a73 commit ea08f67
Show file tree
Hide file tree
Showing 10 changed files with 1,561 additions and 901 deletions.
656 changes: 525 additions & 131 deletions README.md

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions examples/_config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,46 @@ SilverStripe\GraphQL:
readMembers: 'MyProject\GraphQL\ReadMembersQueryCreator'
mutations:
createMember: 'MyProject\GraphQL\CreateMemberMutationCreator'
scaffolding_providers:
- My\Project\Post
scaffolding:
types:
My\Project\Post:
fields: [ID, Title, Content, Author, Date]
nestedQueries:
Comments:
args:
Today: Boolean
sortableFields: [Author]
resolver: My\Project\CommentsResolver
Files: true
operations:
create: true
read:
args:
StartingWith: String
resolver: My\Project\ReadResolver
SilverStripe\Security\Member:
fields: [Name, FirstName, Surname, Email]
SilverStripe\Assets\File:
fieldsExcept: [Content]
fields: [File]
My\Project\Comment:
fields: [Comment, Author]
SilverStripe\CMS\Model\RedirectorPage:
fields: [ExternalURL, Content]
operations:
read: true
create: true
mutations:
updatePostTitle:
type: My\Project\Post
args:
ID: ID!
NewTitle: String!
resolver: My\Project\UpdatePostResolver
queries:
latestPost:
type: My\Project\Post
paginate: false
resolver: My\Project\LatestPostResolver
23 changes: 23 additions & 0 deletions examples/code/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace MyProject\GraphQL;

use SilverStripe\ORM\DataObject;

class Comment extends DataObject
{
private static $db = [
'Comment' => 'Text',
'Author' => 'Varchar'
];

private static $has_one = [
'Post' => 'MyProject\GraphQL\Post'
];

public function canView($member = null, $context = []) { return true; }
public function canEdit($member = null, $context = []) { return true; }
public function canCreate($member = null, $context = []) { return true; }
public function canDelete($member = null, $context = []) { return true; }

}
20 changes: 20 additions & 0 deletions examples/code/CommentsResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace MyProject\GraphQL;

use SilverStripe\GraphQL\Scaffolding\Interfaces\ResolverInterface;

class CommentsResolver implements ResolverInterface
{
public function resolve($object, $args, $context, $info)
{
$comments = $object->Comments();

if(isset($args['Today']) && $args['Today']) {
$comments = $comments->where('DATE(Created) = DATE(NOW())');
}

return $comments;
}

}
13 changes: 13 additions & 0 deletions examples/code/LatestPostResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace MyProject\GraphQL;

use SilverStripe\GraphQL\Scaffolding\Interfaces\ResolverInterface;

class LatestPostResolver implements ResolverInterface
{
public function resolve($object, $args, $context, $info)
{
return Post::get()->sort('Date', 'DESC')->first();
}
}
131 changes: 131 additions & 0 deletions examples/code/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace MyProject\GraphQL;

use SilverStripe\ORM\DataObject;
use SilverStripe\GraphQL\Scaffolding\Interfaces\ScaffoldingProvider;
use SilverStripe\GraphQL\Scaffolding\Scaffolders\GraphQLScaffolder;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Security\Member;
use SilverStripe\Assets\File;

class Post extends DataObject implements ScaffoldingProvider {

private static $db = [
'Title' => 'Varchar',
'Content' => 'HTMLText',
'Date' => 'SilverStripe\ORM\FieldType\DBDateTime'
];

private static $has_one = [
'Author' => 'SilverStripe\Security\Member'
];

private static $many_many = [
'Files' => 'SilverStripe\Assets\File'
];

private static $has_many = [
'Comments' => 'MyProject\GraphQL\Comment'
];

public function provideGraphQLScaffolding(GraphQLScaffolder $scaffolder)
{
$scaffolder
->type(Post::class)
->addFields(['ID','Title','Content', 'Author', 'Date'])
// basic many_many nested query, no options
->nestedQuery('Files')
->end()
// more complex nested query
->nestedQuery('Comments')
->addArgs([
'Today' => 'Boolean'
])
->addSortableFields(['Author'])
->setResolver(function($obj, $args, $context) {
$comments = $obj->Comments();
if(isset($args['Today']) && $args['Today']) {
$comments = $comments->where('DATE(Created) = DATE(NOW())');
}

return $comments;
})
->end()
// basic crud operation, no options
->operation(GraphQLScaffolder::CREATE)
->end()

// complex crud operation, with custom args
->operation(GraphQLScaffolder::READ)
->addArgs([
'StartingWith' => 'String'
])
->setResolver(function($obj, $args) {
$list = Post::get();
if(isset($args['StartingWith'])) {
$list = $list->filter('Title:StartsWith', $args['StartingWith']);
}

return $list;
})
->end()
->end()

// these types were all created implicitly above. Add some fields to them.
->type(Member::class)
->addFields(['Name','FirstName', 'Surname', 'Email'])
->end()
->type(File::class)
->addAllFieldsExcept(['Content'])
->addFields(['File'])
->end()
->type(Comment::class)
->addFields(['Comment','Author'])
->end()

// Arbitrary mutation
->mutation('updatePostTitle', Post::class)
->addArgs([
'ID' => 'ID!',
'NewTitle' => 'String!'
])
->setResolver(function($obj, $args) {
$post = Post::get()->byID($args['ID']);
if($post->canEdit()) {
$post->Title = $args['NewTitle'];
$post->write();
}

return $post;
})
->end()

// Arbitrary query
->query('latestPost', Post::class)
->setUsePagination(false)
->setResolver(function($obj, $args) {
return Post::get()->sort('Date', 'DESC')->first();
})
->end()
->type('SilverStripe\CMS\Model\RedirectorPage')
->addFields(['ExternalURL','Content'])
->operation(GraphQLScaffolder::READ)
->end()
->operation(GraphQLScaffolder::CREATE)
->end()
->end()
->type('Page')
->addFields(['BackwardsTitle'])
->end();



return $scaffolder;
}

public function canView($member = null, $context = []) { return true; }
public function canEdit($member = null, $context = []) { return true; }
public function canCreate($member = null, $context = []) { return true; }
public function canDelete($member = null, $context = []) { return true; }
}
20 changes: 20 additions & 0 deletions examples/code/ReadResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace MyProject\GraphQL;

use SilverStripe\GraphQL\Scaffolding\Interfaces\ResolverInterface;

class ReadResolver implements ResolverInterface
{
public function resolve($object, $args, $context, $info)
{
$list = Post::get();

if(isset($args['StartingWith'])) {
$list = $list->filter('Title:StartsWith', $args['StartingWith']);
}

return $list;
}

}
21 changes: 21 additions & 0 deletions examples/code/UpdatePostResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace MyProject\GraphQL;

use SilverStripe\GraphQL\Scaffolding\Interfaces\ResolverInterface;

class UpdatePostResolver implements ResolverInterface
{
public function resolve($object, $args, $context, $info)
{
$post = Post::get()->byID($args['ID']);

if($post->canEdit()) {
$post->Title = $args['NewTitle'];
$post->write();
}

return $post;
}

}
25 changes: 10 additions & 15 deletions src/Pagination/SortInputType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,14 @@ class SortInputType
*/
private $inputName;


/**
* @var array Keyed by field argument name, values as DataObject column names.
* Does not support in-memory sorting for composite values (getters).
* Does not support in-memory sorting for composite values (getters)
*/
protected $sortableFields = [];

/**
*
*/
public function __construct($name)
{
parent::__construct();
$this->inputName = $name;
}

Expand All @@ -59,30 +54,30 @@ public function toType()

foreach ($this->sortableFields as $fieldAlias => $fieldName) {
$values[$fieldAlias] = [
'value' => $fieldAlias
'value' => $fieldAlias,
];
}

$sortableField = new EnumType([
'name' => ucfirst($this->inputName) . 'SortFieldType',
$sortableField = new EnumType([
'name' => ucfirst($this->inputName).'SortFieldType',
'description' => 'Field name to sort by.',
'values' => $values
'values' => $values,
]);

if (!$this->type) {
$this->type = new InputObjectType([
'name' => ucfirst($this->inputName) .'SortInputType',
'name' => ucfirst($this->inputName).'SortInputType',
'description' => 'Define the sorting',
'fields' => [
'field' => [
'type' => Type::nonNull($sortableField),
'description' => 'Sort field name.'
'description' => 'Sort field name.',
],
'direction' => [
'type' => Injector::inst()->get(SortDirectionType::class)->toType(),
'description' => 'Sort direction (ASC / DESC)'
]
]
'description' => 'Sort direction (ASC / DESC)',
],
],
]);
}

Expand Down
Loading

0 comments on commit ea08f67

Please sign in to comment.