Skip to content

Commit a2fce6f

Browse files
Add missing GraphQL fields (#503)
1 parent ca4a672 commit a2fce6f

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

src/GraphQL/SeoProType.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,27 @@ public function fields(): array
8888
'twitter_title' => [
8989
'type' => GraphQL::string(),
9090
],
91+
'robots' => [
92+
'type' => GraphQL::listOf(GraphQL::string()),
93+
'resolve' => function ($meta) {
94+
return collect($meta['robots'])->map(fn ($item) => (string) $item)->all();
95+
},
96+
],
97+
'robots_indexing' => [
98+
'type' => GraphQL::string(),
99+
],
100+
'is_default_site' => [
101+
'type' => GraphQL::boolean(),
102+
],
103+
'current_hreflang' => [
104+
'type' => GraphQL::string(),
105+
],
106+
'json_ld' => [
107+
'type' => GraphQL::listOf(GraphQL::string()),
108+
'resolve' => function ($meta) {
109+
return $meta['json_ld']->all();
110+
},
111+
],
91112
'image' => [
92113
'type' => GraphQL::type('AssetInterface'),
93114
'resolve' => function ($meta) {

tests/GraphQLTest.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,154 @@ public function it_queries_for_term_seo_cascade_so_user_can_render_custom_meta()
275275
]]);
276276
}
277277

278+
#[Test]
279+
public function it_queries_for_entry_seo_robots_and_indexing()
280+
{
281+
$this->setSeoOnEntry(Data::findByUri('/nectar'), [
282+
'robots' => ['noindex', 'nofollow'],
283+
]);
284+
285+
$query = <<<'GQL'
286+
{
287+
entry(slug: "nectar") {
288+
seo {
289+
robots
290+
robots_indexing
291+
}
292+
}
293+
}
294+
GQL;
295+
296+
$this
297+
->withoutExceptionHandling()
298+
->post('/graphql', ['query' => $query])
299+
->assertGqlOk()
300+
->assertExactJson(['data' => [
301+
'entry' => [
302+
'seo' => [
303+
'robots' => ['noindex', 'nofollow'],
304+
'robots_indexing' => 'noindex',
305+
],
306+
],
307+
]]);
308+
}
309+
310+
#[Test]
311+
public function it_queries_for_entry_seo_robots_defaults_when_not_set()
312+
{
313+
$query = <<<'GQL'
314+
{
315+
entry(slug: "nectar") {
316+
seo {
317+
robots
318+
robots_indexing
319+
}
320+
}
321+
}
322+
GQL;
323+
324+
$this
325+
->withoutExceptionHandling()
326+
->post('/graphql', ['query' => $query])
327+
->assertGqlOk()
328+
->assertExactJson(['data' => [
329+
'entry' => [
330+
'seo' => [
331+
'robots' => [],
332+
'robots_indexing' => 'index',
333+
],
334+
],
335+
]]);
336+
}
337+
338+
#[Test]
339+
public function it_queries_for_entry_seo_hreflang_and_default_site()
340+
{
341+
$query = <<<'GQL'
342+
{
343+
entry(slug: "nectar") {
344+
seo {
345+
is_default_site
346+
current_hreflang
347+
}
348+
}
349+
}
350+
GQL;
351+
352+
$this
353+
->withoutExceptionHandling()
354+
->post('/graphql', ['query' => $query])
355+
->assertGqlOk()
356+
->assertExactJson(['data' => [
357+
'entry' => [
358+
'seo' => [
359+
'is_default_site' => true,
360+
'current_hreflang' => 'en',
361+
],
362+
],
363+
]]);
364+
}
365+
366+
#[Test]
367+
public function it_queries_for_entry_seo_json_ld()
368+
{
369+
$this->setSeoInSiteDefaults([
370+
'site_name' => 'Cool Runnings',
371+
'priority' => 0.7,
372+
'json_ld_entity' => 'organization',
373+
'json_ld_organization_name' => 'Cool Runnings Ltd',
374+
]);
375+
376+
$query = <<<'GQL'
377+
{
378+
entry(slug: "nectar") {
379+
seo {
380+
json_ld
381+
}
382+
}
383+
}
384+
GQL;
385+
386+
$response = $this
387+
->withoutExceptionHandling()
388+
->post('/graphql', ['query' => $query])
389+
->assertGqlOk();
390+
391+
$jsonLd = $response->json('data.entry.seo.json_ld');
392+
393+
$this->assertIsArray($jsonLd);
394+
$this->assertNotEmpty($jsonLd);
395+
396+
$organization = json_decode($jsonLd[0], true);
397+
$this->assertEquals('https://schema.org', $organization['@context']);
398+
$this->assertEquals('Organization', $organization['@type']);
399+
$this->assertEquals('Cool Runnings Ltd', $organization['name']);
400+
}
401+
402+
#[Test]
403+
public function it_queries_for_entry_seo_json_ld_without_empty_schema()
404+
{
405+
$query = <<<'GQL'
406+
{
407+
entry(slug: "nectar") {
408+
seo {
409+
json_ld
410+
}
411+
}
412+
}
413+
GQL;
414+
415+
$response = $this
416+
->withoutExceptionHandling()
417+
->post('/graphql', ['query' => $query])
418+
->assertGqlOk();
419+
420+
$jsonLd = $response->json('data.entry.seo.json_ld');
421+
422+
$this->assertIsArray($jsonLd);
423+
$this->assertEmpty($jsonLd);
424+
}
425+
278426
#[Test]
279427
public function it_gracefully_outputs_null_image_when_not_set()
280428
{

0 commit comments

Comments
 (0)