Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.1] GraphQL query an entry by slug or URI #3193

Merged
merged 2 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/GraphQL/Queries/EntryQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,32 @@ public function args(): array
{
return [
'id' => GraphQL::string(),
'slug' => GraphQL::string(),
'collection' => GraphQL::string(),
'uri' => GraphQL::string(),
];
}

public function resolve($root, $args)
{
$query = Facades\Entry::query();

if ($id = $args['id']) {
if ($id = $args['id'] ?? null) {
$query->where('id', $id);
}

if ($slug = $args['slug'] ?? null) {
$query->where('slug', $slug);
}

if ($collection = $args['collection'] ?? null) {
$query->where('collection', $collection);
}

if ($uri = $args['uri'] ?? null) {
$query->where('uri', $uri);
}

return $query->limit(1)->get()->first();
}
}
73 changes: 73 additions & 0 deletions tests/Feature/GraphQL/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Feature\GraphQL;

use Facades\Statamic\Fields\BlueprintRepository;
use Facades\Tests\Factories\EntryFactory;
use Statamic\Facades\GraphQL;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
Expand Down Expand Up @@ -72,6 +73,78 @@ public function it_queries_an_entry_by_id()
]]);
}

/** @test */
public function it_queries_an_entry_by_slug()
{
EntryFactory::collection('blog')->id('123')->slug('foo')->create();
EntryFactory::collection('events')->id('456')->slug('foo')->create();

$query = <<<'GQL'
{
entry(slug: "foo") {
id
}
}
GQL;

$this
->withoutExceptionHandling()
->post('/graphql', ['query' => $query])
->assertGqlOk()
->assertExactJson(['data' => [
'entry' => [
'id' => '123',
],
]]);
}

/** @test */
public function it_queries_an_entry_by_slug_and_collection()
{
EntryFactory::collection('blog')->id('123')->slug('foo')->create();
EntryFactory::collection('events')->id('456')->slug('foo')->create();

$query = <<<'GQL'
{
entry(slug: "foo", collection: "events") {
id
}
}
GQL;

$this
->withoutExceptionHandling()
->post('/graphql', ['query' => $query])
->assertGqlOk()
->assertExactJson(['data' => [
'entry' => [
'id' => '456',
],
]]);
}

/** @test */
public function it_queries_an_entry_by_uri()
{
$query = <<<'GQL'
{
entry(uri: "/events/event-two") {
id
}
}
GQL;

$this
->withoutExceptionHandling()
->post('/graphql', ['query' => $query])
->assertGqlOk()
->assertExactJson(['data' => [
'entry' => [
'id' => '4',
],
]]);
}

/** @test */
public function it_can_add_custom_fields_to_interface()
{
Expand Down