- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
最近我开始研究 GraphQL,我能够毫无问题地在平面模式中插入数据,但是当涉及到数据数组时,我遇到了类似的错误
{ "errors": [ { "message": "Must be input type" } ]}
我正在使用 postman 测试我的查询,我的变异查询是
mutation M {
AddEvent
(
title: "Birthday event"
description:"Welcome to all"
media:[{url:"www.google.com", mediaType:"image" }]
location:[{address:{state:"***", city:"****"}}]
)
{title,description,media,location,created,_id}}
这是我的事件架构:
EventType = new GraphQLObjectType({
name: 'Event',
description: 'A Event',
fields: () => ({
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
id: {
type: GraphQLString,
description: 'The id of the event.',
},
title: {
type: GraphQLString,
description: 'The title of the event.',
},
description: {
type: GraphQLString,
description: 'The description of the event.',
},
media:{
type:new GraphQLList(mediaType),
description:'List of media',
},
location:{
type:new GraphQLList(locationType),
description:'List of location',
}
})
});
// Media Type
export var mediaType = new GraphQLObjectType({
name: 'Media',
description: 'A Media',
fields: () => ({
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
url:{
type: GraphQLString,
description: 'The url of the event.',
},
mediaType:{
type: GraphQLString,
description: 'The mediaTypa of the event.',
}
})
});
// Location Type
export var locationType = new GraphQLObjectType({
name: 'Location',
description: 'A location',
fields: () => ({
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
address:{
type: GraphQLString,
description: 'The address.',
},
state:{
type: GraphQLString,
description: 'The state.',
},
city:{
type: GraphQLString,
description: 'The city.',
},
zip:{
type: GraphQLString,
description: 'The zip code.',
},
country:{
type: GraphQLString,
description: 'The country.',
}
})
});
Mongoose 架构:
var EventSchema = new mongoose.Schema({
title: {
required: true,
type: String,
trim: true,
match: /^([\w ,.!?]{1,100})$/
},
description: {
required: false,
type: String,
trim: true,
match: /^([\w ,.!?]{1,100})$/
},
media: [{
url: {
type: String,
trim: true
},
mediaType: {
type: String,
trim: true
}
}],
location: [{
address: {
type: String
},
city: {
type: String
},
state: {
type: String
},
zip: {
type: String
},
country: {
type: String
}
}]
})
变异类型:
addEvent: {
type: EventType,
args: {
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
title: {
type: GraphQLString,
description: 'The title of the event.',
},
description: {
type: GraphQLString,
description: 'The description of the event.',
},
media:{
type:new GraphQLList(mediaType),
description:'List of media',
},
location:{
type:new GraphQLList(locationType),
description:'List of media',
},
created: {
type: GraphQLInt,
description: 'The created of the user.',
}
},
resolve: (obj, {title,description,media,location,created,_id}) => {
let toCreateEvent = {
title,
description,
created:new Date(),
start: new Date(),
media,
location,
_id,
};
return mongo()
.then(db => {
return new Promise(
function(resolve,reject){
let collection = db.collection('events');
collection.insert(toCreateEvent, (err, result) => {
db.close();
if (err) {
reject(err);
return;
}
resolve(result);
});
})
});
}
}
最佳答案
您的问题是,当您定义突变时,所有类型都必须是输入类型,因此您会得到 “必须是输入类型”
的错误。所以在这里(来自你的突变):
media:{
type:new GraphQLList(mediaType),
description:'List of media',
},
location:{
type:new GraphQLList(locationType),
description:'List of media',
},
GraphQLList
、mediaType
和 locationType
必须是输入类型。
GraphQLList
已经是一种输入类型(请参阅此处 https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82 以查看被视为输入类型的 GraphQL 类型列表)。
但是您的类型 mediaType
和 locationType
属于 GraphQLObjectType
类型,这不是输入类型,但如果您查看输入列表再次输入:https://github.com/graphql/graphql-js/blob/master/src/type/definition.js#L74-L82 ,你会发现 GraphQLInputObjectType
这是一个对象输入类型,所以,你需要做的就是将 mediaType
和 locationType
替换为它们的 "输入”版本。
我建议做的是创建 mediaInputType
和 locationInputType
与 mediaType
和 locationType
但使用 new GraphQLInputObjectType({...
而不是 new GraphQLObjectType({...
创建并在您的突变中使用它们。
我遇到了同样的问题,我就这样解决了,如果您有任何问题,请随时发表评论。
关于node.js - 如何进行突变查询以在 GraphQL 中插入(数组)字段列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32304486/
下列说法正确的是: Javascript == Typescript Typescript != Javascript 对于 Dgraph 的 GraphQL+ 来说也是如此吗? GraphQL ==
我正在尝试通过使用 .graphql 文件并传递变量来对 Karate 进行测试。在我的 graphql 架构中,我试图重用另一个 .graphql 文件中的片段。我尝试按照 https://www.
从他们的文档中,它说: The leaf values of any request and input values to arguments are Scalars (or Enums) and
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 5 年前。 Improve
有没有一种技术可以让我在 GraphQL 中像这样声明 ADT? // TypeScript type SomeAlgebraicDataType = | { state: 'A', subSta
在基于 graphql API 构建新应用程序时,我们遇到了以下问题: 我们有一个带有输入字段的突变,其类型是具有自己验证规则的自定义标量(在这种情况下,输入是格式正确的电子邮件地址)。 在客户端,应
从语法上讲,您可以在模式中定义查询或变更,使其返回类型。 但是,操作定义(即客户端调用的查询或突变)必须有一个 SelectionSet,所以我必须这样做: mutation X { field }
我希望能听到这里专家的一些意见。我目前正在 NextJS 项目上工作,我的 graphql 正在运行在另一个 repo 中设置的模拟数据上。现在后端由其他开发人员构建,正在慢慢从模拟数据转向真实数据。
Graphql 中的架构和文档有什么区别? 架构是这样的: type Query { fo: String } 但文件是这样的: query SomeQuery { foo { ba
type Person { firstName: String!, lastName: String!, age: Int! } 如何查询所有 18 岁以上的人? 最佳答案 这
有没有办法提供 GraphQL Schema 设计的可视化图表(类似于 UML)? 背景: 我已经有了一个架构设计,要转换成 GraphQL API。但是,在开始 GraphQL 开发之前,我想创建我
我想了解 GraphQL 的(Java)实现是否足够智能,如果在其中一个提取器的执行期间抛出异常,可以取消预定的数据提取? 例如,我运行一个查询来检索客户的所有订单。假设客户有 100 个订单。这意味
我是graphql的新手,但是我在努力查询。 我想通过他们的电子邮件地址返回用户 我有一个类型定义的调用V1User,它具有以下字段 ID, 电子邮件, 密码, 角色 要根据电子邮件返回用户,此查询中
我将GraphQL包装器放在现有的REST API上,如Zero to GraphQL in 30 minutes中所述。我有一个产品的API端点,该端点具有一个指向嵌套对象的属性: // API R
在 GraphQL 中,空格似乎很重要,因为它分隔 token : { human(id: "1000") { name height } } 然而,spec says那个空格
我正在尝试使用带有属性的 sequelize 获取数据并将其传递给 graphql。 结果在控制台中很好,但 graphql 查询为属性字段返回 null。 我的解析器 getUnpayedL
有没有办法在 graphql 查询中生成静态值? 例如,假设我有一个 user具有名称和电子邮件字段的对象。出于某种原因,我总是希望用户的状态为“已接受”。我怎样才能写一个查询来完成这个? 我想做的事
我已关注 the documentation about using graphql-tools to mock a GraphQL server ,但是这会引发自定义类型的错误,例如: Expect
我今天在生产中有以下 graphql 模式定义: type BasketPrice { amount: Int! currency: String! } type BasketItem {
像这样的graphql模式: type User { id: ID! location: Location } type Location { id: ID! user: User }
我是一名优秀的程序员,十分优秀!