【发布时间】:2020-07-07 04:02:03
【问题描述】:
我正在 expressjs 上构建一个 graphql 服务器。下面是代码:
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}`));
这是我的架构:
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;
当我运行应用程序时,转到http://localhost:4000/graphql,并使用查询:
{
customers(input:{NAME: "as"}){
FIRST_NAME
ALL_CUSTOMER_ID
}
}
我回来了:
{
"data": {
"customers": [
{
"FIRST_NAME": null,
"ALL_CUSTOMER_ID": "563",
},
]
}
}
发生这种情况是因为当我查看 joinmonster 正在生成的 sql 查询时,它只请求客户 ID,如下所示:
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%'
当我运行完全相同的代码但使用 express-graphql 时,
const expressGraphQL = require('express-graphql');
app.use('/graphql', expressGraphQL({
schema,
graphiql: true
}))
这是连接怪物正在生成的查询:
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%'
一切都按预期进行。我错过了什么吗?
【问题讨论】:
标签: javascript express graphql-js apollo-server express-graphql