Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gatsby-source-drupal] relationship handling improvments #5020

Merged
merged 4 commits into from
Jul 20, 2018

Conversation

pieh
Copy link
Contributor

@pieh pieh commented Apr 17, 2018

  • add support for relationships with many values (arrays)
  • fix back refs not showing for entities without any relationships
  • BREAKING CHANGE change backrefs to arrays as we can't ensure that backref will have single value

There is also one more breaking change I'd like to add here, but need to consult first:

Right now back references are added with names of referencing types - so if we would have hypothetical node type "Product" and 2 fields: "cross_sell" and "up_sell" than links to other products then both backrefs would all be dumped into single node__product field and we couldn't differentiate if current product is in cross-sell or up-sell relationship.

Proposed solution:
Use name of relationship as base for back reference field name - maybe something like back_${relationship_name} and maybe allow setting that name in plugin settings:

back_references: {
  `node__product.up_sell` : `is_up_sell_for`
}

@gatsbybot
Copy link
Collaborator

gatsbybot commented Apr 17, 2018

Deploy preview for gatsbygram ready!

Built with commit a6ab3f2

https://deploy-preview-5020--gatsbygram.netlify.com

@gatsbybot
Copy link
Collaborator

gatsbybot commented Apr 17, 2018

Deploy preview for using-drupal ready!

Built with commit a6ab3f2

https://deploy-preview-5020--using-drupal.netlify.com

@KyleAMathews
Copy link
Contributor

Oh interesting — yeah, making the backref fields more explicit makes a lot of sense. What's the usecase for making those overridable in the plugin config?

@pieh
Copy link
Contributor Author

pieh commented Apr 18, 2018

What's the usecase for making those overridable in the plugin config?

Sorry, I've missed that yesterday.

So situation was that content node had field name belongs_to_category linking to taxonomy terms. When I changed back ref name from type name to back_field_belongs_to_category it looked like this

