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

shape definiton being overwritten if column name === field name #293

Open
bradzacher opened this issue Feb 18, 2018 · 1 comment
Open

shape definiton being overwritten if column name === field name #293

bradzacher opened this issue Feb 18, 2018 · 1 comment

Comments

@bradzacher
Copy link
Contributor

I have the following query:

query {
  User(id:39) {
    fieldA {
      created_by {
        id
      }
    }
  }
}

The problem that I have is that in the definition: fieldA's underlying column name is created_by, which matches the field name of the relationship field for created_by.

I've analyzed the generated query, and it queries perfectly.
The problem is with the shape generation..

The problem occurs in this switch here:
https://github.com/stems/join-monster/blob/master/src/define-object-shape.js#L17-L47

The switch assumes everything has a completely unique name, hence everything is writing into fieldDefinition[child.fieldName].

the generate SQL AST that runs through this function looks like this:

[
    {
        "type": "column",
        "name": "id",
        "fieldName": "id",
        "as": "id"
    },
    {
        "args": {},
        "type": "table",
        "name": "user",
        "as": "created_by",
        "children": [
            {
                "type": "column",
                "name": "user_id",
                "fieldName": "user_id",
                "as": "user_id"
            },
            {
                "args": {},
                "type": "column",
                "name": "user_id",
                "fieldName": "id",
                "as": "id"
            }
        ],
        "fieldName": "created_by", /* created_by */
        "grabMany": false
    },
    {
        "type": "column",
        "name": "created_by",
        "fieldName": "created_by", /* created_by */
        "as": "created_by"
    }
]

so when the switch iterates over that AST, the shape definition for created_by with type === "table" is overwritten by the the definition for created_by with type === "column".

debug output:

  join-monster
  join-monster SQL_AST
  join-monster  { args: { id: 39 },
  type: 'table',
  name: 'user',
  as: 'Users',
  children:
   [ { type: 'column',
       name: 'user_id',
       fieldName: 'user_id',
       as: 'user_id' },
     SQLASTNode {
       args: {},
       type: 'table',
       name: 'some_table',
       as: 'submitted_',
       children:
        [ { type: 'column', name: 'id', fieldName: 'id', as: 'id' },
          SQLASTNode {
            args: {},
            type: 'table',
            name: 'user',
            as: 'created_by',
            children:
             [ { type: 'column',
                 name: 'user_id',
                 fieldName: 'user_id',
                 as: 'user_id' },
               SQLASTNode {
                 args: {},
                 type: 'column',
                 name: 'user_id',
                 fieldName: 'id',
                 as: 'id' } ],
            fieldName: 'created_by',
            grabMany: false,
            sqlJoin: [Function] } ],
       fieldName: 'submitted_some_table',
       grabMany: true,
       sqlBatch:
        { thisKey:
           { type: 'column',
             name: 'created_by',
             fieldName: 'created_by',
             as: 'created_by' },
          parentKey:
           { type: 'column',
             name: 'user_id',
             fieldName: 'user_id',
             as: 'user_id' } } } ],
  fieldName: 'Users',
  grabMany: true,
  where: [Function] } +0ms
  join-monster
  join-monster SQL
  join-monster  SELECT
  `Users`.`user_id` AS `user_id`
FROM user `Users`
WHERE `Users`.`user_id` = 39
   AND `Users`.`active` = 1 +6ms
  join-monster
  join-monster SHAPE_DEFINITION
  join-monster  [ { user_id: 'user_id' } ] +1ms
  join-monster
  join-monster RAW DATA
  join-monster  [ TextRow { user_id: 39 } ] +22ms
  join-monster 1 rows... +0ms
  join-monster
  join-monster SHAPED_DATA
  join-monster  [ { user_id: 39 } ] +2ms
  join-monster
  join-monster SQL_AST
  join-monster  SQLASTNode {
  args: {},
  type: 'table',
  name: 'some_table',
  as: 'submitted_',
  children:
   [ { type: 'column', name: 'id', fieldName: 'id', as: 'id' },
     SQLASTNode {
       args: {},
       type: 'table',
       name: 'user',
       as: 'created_by',
       children:
        [ { type: 'column',
            name: 'user_id',
            fieldName: 'user_id',
            as: 'user_id' },
          SQLASTNode {
            args: {},
            type: 'column',
            name: 'user_id',
            fieldName: 'id',
            as: 'id' } ],
       fieldName: 'created_by',
       grabMany: false,
       sqlJoin: [Function] },
     { type: 'column',
       name: 'created_by',
       fieldName: 'created_by',
       as: 'created_by' } ],
  fieldName: 'some_table',
  grabMany: true,
  sqlBatch:
   { thisKey:
      { type: 'column',
        name: 'created_by',
        fieldName: 'created_by',
        as: 'created_by' },
     parentKey:
      { type: 'column',
        name: 'user_id',
        fieldName: 'user_id',
        as: 'user_id' } } } +4ms
  join-monster
  join-monster SQL
  join-monster  SELECT
  `submitted_`.`id` AS `id`,
  `created_by`.`user_id` AS `created_by__user_id`,
  `created_by`.`user_id` AS `created_by__id`,
  `submitted_`.`created_by` AS `created_by`
FROM some_table `submitted_`
LEFT JOIN user `created_by` ON `submitted_`.created_by = `created_by`.user_id
WHERE `submitted_`.`created_by` IN (39) +1ms
  join-monster
  join-monster SHAPE_DEFINITION
  join-monster  [ { id: 'id', created_by: 'created_by' } ] +0ms
  join-monster
  join-monster RAW DATA
  join-monster  [ TextRow {
    id: 1459,
    created_by__user_id: 39,
    created_by__id: 39,
    created_by: 39 },
  TextRow {
    id: 1464,
    created_by__user_id: 39,
    created_by__id: 39,
    created_by: 39 } ] +4ms
  join-monster 2 rows... +0ms
  join-monster
  join-monster SHAPED_DATA
  join-monster  [ { id: 1459, created_by: 39 }, { id: 1464, created_by: 39 } ] +2ms
@nicoabie
Copy link
Contributor

nicoabie commented May 5, 2024

Can you provide a https://github.com/join-monster/join-monster-sscce ? I believe this could still be an edge case.

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

No branches or pull requests

2 participants