繁体   English   中英

类型为“突变”(graphql-compose)的字段上的未知参数“记录”

[英]Unknown argument “record” on field of type “Mutation” (graphql-compose)

我对如何在graphql-compose中正确创建解析器感到困惑:我有两个相关的实体:EntityGroup和Entity。 我想每一个entityGroup创建时间创建一个默认的实体:所以我需要调用EntityGroup.createOne的解决方法,然后使用该结果的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使用了预先存在的解析器:

schemaComposer.rootMutation().addFields({
    entityGroupCreate: EntityGroupTC.getResolver('createOne'),
}

我的问题是,对于预定义的解析器来说一切正常,但是在上面描述的解析器中,出现了此错误:

graphQl错误:类型为“ Mutation”的字段“ entityGroupCreate”上的未知参数“ record”。 graphQl错误:无法查询类型“ EntityGroup”上的字段“ recordId”。 graphQl错误:无法查询“ EntityGroup”类型的字段“ record”。 graphQl错误:类型为“字符串!”的字段“ entityGroupCreate”参数“名称” 是必需的,但未提供。

这是我的查询

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-compose-mongoose构造的,我可以简单地将其命名为resolver的type字段: 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错误:类型为“ Mutation”的字段“ entityGroupCreate”上的未知参数“ record”。 graphQl错误:类型为“字符串!”的字段“ entityGroupCreate”参数“名称” 是必需的,但未提供。

所以我必须了解这部分是如何工作的: 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),
      }),
    },

在我看来,对于一个应该编写较少接线代码的库来说,这注定会变得复杂:我敢肯定我做错了方法...

最好的祝福,

好的,解决方案就在我眼前:/,这是一天结束syndrom

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM