gpt4 book ai didi

express - 突变上的未定义参数,使用 apollo-server

转载 作者:行者123 更新时间:2023-12-04 02:48:31 26 4
gpt4 key购买 nike

我正在使用 apollo-server,一切都按预期工作,但是当从前端调用突变时,突变参数未定义。

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")
}

最佳答案

解析器签名如下:(parent, args, context, info)在哪里:

  • parent: The object that contains the result returned from the resolver on the parent field, or, in the case of a top-level Query field, the rootValue passed from the server configuration. This argument enables the nested nature of GraphQL queries.
  • args: An object with the arguments passed into the field in the query. For example, if the field was called with query{ key(arg: "you meant") }, the args object would be: { "arg": "you meant" }.
  • context: This is an object shared by all resolvers in a particular query, and is used to contain per-request state, including authentication information, dataloader instances, and anything else that should be taken into account when resolving the query. Read this section for an explanation of when and how to use context.
  • info: This argument contains information about the execution state of the query, including the field name, path to the field from the root, and more. It's only documented in the GraphQL.js source code, but is extended with additional functionality by other modules, like apollo-cache-control.


参数作为第二个参数传递给解析器,而不是第一个。见 the docs了解更多详情。

关于express - 突变上的未定义参数,使用 apollo-server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56154360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com