我的GraphQL模式如下:
type Outer {
id: ID
name: String,
inner: [Inner]
}
type Inner {
id: ID
name: String
}
input OuterInput {
name: String,
inner: InnerInput
}
Input InnerInput {
name: String
}
type Query {
getOuter(id: ID): Outer
}
type Mutation {
createOuter(outer: OuterInput): Outer
}
在数据库中,外部对象的存储方式如下:
{
id: 1,
name: ‘test’,
inner: [5, 6] //IDs of inner objects. Not the entire inner object
}
外部和内部存储在单独的数据库集合/表中(我正在使用DynamoDB)
我的解析器如下:
Query: {
getOuter: (id) => {
//return ‘Outer’ object with the ‘inner’ IDs from the database since its stored that way
}
}
Outer: {
inner: (outer) => {
//return ‘Inner’ objects with ids from ‘outer.inner’ is returned
}
}
Mutation: {
createOuter: (outer) => {
//extract out the IDs out of the ‘inner’ field in ‘outer’ and store in DB.
}
}
OuterInput: {
inner: (outer) => {
//Take the full ‘Inner’ objects from ‘outer’ and store in DB.
// This is the cause of the error
}
}
但是,我无法像在“外部”中为“外部输入”中的“内部”字段编写解析器一样。 GraphQL抛出错误提示
Error: OuterInput was defined in resolvers, but it's not an object
如果不允许为输入类型编写解析器,该如何处理?
答案 0 :(得分:0)
解析器对于输入类型不是必需的。输入类型仅作为值传递给字段参数,这意味着它们的值已经知道了-不需要由服务器解析,这与查询中返回的字段的值不同。
每个解析器的签名都是相同的,并包含4个参数:1)父对象,2)要解析的字段的参数,3)上下文,以及4)一个包含有关请求的更详细信息的对象,整个。
因此,请访问您的outer
突变的createOuter
参数:
Mutation: {
createOuter: (root, args, ctx, info) => {
console.log('Outer: ', args.outer)
console.log('Outer name: ', args.outer.name)
console.log('Inner: ', args.outer.inner)
console.log('Inner name: ', args.outer.inner.name)
// resolver logic here
}
}