Undefined args on a mutation, using apollo-server

南楼画角 提交于 2020-01-22 03:27:04

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/56154360/undefined-args-on-a-mutation-using-apollo-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!