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

标签 express graphql apollo-server

我正在使用 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/

相关文章:

graphql - 弃用 ApolloServer 中的类型

mysql - 从 sequelize 查询返回一个值

node.js - 针对经过身份验证的用户的 Express 静态

javascript - 如何使用nodejs和expressjs在mysql上显示数据

windows - 安卓上的 Apollo : can't download schema or generate classes

graphql - 如何在 graphql dotnet 中使用 pascal 大小写代替 Camel 大小写

javascript - 如果我在 Apollo Client 中使用 dataIdFromObject 设置了唯一标识符,是否需要 graphql 的 ID 类型

node.js - 是否可以使用 Apollo Server 在服务器端本地执行突变或查询

express - 如何使用 graphql 和 passport 设置身份验证但仍然使用 Playground

json - 使用 node express 代理 json 请求