I'm confused about how to create a Resolver correctly in graphql-compose: I have two related entities : entityGroup and 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"
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:
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 error : Cannot query field "recordId" on type "EntityGroup". graphQl error : Cannot query field "record" on type "EntityGroup". graphQl error : Field "entityGroupCreate" argument "name" of type "String!" 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
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 error : Field "entityGroupCreate" argument "name" of type "String!" 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
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
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
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.