drupal-graphql / drupal-graphql/graphql

graphql 4.x retrieving paragraph fields

Open
#1,017 5 comments 0 reactions 0 assignees View on GitHub
4.x question
Dominant language
PHP
Stars
287
Forks
198
PR merge metrics
No merged PRs in 30d

Description

I've been attempting to retrieve paragraph fields from a node, and could really do with a solid working example. Ive tried following https://github.com/drupal-graphql/graphql/pull/968
But am getting an error.
So my example:
node called Event
->entity_reference_revisions called field_venue_type pointing at 2 Paragraph types
-> Paragraph -> virtual_meeting
-> physical_meeting

My Schema

type Business_Event {
id: Int!
venueType: [VenueType]
}
interface VenueType {
id: Int!
}
type VirtualMeeting implements VenueType {
id: Int!
}
type PhysicalMeeting implements VenueType {
id: Int!
placeName: String
}

My Resolvers

$registry->addFieldResolver('Business_Event', 'venueType',
$builder->produce('entity_reference_revisions')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_venue_type'))
);
$registry->addTypeResolver('VenueType', function ($value) {
if ($value instanceof Paragraph) {
switch ($value->bundle()) {
case 'physical_meeting': return 'PhysicalMeeting';
case 'virtual_meeting': return 'VirtualMeeting';
}
}
throw new Error('Could not resolve VenueType type.');
});
$venueItemBuilder = $builder->compose(
$builder->produce('entity_reference_revisions')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_venue_type'))
);
$registry->addFieldResolver('PhysicalMeeting', 'id',
$builder->compose(
$venueItemBuilder,
$builder->produce('entity_id')
->map('entity', $builder->fromParent())
)
);
$registry->addFieldResolver('PhysicalMeeting', 'placeName',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:paragraph'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_place_name.value'))
);
$registry->addFieldResolver('VirtualMeeting', 'id',
$builder->produce('entity_id')
->map('entity', $builder->fromParent())
);

These all appear as expected in the explorer, but when i run the query

query MyQuery {
business_event(id: 909) {
venueType {
id
... on VirtualMeeting {
id
}
... on PhysicalMeeting {
id
placeName
}
}
id
}
}

I only get

{
"data": {
"business_event": {
"venueType": [
null
],
"id": 909
}
},
"extensions": [
{
"message": "Class 'Drupal\\businesslisting_graphql\\Plugin\\GraphQL\\Schema\\Error' not found",
"locations": [
{
"line": 3,
"column": 5
}
],
"path": [
"business_event",
"venueType",
0
]
}
]
}

Any pointers you might be able to give me to solve this gratefully received, hoping its something simple, rather than an issue with your excellent module. Many thanks

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.