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

本文是小编为大家收集整理的关于使用apollo-server,在突变中未定义的args的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

IM与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(电子邮件:" test@mail.com" textarea:" testing textarea" createat:" 19-05-2018") }

推荐答案

解析器签名如下:(parent, args, context, info)其中:

  • parent:包含从父字段上的解析器返回的结果,或者在顶级查询字段的情况下,rootValue从服务器配置传递.该参数启用GraphQl查询的嵌套性质.
  • args:一个带有参数的对象传递到查询中的字段.例如,如果使用查询调用该字段{key(arg:'you trage')},则args对象为:{" arg":" you the}.
  • 上下文:这是所有解析器在特定查询中共享的对象,用于包含每次要求状态,包括身份验证信息,数据加载器实例,以及在解决查询时应考虑的其他内容.阅读本节以说明何时以及如何使用上下文.
  • 信息:此参数包含有关查询的执行状态的信息,包括字段名称,从根目录的路径等等.它仅在GraphQl.js源代码中进行了记录,但通过其他模块(例如Apollo-Cache-Control.)扩展了其他功能.

这些参数以 second 参数而不是 first 而传递给分辨率.请参阅文档有关其他详细信息.

本文地址:https://www.itbaoku.cn/post/1938149.html

问题描述

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

This is what i send from /graphql mutation { createMsg(email: "test@mail.com" textarea: "testing textarea" createdAt: "19-05-2018") }

推荐答案

The resolver signature is as follows: (parent, args, context, info) where:

  • 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 arguments are passed to the resolver as the second parameter, not the first. See the docs for additional details.