In the process of creating a graphql server using expressjs, I have implemented the following code snippet:
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}`));
This is the schema definition that I am working with:
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;
Upon running the application and accessing http://localhost:4000/graphql
, along with executing the provided query:
{
customers(input:{NAME: "as"}){
FIRST_NAME
ALL_CUSTOMER_ID
}
}
The resulting data looks like this:
{
"data": {
"customers": [
{
"FIRST_NAME": null,
"ALL_CUSTOMER_ID": "563",
},
]
}
}
However, after inspecting the generated SQL query by joinmonster, it appears to only fetch the customer id without any additional fields requested, as shown 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 switching to use express-graphql
instead of ApolloServer, the generated query retrieves all the necessary fields as expected:
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%'
It seems to work smoothly with express-graphql
. Is there something I might be overlooking?