当前位置: 首页 > 知识库问答 >
问题:

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

詹钊
2023-03-14

我很困惑如何创建一个Resolver正确地在图ql-组成:我有两个相关的实体:entityGroup和实体。我想在每次创建实体组时创建一个默认实体:所以我需要调用EntityG的解析方法roup.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错误:类型为“突变”的字段“entityGroupCreate”上的未知参数“record”。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
            }
        }
  }`

现在我明白了突变模式是错误的,但是我真的不知道从哪里开始,因为这个模式是由graql-compose-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”上的未知参数“record”。graphQl错误:字段“entityGroupCreate”参数“name”类型为“String!”是必需的,但未提供。

因此,我必须了解这部分的工作html" target="_blank">原理: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),
      }),
    },

在我看来,对于一个应该写更少接线代码的库来说,这变得越来越复杂:我肯定我做错了...

最好的问候,

共有1个答案

朱渝
2023-03-14

好吧,解决方案就在我面前:/,这是一天结束的综合征

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
    }
});
 类似资料:
  • 我有一个名为汽车制造商的GraphQL模式,它有两个字段,< code>id和< code>name。我试图通过GraphiQL完成的是将几个数据插入到模式中,但我需要改变这两个提到的字段。 有没有办法在字段上插入所需的值?

  • 假设我有一个直接转换db表的case类,在创建新行时将随机生成id。 现在查看这些测试,我需要从测试中检索一行,并将其与 但是我没有第一个参数,因为它是随机创建的,我想以某种方式忽略它... 我试过了 但这是无效的。 有什么方法可以在Scala中忽略case类参数吗? 谢谢

  • 我正在尝试创建一个通用类型安全方法来逐行获取一些字段,如下所示: 虽然它工作正常,但问题是我不能在这里提供字段类型作为第二个泛型参数: 因为它只知道字段本身,所以可能会导致堆污染问题。 我很乐意将我的签名改为: 但是只接受其类型作为泛型参数,而不是记录类型。 在JOOQ代码库中,字段类是否有绑定到其记录类型的子/父类? 谢谢

  • 问题内容: 我从Hibernate开始,但有一个我无法弄清的错误。 我有以下课程: 那我有 最后: 我得到的错误是: 错误引发的唯一提示是该错误在此处的主类中: 这些id是字符串,我将它们标注为@GeneratedValue(我认为我不需要自己初始化它们)。关系@OneToMany具有级联注释,因此应正确映射。 我已经尝试了没有session.save行的代码,并且不会抛出错误,因此代码本身不是问

  • 问题总结:我想传递一个带有类型参数(如