drupal-graphql / graphql

GraphQL integration for Drupal 9/10
286 stars 202 forks source link

graphql 4.x retrieving paragraph fields #1017

Open digcatcom opened 4 years ago

digcatcom commented 4 years ago

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

bejes commented 4 years ago

You should add use GraphQL\Error\Error; at the top of your schema.

digcat commented 4 years ago

hi bejes

Ok Ive added that to my schema file, located in src/Plugin/GraphQL/Schema

Cant see any additional errors to what I have above. Content types, taxonomys are working fine, just seems an issue with how im trying to get Paragraph. A working example from someone else who has had success getting Paragraph data would be the most helpful.

thanks for your input though, been struggling on this for weeks on and off.

cheers

bejes commented 4 years ago

I got this working today. Here is my schema `schema { query: Query }

interface NodeInterface { id: Int! }

type Query { section(id: Int!): Section sections( offset: Int = 0 limit: Int = 10 ): SectionConnection! }

type Section implements NodeInterface { id: Int! title: String! author: String parent: Parent displayChildLinks: Boolean displayChildSummary: Boolean! syndicatedSummary: String tags: [TagTerm] sectionBody: [Row]!

}

type TagTerm { id: Int name: String }

interface Paragraph { id: Int! }

type Parent { id: Int! }

type Row implements Paragraph { id: Int! isFluidWidth: Boolean items: [Paragraph]! }

type PageContent implements Paragraph { id: Int! content: String }

type Accordion implements Paragraph { id: Int! title: String text: String }

type SectionConnection { total: Int! items: [Section!] } And my resolvers. /**

bejes commented 4 years ago

Query is something like { sections { items { title parent { id } author displayChildLinks displayChildSummary tags { name } sectionBody { ... on Row { items { ... on PageContent { id content } ... on Accordion { id text title } } } } } } }

digcat commented 4 years ago

thankyou bejes, that was what I needed to progress, your example gave me my missing pieces, so getting Paragraph data is working well, once you have the correct includes and syntax's. This ticket can be closed, although would imagine useful for others facing similar challenge.