forked from silverstripe/silverstripe-elemental
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW Add GraphQL query HOC for returning the content blocks on a page
This will return content blocks via the ElementalArea has_one relationship on a page. If you have implemented custom or extra has_one ElementalArea relationships then you will need to replace and/or augment this query using the JavaScript Injector API.
- Loading branch information
1 parent
48926b1
commit 4b5ee57
Showing
5 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { graphql } from 'react-apollo'; | ||
import gql from 'graphql-tag'; | ||
|
||
// GraphQL query for retrieving the current state of elements for a page, via the | ||
// ElementalArea. The results of the query must be set to the "blocks" prop on | ||
// the component that this HOC is applied to for binding implementation. | ||
const query = gql` | ||
query readBlocksForPage($id:ID!) { | ||
readOnePage(ID: $id, Versioning: { | ||
Mode: LATEST | ||
}){ | ||
ID | ||
ElementalArea { | ||
Elements { | ||
pageInfo { | ||
totalCount | ||
} | ||
edges { | ||
node { | ||
ID | ||
Title | ||
Summary | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const config = { | ||
options({ pageId }) { | ||
return { | ||
variables: { | ||
id: pageId, | ||
} | ||
}; | ||
}, | ||
props( | ||
{ | ||
data: { | ||
error, | ||
readBlocksForPage, | ||
loading: networkLoading, | ||
}, | ||
} | ||
) { | ||
const blocks = readBlocksForPage || null; | ||
|
||
const errors = error && error.graphQLErrors && | ||
error.graphQLErrors.map((graphQLError) => graphQLError.message); | ||
|
||
return { | ||
loading: networkLoading || !blocks, | ||
blocks, | ||
graphQLErrors: errors, | ||
}; | ||
}, | ||
}; | ||
|
||
export { query, config }; | ||
|
||
export default graphql(query, config); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace DNADesign\Elemental\GraphQL; | ||
|
||
use GraphQL\Type\Definition\ResolveInfo; | ||
use SilverStripe\GraphQL\OperationResolver; | ||
use SilverStripe\ORM\DataList; | ||
|
||
class ElementsResolver implements OperationResolver | ||
{ | ||
public function resolve($object, array $args, $context, ResolveInfo $info) | ||
{ | ||
if (!$object::singleton()->canView($context['currentUser'])) { | ||
throw new \Exception('Current user cannot view elements'); | ||
} | ||
|
||
/** @var DataList $elements */ | ||
$elements = $object->Elements(); | ||
return $elements; | ||
} | ||
} |