这是我第一次尝试 GraphQl,我在 Playground 上遇到了这个错误:
{
"errors": [
{
"message": "TypeError: Cannot read property 'PaymentInfo' of undefined",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"createCreditCard"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"Error: TypeError: Cannot read property 'PaymentInfo' of undefined",
" at _callee3$ (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/src/resolvers/payment.js:18:23)",
" at tryCatch (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/regenerator-runtime/runtime.js:62:40)",
" at Generator.invoke [as _invoke] (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/regenerator-runtime/runtime.js:288:22)",
" at Generator.prototype.(anonymous function) [as next] (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/regenerator-runtime/runtime.js:114:21)",
" at asyncGeneratorStep (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/src/resolvers/payment.js:8:103)",
" at _next (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/src/resolvers/payment.js:10:194)",
" at /Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/src/resolvers/payment.js:10:364",
" at new Promise (<anonymous>)",
" at /Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/src/resolvers/payment.js:10:97",
" at createCreditCard (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/src/resolvers/payment.js:11:25)",
" at field.resolve (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/graphql-extensions/src/index.ts:274:18)",
" at resolveFieldValueOrError (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/graphql/execution/execute.js:467:18)",
" at resolveField (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/graphql/execution/execute.js:434:16)",
" at /Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/graphql/execution/execute.js:244:18",
" at /Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/graphql/jsutils/promiseReduce.js:23:10",
" at Array.reduce (<anonymous>)",
" at promiseReduce (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/graphql/jsutils/promiseReduce.js:20:17)",
" at executeFieldsSerially (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/graphql/execution/execute.js:241:37)",
" at executeOperation (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/graphql/execution/execute.js:219:55)",
" at executeImpl (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/graphql/execution/execute.js:104:14)",
" at Object.execute (/Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/graphql/execution/execute.js:64:35)",
" at /Users/milano/Documents/Projects/mtg-shop-v1/mtg-shop-server/node_modules/apollo-server-core/src/requestPipeline.ts:464:30"
]
}
}
}
],
"data": null
}
我想做的就是使用 Playground 在我的数据库(我使用 postgres)上创建一个新的“PaymentInfo”。
我的突变:
mutation {
createCreditCard(cvv: 165, cardNumber: 123654376, isValid: true) {
id
}
}
我的解析器:
Mutation: {
createCreditCard: async (parent, { me, models }) => {
try {
return await models.PaymentInfo.create({
paymentInfos,
userId: me.id,
});
} catch (error) {
throw new Error(error);
}
},
我的架构:
createCreditCard(
cardNumber: Int!
cvv: Int!
isValid: Boolean!
): PaymentInfo!
deleteCreditCard(id: ID!): Boolean!
}
没有加起来的是,我可以通过代码(https://sequelize.org/master/manual/getting-started.html#querying)使用sequelize查询创建任何东西,但是当涉及到在 Playground 上使用突变时,我根本不能。
我确信我在 Playground 上的突变语法上做错了什么,但我不知道是什么。
谢谢
最佳答案
您的解析器似乎有问题。 Graphql 解析器传递四个参数 - obj、args、context、info。我猜测 { me, models }
是试图解构您的上下文,但可能存在问题,因为您没有考虑或访问介入的 args 对象,您需要访问该对象createCreditCard
变量(事实上,没有看到 paymentInfos 的定义位置)。也许可以尝试以下方法:
Mutation: {
createCreditCard: async (parent, args, { me, models }) => {
try {
const paymentInfos = { ...args };
console.log(paymentInfos); // confirm you are passing object of correct shape to db
return await models.PaymentInfo.create({
paymentInfos,
userId: me.id,
});
} catch (error) {
throw new Error(error);
}
},
关于javascript - 类型错误 : Cannot read propety of undefined. Apollo Server 和 Sequelize 的 GraphQL 突变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57940163/