为什么我不能解构 'input',我怎样才能得到我在请求中传递的 "title"?
Why i cannot destructure 'input' and how can i get "title" which i pass in request?
声明类型定义
const typeDefs = gql`
type Todo {
_id: ID,
title : String,
done : Boolean
}
type QueryTodo {
todos : [Todo]
}
input TodoInput {
title : String
}
type Query {
todos : [Todo]
}
type Mutation{
createTodo(input: TodoInput): Todo
}
`;
export {typeDefs}
然后在解析器中我声明了密钥突变。
下面是我的解析器代码
import {Todo} from "../../DB/Models/Todo";
const resolvers = {
Query:{
todos:async()=>{
return await Todo.find()
}
},
Mutation: {
createTodo : ({input}) => {
console.log(input)
return {_id:"12312321",title:input,done:false}
}
}
}
export {resolvers}
当我尝试发出请求时出现错误
mutation {
createTodo(input: {
title:"string"
}) {
_id
}
}
我得到的回复是
“错误”:[
{
"message": "无法解构 'undefined' 的 属性 'input',因为它未定义。",
“地点”:[
{
“线”:2,
“列”:3
}
],
“小路”: [
“创建待办事项”
],
“扩展”:{
“代码”:“INTERNAL_SERVER_ERROR”,
“例外”: {
“堆栈跟踪”:[
如何获取我在请求中传递的参数“title”
参见https://graphql.org/learn/execution/#root-fields-resolvers:
A resolver function receives four arguments:
obj
The previous object, which for a field on the root Query type is often not used.
args
The arguments provided to the field in the GraphQL query.
context
A value which is provided to every resolver and holds important contextual information like the currently logged in user, or
access to a database.
info
A value which holds field-specific information relevant to the current query as well as the schema details
第一个参数将是 Mutation
解析器架构的 root
值 - 如果您没有通过任何解析器,显然 undefined
。要访问(和解构)查询传递的参数,请使用第二个参数。
声明类型定义
const typeDefs = gql`
type Todo {
_id: ID,
title : String,
done : Boolean
}
type QueryTodo {
todos : [Todo]
}
input TodoInput {
title : String
}
type Query {
todos : [Todo]
}
type Mutation{
createTodo(input: TodoInput): Todo
}
`;
export {typeDefs}
然后在解析器中我声明了密钥突变。 下面是我的解析器代码
import {Todo} from "../../DB/Models/Todo";
const resolvers = {
Query:{
todos:async()=>{
return await Todo.find()
}
},
Mutation: {
createTodo : ({input}) => {
console.log(input)
return {_id:"12312321",title:input,done:false}
}
}
}
export {resolvers}
当我尝试发出请求时出现错误
mutation {
createTodo(input: {
title:"string"
}) {
_id
}
}
我得到的回复是
“错误”:[ { "message": "无法解构 'undefined' 的 属性 'input',因为它未定义。", “地点”:[ { “线”:2, “列”:3 } ], “小路”: [ “创建待办事项” ], “扩展”:{ “代码”:“INTERNAL_SERVER_ERROR”, “例外”: { “堆栈跟踪”:[
如何获取我在请求中传递的参数“title”
参见https://graphql.org/learn/execution/#root-fields-resolvers:
A resolver function receives four arguments:
obj
The previous object, which for a field on the root Query type is often not used.args
The arguments provided to the field in the GraphQL query.context
A value which is provided to every resolver and holds important contextual information like the currently logged in user, or access to a database.info
A value which holds field-specific information relevant to the current query as well as the schema details
第一个参数将是 Mutation
解析器架构的 root
值 - 如果您没有通过任何解析器,显然 undefined
。要访问(和解构)查询传递的参数,请使用第二个参数。