使用apollo-server的突变未定义的args [英] Undefined args on a mutation, using apollo-server

查看:118
本文介绍了使用apollo-server的突变未定义的args的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用apollo服务器,一切都按预期进行,但是当从前端调用突变时,突变参数是不确定的.

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(电子邮件:"test@mail.com"文本区域:测试文本区域" createdAt:"2018年5月19日") }

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("arg)",你的意思)}来调用该字段,则args对象将是:{" arg:"你的意思}.
  • 上下文:这是特定查询中所有解析程序共享的对象,用于包含每个请求的状态,包括身份验证信息,数据加载器实例以及解析该查询时应考虑的任何其他内容.阅读本节以了解何时以及如何使用上下文.
  • info:此参数包含有关查询的执行状态的信息,包括字段名称,从根到字段的路径等.它仅在GraphQL.js源代码中进行了记录,但其他模块(如apollo-cache-control)对其进行了附加功能扩展.
  • 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.

参数作为 second 参数而不是 first 传递给解析器.有关其他详细信息,请参见文档.

The arguments are passed to the resolver as the second parameter, not the first. See the docs for additional details.

这篇关于使用apollo-server的突变未定义的args的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