taxonomyTermCategories {
  relationships {
    back_field_belongs_to_category {
      # this is content node, but field name is a bit confusing
      # allowing to name it like "linked_content_node" would make schema more self documented
      # but we can't do that automatically because there would be issue described with 
      # "cross-sell" / "up-sell" scenario in PR description
      title
    }
}

I'm not convinced that proposed solution is best (or even good) - not sure what's the nicest way to handle this

@m-allanson
Copy link
Contributor

I don't think I understand what this is doing 😅. Nudging @KyleAMathews for another look.

@dubcanada
Copy link
Contributor

dubcanada commented May 1, 2018

I like this as it stands, tested it in my environment and it works.

I am not sure about the second part, maybe make a todo for that so we can discuss? I am not sure I follow you, are you saying if both fields have the same machine name? Cause you can type different them, they would be added as multiple different types and you would just use

... on field_type {}

To get the specific one you want, so for paragraphs you can use

... on paragraph__machine_name{}

But maybe I don't follow what you are saying.

👍

dubcanada
dubcanada previously approved these changes May 1, 2018
@eojthebrave
Copy link
Contributor

This PR is working well and solving some problems I was encountering.

FWIW. This merges cleanly with the V1 branch. Should it be re-rolled for Gatsby V2 (master branch)? And then back-ported for V1?

eojthebrave added a commit to OsioLabs/gatsby-source-drupal that referenced this pull request Jul 3, 2018
@KyleAMathews
Copy link
Contributor

@pieh you want to revisit this and get it landed in v1/master?

@pieh
Copy link
Contributor Author

pieh commented Jul 10, 2018

Yeah, I'll update this so it can be merged to master before I open any new PR - but will have to handle #6261 first

@eojthebrave
Copy link
Contributor

Let me know if there's anything I can do to help, and/or test things.

… (arrays), fix back refs not showing for entities without any relationships, change backrefs to arrays as we can't ensure that backref will have single value

Signed-off-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
@KyleAMathews
Copy link
Contributor

KyleAMathews commented Jul 19, 2018

Deploy preview for using-postcss-sass failed.

Built with commit a6ab3f2

https://app.netlify.com/sites/using-postcss-sass/deploys/5b5086953813f00e269da002

@mglaman
Copy link
Contributor

mglaman commented Jul 19, 2018

Just gave this a test run using some Drupal Commerce data, and got the following error:

  TypeError: Cannot read property 'internal' of null
  
  - infer-graphql-input-fields.js:327 _.each
    [gatsby]/[gatsby]/dist/schema/infer-graphql-input-fields.js:327:38

Which is at:

      // TODO: Union the objects in array
      const nodeToFind = _.isArray(value) ? value[0] : value;
      const linkedNode = findLinkedNode(nodeToFind); // Get from cache if found, else store into it
      if (linkedNodeCache[linkedNode.internal.type]) {

I did some basic debugging and findLinkedNode(nodeToFind); isn't finding the node from the passed UUID value.

I'm going to try and debug it some more and figure out which relationship is erroring.

@pieh
Copy link
Contributor Author

pieh commented Jul 19, 2018

Hey, I'm working on it right now - I might have fix for it already locally. Let me push out my work here

@pieh
Copy link
Contributor Author

pieh commented Jul 19, 2018

@mglaman can You pull and check again?

@mglaman
Copy link
Contributor

mglaman commented Jul 19, 2018

Rad! I'll test after my commute and report back

@mglaman
Copy link
Contributor

mglaman commented Jul 19, 2018

graphiql_and_commerce2x____drupal_sites_commerce2x__-____gatsby_plugins_gatsby-source-drupal-fork_gatsby-node_js__commerce2x

Amazing! It fixes the error, and now the query is returning all of my variations!

💯 💯 💯 💯 💯

@mglaman
Copy link
Contributor

mglaman commented Jul 19, 2018

I'm still seeing one issue, but I honestly think it could be done in a follow up, so that multiple valued references can at least be fixed first.

manage_fields___drush_site-install

We have three taxonomy references on an entity. But only the first one is being provided as a relationship. When I fetch the product over JSON API I see the relationships available:

        "field_brand": {
          "data": {
            "type": "taxonomy_term--brands",
            "id": "316b2893-6876-49e0-9ce6-68d8751bc8b9"
          },
          "links": {
            "self": "http:\/\/commerce2x.ddev.local\/jsonapi\/commerce_product\/clothing\/802dfe56-8536-4287-a0fa-6bed3fd3a621\/relationships\/field_brand",
            "related": "http:\/\/commerce2x.ddev.local\/jsonapi\/commerce_product\/clothing\/802dfe56-8536-4287-a0fa-6bed3fd3a621\/field_brand"
          }
        },
        "field_product_categories": {
          "data": [
            {
              "type": "taxonomy_term--product_categories",
              "id": "d52448c0-22c5-4a62-995c-e09c881a3f67"
            }
          ],
          "links": {
            "self": "http:\/\/commerce2x.ddev.local\/jsonapi\/commerce_product\/clothing\/802dfe56-8536-4287-a0fa-6bed3fd3a621\/relationships\/field_product_categories",
            "related": "http:\/\/commerce2x.ddev.local\/jsonapi\/commerce_product\/clothing\/802dfe56-8536-4287-a0fa-6bed3fd3a621\/field_product_categories"
          }
        },
        "field_special_categories": {
          "data": [],
          "links": {
            "self": "http:\/\/commerce2x.ddev.local\/jsonapi\/commerce_product\/clothing\/802dfe56-8536-4287-a0fa-6bed3fd3a621\/relationships\/field_special_categories",
            "related": "http:\/\/commerce2x.ddev.local\/jsonapi\/commerce_product\/clothing\/802dfe56-8536-4287-a0fa-6bed3fd3a621\/field_special_categories"
          }
        },

As I said, in my opinion the bigger blocker is not being able to receive all relationship values.

@pieh
Copy link
Contributor Author

pieh commented Jul 19, 2018

It fixes the error, and now the query is returning all of my variations!

That's great to hear! I was about to install docker to setup demo site that would actually test those changes (was doing test driven dev on this on today) :)

I'm still seeing one issue, but I honestly think it could be done in a follow up, so that multiple valued references can at least be fixed first.

Do You have your site hosted somewhere (I seeYou use local url) so I could debug this issue? If so please let me know - either here or via mail if You don't want to show this publicly (misiek.piechowiak@gmail.com)

@pieh pieh self-assigned this Jul 19, 2018
@mglaman
Copy link
Contributor

mglaman commented Jul 19, 2018

@pieh :) here is a site! http://dev-commerce2x-jsonapi.pantheonsite.io/jsonapi

Took a little to get set up (haven't used Pantheon in a long time)

Here is the Gatsby setup I am using https://github.com/mglaman/gatsbyjs_commerce_demo

@pieh
Copy link
Contributor Author

pieh commented Jul 19, 2018

I just checked that locally it all 3 taxonomies show up in relationships (at least for commerce_product/simple):
drupal-categories

So I would suspect this is related to how we construct schema - we do that from actual data - so if none of Your products have assigned any Brand / Categories / Special categories, then it won't show up in schema. For testing purpose You can create dummy category and add it to dummy product to force those fields appear in schema.

Let me know if what I wrote make sense and is actually valid in Your case!

@mglaman
Copy link
Contributor

mglaman commented Jul 19, 2018

Hm, weird. I think my local was just messed up. When I ran this query against that sandbox environment all was great!

query test {
  allCommerceProductClothing {
    edges {
      node {
        title
        id
        relationships {
          field_brand {
            id
          }
          field_product_categories {
            id
          }
          variations {
            id
            sku
            relationships {
              attribute_size {
                id
              }
              attribute_color {
                id
              }
              field_images {
                localFile {
                  id
                  relativePath
                }
              }
            }
          }
        }
      }
    }
  }
}

@mglaman
Copy link
Contributor

mglaman commented Jul 19, 2018

@pieh thanks so much for helping unblock this :)

@pieh
Copy link
Contributor Author

pieh commented Jul 19, 2018

This is ready for review

Copy link
Contributor

@KyleAMathews KyleAMathews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gave a glance over and it looks reasonable :-) So take that for what its worth haha :-D

@pieh
Copy link
Contributor Author

pieh commented Jul 20, 2018

I'll just merge it (it being opened for 3 months really bugs me) and if it cause problems it will be additional motivation to fix those :)

@pieh pieh merged commit 51ff809 into gatsbyjs:master Jul 20, 2018
@luke-dare
Copy link

Great work .. this is definitely something I'm going to need for a site I'm working on.

Is a release planned for the near future?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants