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

joinMonster creating invalid MySQL sql #486

Closed
imjoshin opened this issue Jul 31, 2022 · 2 comments
Closed

joinMonster creating invalid MySQL sql #486

imjoshin opened this issue Jul 31, 2022 · 2 comments
Assignees

Comments

@imjoshin
Copy link

Description

I have a very simple schema, as I'm testing out joinMonster for new project. When calling on joinMonster, it creates invalid SQL.

SELECT
  "cards"."id" AS "id",
  "cards"."name" AS "name"
FROM card "cards"

Reproduction

Detail the steps taken to reproduce this error, what was expected, and whether this issue can be reproduced consistently or if it is intermittent.

I have the following schema:

CREATE TABLE IF NOT EXISTS card (
  id VARCHAR(32) PRIMARY KEY NOT NULL,
  name TEXT NOT NULL,
  setId VARCHAR(64),
  CONSTRAINT FK_setId FOREIGN KEY (setId) REFERENCES 'set'(id)
)

CREATE TABLE IF NOT EXISTS 'set' (
  id VARCHAR(64) PRIMARY KEY NOT NULL,
  fullName VARCHAR(128) NOT NULL,
  shortName VARCHAR(8) NOT NULL
)

I'm setting up the graphql object as:

const GraphQLSet = new graphql.GraphQLObjectType({
  name: `Set`,
  extensions: {
    joinMonster: {
      sqlTable: `set`,
      uniqueKey: `id`,
    }
  },
  fields: () => ({
    id: { type: graphql.GraphQLString },
    fullName: { type: graphql.GraphQLString },
    shortName: { type: graphql.GraphQLString },
  })
})

const GraphQLCard = new graphql.GraphQLObjectType({
  name: `Card`,
  extensions: {
    joinMonster: {
      sqlTable: `card`,
      uniqueKey: `id`,
    }
  },
  fields: () => ({
    id: { type: graphql.GraphQLString },
    name: { type: graphql.GraphQLString },
    set: {
      type: GraphQLSet,
      sqlJoin: (myTable: string, parentTable: string) => `${myTable}.id = ${parentTable}.setId`
    }
  })
})

const QueryRoot = new graphql.GraphQLObjectType({
  name: `Query`,
  fields: () => ({
    cards: {
      type: new graphql.GraphQLList(GraphQLCard),
      resolve: (parent, args, context, resolveInfo) => {
        return joinMonster(resolveInfo, {}, (sql: string) => {
          return db.run(sql)
        })
      }
    },
  })
})
    
const executableSchema = new graphql.GraphQLSchema({ query: QueryRoot })

When I issue this query:

{
  cards {
    id
    name
  }
}

The resulting SQL is:

SELECT
  "cards"."id" AS "id",
  "cards"."name" AS "name"
FROM card "cards"

It should result in something like:

SELECT
  `card`.`id` AS "id",
  `card`.`name` AS "name"
FROM card

Environment

  • Version of this library used: 3.1.1
  • Version of the platform or framework used, if applicable: Node 18
  • Other relevant versions (language, server software, OS, browser): Ubuntu 18, WSL2
  • Other modules/plugins/libraries that might be involved: graphql@15.3.0
@nicoabie
Copy link
Contributor

nicoabie commented May 4, 2024

Hi @imjoshin can you create a repro with this? https://github.com/join-monster/join-monster-sscce

I'd love to take a look here!

@nicoabie
Copy link
Contributor

SQL is totally valid, see here join-monster/join-monster-sscce#2

@nicoabie nicoabie self-assigned this May 13, 2024
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