1
votes

I'm struggeling to find an answer, because I'm not really sure if this is a Drupal, GraphQl or Gatsby question.

I'm building a portfolio-site with Gatsby.js and Drupal-8 as datasource (via gatsby-source-drupal).

GraphQl queries for Nodes, Taxonomy, Users, eg are working without problems. But I can not access my created Views at my API-endpoint.

I have created a working Views-Page with a path. I also exposed the data as a Block. Tried a REST-Export as serialized JSON, but I can not get it working with JSON:API and JSON-Views-Module.

I expect to access the data from my View at my /jsonapi/endpoint, but my Views are not showing up.

I can't get my head around this. What am I missing? Is it even possible? Thanks!

2
I believe JSON:API only exposes entities (e.g., nodes, taxonomy, and users). You can query a collection of entities and construct a view (of sorts) on the front end. To expose the results of a Drupal view via a JSON endpoint you need the RESTful Web Services and Serialization modules (both are in core), then you would configure the view so that it provides a REST export at whichever path you specify. - othermachines
Thank you for your answer. I also believe only entities are exposed. I hoped for a way to "plug" the views data into my JSON:API endpoint, but this seems not to be possible. I next will try to access the two different endpoints (from JSON:API and my Views REST export) from within my Gatsby project. I also might give the ´entityqueue´-module (drupal.org/project/entityqueue) another try. thank you - lucasdon
You're welcome. If you're able to successfully export your view as JSON on the Drupal side then the issue probably lies with your front end set-up. I haven't yet used the Drupal source plugin for Gatsby but if you include more details in your question that might help get attention from someone who knows more about that side of things than I do. Cheers and good luck- - othermachines
Hey @othermachines, thank you for clarifying this! I now got what I needed (a customer ordered list of entities) via ´entityqueue´-module. It is not an elegant solution, but it works. But I'm frustrated that one of the most powerful features of Drupal can not be used this easily in a headless setup. If I had encountered this issue earlier it would have been a dealbreaker to use Drupal as backend. But I'll investigate further and if I find another solution I'll add it as an edit to this question. Thanks for your time! - lucasdon

2 Answers

0
votes

TL;DR; You can't (from Drupal's JSON:API docs Unlike the REST module that comes with Drupal Core, JSON:API does not export Views results)

How to get filtered results

gatsby-source-drupal only fetches from the JSON:API endpoint (by default /jsonapi).

The way I see you can, kind of, emulate what Views does is using the filters options that gatsby-source-drupal provides.

The JSON:API Extras module for Drupal allows you to set some defaults filters as well.

Drupal documentation about filters

Example: ?filter[field_name]=value&filter[field_other]=value

Best!

0
votes

With the JSON:API module you can not get views indeed, but there is also the GraphQl module and for your purpose the GraphQl views module that can make a view available to create a custom graphql schema. Good luck