简体   繁体   English

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

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

I'm confused about how to create a Resolver correctly in graphql-compose: I have two related entities : entityGroup and entity. 我对如何在graphql-compose中正确创建解析器感到困惑:我有两个相关的实体:EntityGroup和Entity。 I want to create a default entity each time an entityGroup is created: so I need to call the resolve method of EntityGroup.createOne , then use that result's id to call "Entity.createOne" 我想每一个entityGroup创建时间创建一个默认的实体:所以我需要调用EntityGroup.createOne的解决方法,然后使用该结果的ID叫“Entity.createOne”

This is the code I wrote so far: 这是我到目前为止编写的代码:

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,
}

Now from the client side, I call the same code that I was using for the raw case where entityGroupCreate used the preexisiting resolver: 现在,从客户端,我调用了与原始情况相同的代码,其中EntityGroupCreate使用了预先存在的解析器:

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

My issue is that everything works fine for the predefined resolver, but with my resolver I described above I get this error: 我的问题是,对于预定义的解析器来说一切正常,但是在上面描述的解析器中,出现了此错误:

graphQl error : Unknown argument "record" on field "entityGroupCreate" of type "Mutation". graphQl错误:类型为“ Mutation”的字段“ entityGroupCreate”上的未知参数“ record”。 graphQl error : Cannot query field "recordId" on type "EntityGroup". graphQl错误:无法查询类型“ EntityGroup”上的字段“ recordId”。 graphQl error : Cannot query field "record" on type "EntityGroup". graphQl错误:无法查询“ EntityGroup”类型的字段“ record”。 graphQl error : Field "entityGroupCreate" argument "name" of type "String!" graphQl错误:类型为“字符串!”的字段“ entityGroupCreate”参数“名称” is required but not provided. 是必需的,但未提供。

this is my query 这是我的查询

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
            }
        }
  }`

Now I understand that the mutation schema is wrong, but I really don't know where to start since that schema is constructed by graphql-compose-mongoose, and I touhght I can simply name it in the type field of the resolver : type: EntityGroupTC 现在我知道突变模式是错误的,但是我真的不知道从哪里开始,因为该模式是由graphql-compose-mongoose构造的,我可以简单地将其命名为resolver的type字段: type: EntityGroupTC

I attempted to redefine the response format as specified in the comment: 我试图重新定义注释中指定的响应格式:

const outputType = EntityGroupTC.constructor.schemaComposer.getOrCreateTC("entityGroupCreate", t => {
    t.addFields({
        recordId: {
            type: 'MongoID',
            description: 'Created document ID',
        },
        record: {
            type: EntityGroupTC,
            description: 'Created document',
        },
    });
});

but I still have these errors 但我仍然有这些错误

graphQl error : Unknown argument "record" on field "entityGroupCreate" of type "Mutation". graphQl错误:类型为“ Mutation”的字段“ entityGroupCreate”上的未知参数“ record”。 graphQl error : Field "entityGroupCreate" argument "name" of type "String!" graphQl错误:类型为“字符串!”的字段“ entityGroupCreate”参数“名称” is required but not provided. 是必需的,但未提供。

So I have to understand how this part works : https://github.com/graphql-compose/graphql-compose-mongoose/blob/master/src/resolvers/createOne.js:42 所以我必须了解这部分是如何工作的: 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),
      }),
    },

In my opinion this is staring to get complicated for a library that is supposed to write less wiring code : I'm sure I'm doing it the wrong way... 在我看来,对于一个应该编写较少接线代码的库来说,这注定会变得复杂:我敢肯定我做错了方法...

Best regards, 最好的祝福,

Ok the solution was just in front of me :/, this is the end of the day syndrom 好的,解决方案就在我眼前:/,这是一天结束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.

相关问题 graphql-compose:需要输入类型的参数 - graphql-compose: require argument that's an input type graphql-compose,嵌套字段的触发解析器 - graphql-compose, trigger resolver of a nested field “突变”类型的字段上的未知参数 - Unknown argument on field of type “Mutation” Angular Apollo:错误:GraphQL 错误:“Mutation”类型的“AuthLogin”字段上的未知参数“用户名” - Angular Apollo: Error: GraphQL error: Unknown argument “username” on field “AuthLogin” of type “Mutation” 类型为“ Mutation \\”的字段“ forgotPassword \\”上的“未知参数\\” email \\”。” - “Unknown argument \”email\“ on field \”forgotPassword\“ of type \”Mutation\“.” 如何在graphql-compose中使用自定义输入类型? - How to use custom input type in graphql-compose? 突变错误,类型为“ Mutation \\”的字段“ createProduct”上的未知参数“ barcode”-django - mutation error, Unknown argument \“barcode\” on field \“createProduct\” of type \“Mutation\” - django 如何在graphql-compose中扩展模式 - How to extends schema in graphql-compose Graphql 字段上的未知参数 - Graphql Unknown argument on field 在GraphQL突变中为字段分配类型 - Assigning a Type to a field in GraphQL Mutation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM