join-monster / join-monster-graphql-tools-adapter

Use Join Monster to fetch your data with Apollo Server.
140 stars 20 forks source link

Join monster is not returning all the requested in the sql string it generates when using it with Apollo server 2.0. #20

Open abhirham opened 4 years ago

abhirham commented 4 years ago

Below is my app.js.

const express = require('express');
const app = express();
const {ApolloServer} = require('apollo-server-express');

const server = new ApolloServer({schema});
server.applyMiddleware({app, path: '/graphql'});

app.listen(4000,()=>console.log(`server started on port $4000}`));

Here is my schema:

const typeDefs = `

    input CustomersInput {
        EMAIL_ADDRESS: String
        NAME: String
        HOME_PHONE: String
        SPA_FOLIO_ID: ID
        ALL_CUSTOMER_ID: ID
    }

    type Customer {
        ALL_CUSTOMER_ID: ID
        NAME: String
        ALL_CUSTOMER_TYPE: String
        FIRST_NAME: String
    }

    type Query {
        customers(input: CustomersInput): [Customer]!
    }

    schema {
        query: Query
    }
`;

const resolvers = {
    Query: {
        customers(parent, args, ctx, resolveInfo) {
            return joinMonster.default(resolveInfo,ctx, async sql=>{
                console.log(sql)
                return knex.raw(sql); 
            });
        },
    },
}

const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
});

joinMonsterAdapt(schema, {
    Query: {
        fields: {
            customers: {
                where: (customerTable,args) => {
                    return escape(`${customerTable}.UPPER_FIRST_NAME || ' ' || ${customerTable}.UPPER_LAST_NAME || ' ' || ${customerTable}.UPPER_FIRST_NAME like %L`, `%${args.input.NAME.toUpperCase()}%`);
                },
            },
        }
    },
    Customer: {
        sqlTable: 'ALL_CUSTOMER',
        uniqueKey: 'ALL_CUSTOMER_ID',
    },
});

module.exports = schema;

Here is the query:

{
  customers(input:{NAME: "as"}){
    FIRST_NAME
    ALL_CUSTOMER_ID

  }
}

Here is the sql generated by joinmonster:

SELECT
  "customers"."ALL_CUSTOMER_ID" AS "ALL_CUSTOMER_ID"
FROM ALL_CUSTOMER "customers"
WHERE "customers".UPPER_FIRST_NAME || ' ' || "customers".UPPER_LAST_NAME || ' ' || "customers".UPPER_FIRST_NAME like '%AS%'

As you can see, I am requesting for FIRST_NAME , but joinmonster is not including it in the generated sql.

This is not the case when I use express-graphql . It works fine. Here is the link to my stackOverflow with all the details: https://stackoverflow.com/q/60868650/6425689

bcloutier commented 4 years ago

@abhirham I was having the same issue after I upgraded to Apollo Server 2.0. In order to fix it I had to add an explicit mapping from graphql field to sql column. Example:

{  
...
Customer: {
        sqlTable: 'ALL_CUSTOMER',
        uniqueKey: 'ALL_CUSTOMER_ID',
        fields: { 
                firstName: { sqlColumn: 'first_name' }
        }
    },
}