突变上的未定义参数,使用 apollo-server [英] Undefined args on a mutation, using apollo-server
问题描述
我正在使用 apollo-server,一切都按预期进行,但是当从前端调用突变时,未定义突变参数.
Im working with apollo-server, everything works as expetected but the mutation arguments are undefined when the mutation is called from the frontend.
const express = require('express');
const morgan = require('morgan');
const { ApolloServer, gql } = require('apollo-server-express');
const mongoose = require('mongoose');
require('dotenv').config();
const app = express();
const typeDefs = gql`
type msgFields {
email: String!
textarea: String!
createdAt: String!
}
input MsgFieldsInput {
email: String!
textarea: String!
createdAt: String!
}
type Query {
formContact: msgFields!
}
type Mutation {
createMsg(email: String!, textarea: String!, createdAt: String!): String!
}
`;
const resolvers = {
Query: {
formContact: () => {
return {
email: 'test@mail.com',
textarea: 'checking Checking checking Checking checking Checking'
}
}
},
Mutation: {
createMsg: (args) => {
console.log(args); // => undefined here
return 'Worked';
}
}
}
const server = new ApolloServer({
typeDefs,
resolvers
});
app.use(morgan('dev'));
server.applyMiddleware({app})
mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true })
.then(() => {
app.listen({port: 4000}, () => {
console.log(`Server and DB ready at http://localhost:4000${server.graphqlPath}`)
});
})
.catch(err => {
throw err;
})
这是我从/graphql 发送的变异{createMsg(email: "test@mail.com" textarea: "testing textarea" createdAt: "19-05-2018")}
This is what i send from /graphql mutation { createMsg(email: "test@mail.com" textarea: "testing textarea" createdAt: "19-05-2018") }
推荐答案
解析器签名如下:(parent, args, context, info)
其中:
The resolver signature is as follows: (parent, args, context, info)
where:
- parent:包含从父字段的解析器返回的结果的对象,或者,在顶级查询字段的情况下,从服务器配置传递的 rootValue.此参数启用 GraphQL 查询的嵌套性质.
- args:带有传入查询字段的参数的对象.例如,如果使用 query{ key(arg: "you mean") } 调用该字段,则 args 对象将是:{ "arg": "you mean" }.
- context:这是特定查询中所有解析器共享的对象,用于包含每个请求的状态,包括身份验证信息、数据加载器实例以及在解析查询时应考虑的任何其他内容.阅读本节以了解何时以及如何使用上下文.
- info:该参数包含有关查询执行状态的信息,包括字段名称、从根到字段的路径等.它仅记录在 GraphQL.js 源代码中,但通过其他模块(如 apollo-cache-control)扩展了附加功能.
参数作为第二个参数传递给解析器,而不是第一个.有关其他详细信息,请参阅文档.
The arguments are passed to the resolver as the second parameter, not the first. See the docs for additional details.
这篇关于突变上的未定义参数,使用 apollo-server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!