I am building a graphql server on expressjs. Below is the code:
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;
When I run the app, and go to http://localhost:4000/graphql
, and use the query:
{
customers(input:{NAME: "as"}){
FIRST_NAME
ALL_CUSTOMER_ID
}
}
I get back:
{
"data": {
"customers": [
{
"FIRST_NAME": null,
"ALL_CUSTOMER_ID": "563",
},
]
}
}
This is happening because when I look at the sql query which joinmonster is generating, it is only requesting for customer id and nothing else as seen below:
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%'
When I run the exact same code but use express-graphql
instead,
const expressGraphQL = require('express-graphql');
app.use('/graphql', expressGraphQL({
schema,
graphiql: true
}))
This is the query which join monster is generating:
SELECT
"customers"."ALL_CUSTOMER_ID" AS "ALL_CUSTOMER_ID",
"customers"."FIRST_NAME" AS "FIRST_NAME"
FROM ALL_CUSTOMER "customers"
WHERE "customers".UPPER_FIRST_NAME || ' ' || "customers".UPPER_LAST_NAME || ' ' || "customers".UPPER_FIRST_NAME like '%AS%'
And everything works as expected. Am I missing something?