Giter VIP home page Giter VIP logo

Comments (2)

mattbretl avatar mattbretl commented on August 11, 2024

This was resolved via Discord chat. The following is a summary..

Definition of PgManytoManyInflector:

import GraphileUtils from 'graphile-utils'

const { makeAddInflectorsPlugin } = GraphileUtils

export default makeAddInflectorsPlugin(
  {
    manyToManyRelationByKeys (
      _leftKeyAttributes,
      _junctionLeftKeyAttributes,
      _junctionRightKeyAttributes,
      _rightKeyAttributes,
      _junctionTable,
      rightTable,
      _junctionLeftConstraint,
      junctionRightConstraint
    ) {
      if (junctionRightConstraint.tags.manyToManyFieldName) {
        return junctionRightConstraint.tags.manyToManyFieldName
      }
      return this.camelCase(
        `${this.pluralize(this._singularizedTableName(rightTable))}`
      )
    },
    manyToManyRelationByKeysSimple (
      _leftKeyAttributes,
      _junctionLeftKeyAttributes,
      _junctionRightKeyAttributes,
      _rightKeyAttributes,
      _junctionTable,
      rightTable,
      _junctionLeftConstraint,
      junctionRightConstraint
    ) {
      if (junctionRightConstraint.tags.manyToManySimpleFieldName) {
        return junctionRightConstraint.tags.manyToManySimpleFieldName
      }
      return this.camelCase(
        `${this.pluralize(this._singularizedTableName(rightTable))}`
      )
    }
  },
  true
)

It's working as expected. The many-to-many plugin adds additional fields to your GraphQL types by looking for foreign keys that lead to junction tables. In your case, the plugin is trying to add two fields on your User type that have a many-to-many relationship with your Image type:

  • The first represents the path from users -> articles.author_id -> articles.cover_id -> images
  • The second represents the path from users -> articles.updater_id -> articles.cover_id -> images

Your PgManyToManyInflector plugin is trying to simplify the field names by simply calling the field images, but in this case, images could mean either the article images as an author or the article images as an updater. PostGraphile detects this field name conflict and throws the error.

This why the ManyToMany plugin has to use such verbose names by default.. it's the only way to prevent conflicts like this. Simply using the other table's name will work.. until you have two paths to the table, like this case.

To fix this, you can either:

  1. Add some extra logic to your Inflector plugin.. essentially an extra if statement that catches these cases and assigns custom field names; or
  2. Wait for the implementation of @omit manyToMany (see #11) and then omit one (or both) of the fields that caused the conflict.

The first approach would look something like this:

      // Just before the final return statement in the custom inflector..
      if (
        junctionTable.name === "articles" &&
        rightTable.name === "images" &&
        junctionLeftKeyAttributes.length === 1 &&
        junctionLeftKeyAttributes[0].name === "author_id"
      ) {
        return this.camelCase(
          `author-article-${this.pluralize(this._singularizedTableName(rightTable))}`
        );
      }
      if (
        junctionTable.name === "articles" &&
        rightTable.name === "images" &&
        junctionLeftKeyAttributes.length === 1 &&
        junctionLeftKeyAttributes[0].name === "updater_id"
      ) {
        return this.camelCase(
          `updater-article-${this.pluralize(this._singularizedTableName(rightTable))}`
        );
      }

With this approach you'll end up with authorArticleImages and updaterArticleImages fields.

from pg-many-to-many.

benjie avatar benjie commented on August 11, 2024

By the way, if you can think of a way to make these errors better, let me know 👍

from pg-many-to-many.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.