我很困惑如何在graphql中正确地创建一个解析器:我有两个相关的实体: entityGroup和entity。每次创建一个EntityGroup.createOne,时,我都要创建一个默认实体:所以我需要调用entityGroup的解析方法,然后使用结果的id调用"Entity.createOne“
这是我到目前为止编写的代码:
import { composeWithMongoose } from 'graphql-compose-mongoose';
import { schemaComposer } from 'graphql-compose';
import {Resolver} from 'graphql-compose'
const customizationOptions = {}; // left it empty for simplicity, described below
const EntityTC = composeWithMongoose(Entity, customizationOptions)
const EntityGroupTC = composeWithMongoose(EntityGroup, customizationOptions)
const entityGroupCreate = new Resolver({
name: 'entityGroupCreate',
type: EntityGroupTC,
args: {
name: 'String!',
},
resolve: async ({ source, args, context, info }) => {
const created = await EntityGroupTC.getResolver('createOne').resolve({ source, args, context, info })
console.log("created entity : ", created)
return created
}
});
schemaComposer.rootMutation().addFields({
entityGroupCreate,
}现在,在客户端,我调用了与我在原始情况下使用的代码相同的代码,其中entityGroupCreate使用了预preexisiting解析器:
schemaComposer.rootMutation().addFields({
entityGroupCreate: EntityGroupTC.getResolver('createOne'),
}我的问题是,对于预定义的解析器,一切都很好,但是使用上面描述的解析器,我得到了以下错误:
graphQl错误:"entityGroupCreate“类型的”突变“字段中的未知参数”记录“。graphQl错误:无法查询"EntityGroup“类型上的字段"recordId”。graphQl错误:无法查询"EntityGroup“类型上的字段"record”。graphQl错误:字段"entityGroupCreate“参数"name”类型为"String!“是必需的,但没有提供。
这是我的查询
const ADD_COMPLAINT = gql`mutation complaintCreate($entityId:String!, $title: String!, $desc: String!)
{
complaintCreate(record:{entityId:$entityId, title:$title, desc:$desc}){
recordId,
record{
_id,
entityId,
user {
userId,
userName,
roleInShop
},
title,
desc,
createdAt,
updatedAt
}
}
}`现在我知道突变模式是错误的,但是我真的不知道从哪里开始,因为这个模式是由graphql-组合-mongoose构造的,我可以简单地在解析器的类型字段中命名它: type : EntityGroupTC。
我试图重新定义注释中指定的响应格式:
const outputType = EntityGroupTC.constructor.schemaComposer.getOrCreateTC("entityGroupCreate", t => {
t.addFields({
recordId: {
type: 'MongoID',
description: 'Created document ID',
},
record: {
type: EntityGroupTC,
description: 'Created document',
},
});
});但我仍然有这些错误
graphQl错误:"entityGroupCreate“类型的”突变“字段中的未知参数”记录“。graphQl错误:字段"entityGroupCreate“参数"name”类型为"String!“是必需的,但没有提供。
所以我必须了解这个部分是如何工作的:https://github.com/graphql-compose/graphql-compose-mongoose/blob/master/src/resolvers/createOne.js:42
args: {
...recordHelperArgs(tc, {
recordTypeName: `CreateOne${tc.getTypeName()}Input`,
removeFields: ['id', '_id'],
isRequired: true,
...(opts && opts.record),
}),
},在我看来,对于一个应该编写较少布线代码的库来说,这将变得越来越复杂:我确信我这样做是错误的……
诚挚的问候,
发布于 2018-10-23 19:16:39
好的,解决办法就在我面前:/,这是一天的症候群的结束。
const createOneResolver = EntityGroupTC.getResolver('createOne')
const entityGroupCreate = new Resolver({
name: 'entityGroupCreate',
type: createOneResolver.type,
args: createOneResolver.args,
resolve: async ({ source, args, context, info }) => {
const created = await createOneResolver.resolve({ source, args, context, info })
console.log("created entity : ", created)
return created
}
});https://stackoverflow.com/questions/52955662
复制相似问题