drupal-graphql / graphql-views

11 stars 18 forks source link

feat(viewsreference): added support for viewsreference. #22

Closed LennardWesterveld closed 5 years ago

LennardWesterveld commented 6 years ago

I'm working on a viewsreference support for graphql. I have the basic working but I don't have support right now for filter arguments and pagination. Do you have any suggestion on how to implement this?

How the query looks like now

{
 fieldViews {
    viewDerivative {
      ... on RecentBlockResult {
        results {
          nid
          ... on NodeBlog {
            fieldMedia {
              entity {
                ... on MediaImage {
                  fieldImage {
                    title
                  }
                }
              }
            }
          }
        }
      }
      ... on BlogOverviewResult {
        results {
          nid
          ... on NodeBlog {
            title
          }
        }
      }
    }
  }
}
pmelab commented 5 years ago

Hi! I think you are pretty close. The view field already accepts arguments, but you have to declare them in the plugin definition. Like the ViewDervier does. https://github.com/drupal-graphql/graphql-views/blob/8.x-1.x/src/Plugin/Deriver/Fields/ViewDeriver.php#L23-L54

Would make sense to move that logic into a trait to use it in both.

LennardWesterveld commented 5 years ago

Added support for arguments as well please see changes:

{
  fieldViews {
    viewDerivative(filter: {blog_category: "4"}, page: 0) {
        ... on BlogOverviewResult {
          results {
            nid
            ... on NodeBlog {
              title
            }
          }
        }
      }
    }
  }
}
LennardWesterveld commented 5 years ago

Fixed the failing tests!

eescribanoc commented 5 years ago

I am wondering if there is a plan to roll out this feature into the module https://www.drupal.org/project/graphql_views or if the module in drupal is not the same as this one

eescribanoc commented 5 years ago

I am using this code for a project and I found out that I need the following changes to make it work all the way. Without these changes the contextual filters don't work:

Index: graphql-views/src/Plugin/Deriver/Fields/ViewDeriver.php
===================================================================
--- graphql-views/src/Plugin/Deriver/Fields/ViewDeriver.php (date 1546446611000)
+++ graphql-views/src/Plugin/Deriver/Fields/ViewDeriver.php (date 1546446611000)
@@ -46,7 +46,7 @@
           'view' => $viewId,
           'display' => $displayId,
           'paged' => $this->isPaged($display),
-          'arguments_info' => $info,
+          'argument_info' => $info,
         ] + $this->getCacheMetadataDefinition($view, $display) + $basePluginDefinition;
       }
     }
Index:graphql-views/src/Plugin/GraphQL/Fields/View.php
===================================================================
--- graphql-views/src/Plugin/GraphQL/Fields/View.php    (date 1546446505000)
+++ graphql-views/src/Plugin/GraphQL/Fields/View.php    (date 1546446505000)
@@ -71,7 +71,7 @@

       // Set view contextual filters.
       /* @see \Drupal\graphql_core\Plugin\Deriver\ViewDeriverBase::getArgumentsInfo() */
-      if (!empty($definition['arguments_info'])) {
+      if (!empty($definition['argument_info'])) {
         $arguments = $this->extractContextualFilters($value, $args);
         $executable->setArguments($arguments);
       }
@@ -131,7 +131,7 @@
     $definition = $this->getPluginDefinition();
     $arguments = [];

-    foreach ($definition['arguments_info'] as $argumentId => $argumentInfo) {
+    foreach ($definition['argument_info'] as $argumentId => $argumentInfo) {
       if (isset($args['contextualFilter'][$argumentId])) {
         $arguments[$argumentInfo['index']] = $args['contextualFilter'][$argumentId];
       }
LennardWesterveld commented 5 years ago

Nice one, i will apply this patch this week!

LennardWesterveld commented 5 years ago

I changed it they other way from argument_info to arguments_info

eescribanoc commented 5 years ago

@LennardWesterveld I have been working today on supporting the configuration options from the last version from viewsreference module where you can configure the items per page, pager type, offset and argument. Maybe it is an idea that I update a fork of my own and you take a look at it? Otherwise I can do a diff. In the project I am working on I wanted to be able to create overview pages listing nodes of certain content types but I wanted to use only one view and one display. Just wanted to be able to adjust the pagination and offset settings and a contextual filter that would filter the view by type. So I thought to use the settings from the viewsreference module.

LennardWesterveld commented 5 years ago

Thanks for the PR I will look in to it this weekend!

eescribanoc commented 5 years ago

@LennardWesterveld I have this code, being $type "news" for example, and it does not work: viewDerivative(filter: {type: $type}) { }

But the following code works viewDerivative(filter: {type: "news"}) { }

Is it not possible to work with variables? In the view I have available once I create a display I can use the variables but as a viewsreference I cannot.

LennardWesterveld commented 5 years ago

@LennardWesterveld I have this code, being $type "news" for example, and it does not work: viewDerivative(filter: {type: $type}) { }

But the following code works viewDerivative(filter: {type: "news"}) { }

Is it not possible to work with variables? In the view I have available once I create a display I can use the variables but as a viewsreference I cannot.

I tested it locally with query Router($uri: String!, $category: String!) { first defining the parameters and later on using it in the viewDerivative(filter: {blog_category: $category}) { and that works.

LennardWesterveld commented 5 years ago

Resolved the feedback @fubhy.

tmsss commented 4 years ago

I'm trying to get the data from a views reference field, but I'm getting a "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data" error. The query that I'm using is the following, am I missing something and/or can you provide a full working query?

{
  node:nodeById(id: $nid) {
       ...Page

  }
}

fragment Page on NodeNdPage {
  title
  body {
    summaryProcessed
    processed
  }
  fieldPageView {
    viewDerivative {
      ... on NewsGraphql1ViewResult  {
        results {
          nid
        }
      }
    }
  }
}