You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use wrapResolverResolve for an ObjectTypeComposer created using composeMongoose function, but I get the error:
Error: Type User does not have resolver with name 'createOne'
import{graphql}from'graphql';import{Schema,model,connect,connection,disconnect}from'mongoose';import{composeMongoose}from'graphql-compose-mongoose';import{schemaComposer}from'graphql-compose';// STEP 1: DEFINE MONGOOSE SCHEMA AND MODELconstLanguagesSchema=newSchema({language: String,skill: {type: String,enum: ['basic','fluent','native'],},});constUserSchema=newSchema({name: String,// standard typesage: {type: Number,index: true,},ln: {type: [LanguagesSchema],// you may include other schemas (here included as array of embedded documents)default: [],alias: 'languages',// in schema `ln` will be named as `languages`},contacts: {// another mongoose way for providing embedded documentsemail: String,phones: [String],// array of strings},gender: {// enum field with valuestype: String,enum: ['male','female'],},someMixed: {type: Schema.Types.Mixed,description: 'Can be any mixed type, that will be treated as JSON GraphQL Scalar Type',},});constUser=model('User',UserSchema);// STEP 2: CONVERT MONGOOSE MODEL TO GraphQL PIECESconstcustomizationOptions={};// left it empty for simplicity, described belowconstUserTC=composeMongoose(User,customizationOptions);// STEP 3: ADD NEEDED CRUD USER OPERATIONS TO THE GraphQL SCHEMA// via graphql-compose it will be much much easier, with less typingschemaComposer.Query.addFields({userById: UserTC.mongooseResolvers.findById(),userByIds: UserTC.mongooseResolvers.findByIds(),userMany: UserTC.mongooseResolvers.findMany(),});schemaComposer.Mutation.addFields({userCreateOne: UserTC.mongooseResolvers.createOne(),userUpdateOne: UserTC.mongooseResolvers.updateOne(),});UserTC.wrapResolverResolve('createOne',next=>asyncrp=>{// extend resolve params with hookrp.beforeRecordMutate=async(doc: any,resolveParams: any)=>{console.log(doc,resolveParams)};returnnext(rp);});// STEP 4: BUILD GraphQL SCHEMA OBJECTconstschema=schemaComposer.buildSchema();exportdefaultschema;// STEP 5: DEMO USE OF GraphQL SCHEMA OBJECT// Just a demo, normally you'd pass schema object to server such as Apollo server.(async()=>{awaitconnect('mongodb://localhost:27017/test');awaitconnection.dropDatabase();awaitUser.create({name: 'alice',age: 29,gender: 'female'});awaitUser.create({name: 'maria',age: 31,gender: 'female'});constbob=awaitUser.create({name: 'bob',age: 30,gender: 'male'});constresponse1=awaitgraphql({
schema,source: 'query { userMany { _id name } }',});console.dir(response1,{depth: 5});constresponse2=awaitgraphql({
schema,source: 'query($id: MongoID!) { userById(_id: $id) { _id name } }',variableValues: {id: bob._id},});console.dir(response2,{depth: 5});constresponse3=awaitgraphql({
schema,source: 'mutation($id: MongoID!, $name: String) { userUpdateOne(filter: {_id: $id}, record: { name: $name }) { record { _id name } } }',variableValues: {id: bob._id,name: 'bill'},});console.dir(response3,{depth: 5});disconnect();})();
But when I use the same but with a composeWithMongoose it works. (example bellow)
import{graphql}from'graphql';import{Schema,model,connect,connection,disconnect}from'mongoose';import{composeWithMongoose}from'graphql-compose-mongoose';import{schemaComposer}from'graphql-compose';// STEP 1: DEFINE MONGOOSE SCHEMA AND MODELconstLanguagesSchema=newSchema({language: String,skill: {type: String,enum: ['basic','fluent','native'],},});constUserSchema=newSchema({name: String,// standard typesage: {type: Number,index: true,},ln: {type: [LanguagesSchema],// you may include other schemas (here included as array of embedded documents)default: [],alias: 'languages',// in schema `ln` will be named as `languages`},contacts: {// another mongoose way for providing embedded documentsemail: String,phones: [String],// array of strings},gender: {// enum field with valuestype: String,enum: ['male','female'],},someMixed: {type: Schema.Types.Mixed,description: 'Can be any mixed type, that will be treated as JSON GraphQL Scalar Type',},});constUser=model('User',UserSchema);// STEP 2: CONVERT MONGOOSE MODEL TO GraphQL PIECESconstcustomizationOptions={};// left it empty for simplicity, described belowconstUserTC=composeWithMongoose(User,customizationOptions);// STEP 3: ADD NEEDED CRUD USER OPERATIONS TO THE GraphQL SCHEMA// via graphql-compose it will be much much easier, with less typingschemaComposer.Query.addFields({userById: UserTC.getResolver('findById'),userByIds: UserTC.getResolver('findByIds'),userMany: UserTC.getResolver('findMany'),});schemaComposer.Mutation.addFields({userCreateOne: UserTC.getResolver('createOne'),userUpdateOne: UserTC.getResolver('updateOne'),});UserTC.wrapResolverResolve('createOne',next=>asyncrp=>{// extend resolve params with hookrp.beforeRecordMutate=async(doc: any,resolveParams: any)=>{console.log(doc,resolveParams)};returnnext(rp);});// STEP 4: BUILD GraphQL SCHEMA OBJECTconstschema=schemaComposer.buildSchema();exportdefaultschema;// STEP 5: DEMO USE OF GraphQL SCHEMA OBJECT// Just a demo, normally you'd pass schema object to server such as Apollo server.(async()=>{awaitconnect('mongodb://localhost:27017/test');awaitconnection.dropDatabase();awaitUser.create({name: 'alice',age: 29,gender: 'female'});awaitUser.create({name: 'maria',age: 31,gender: 'female'});constbob=awaitUser.create({name: 'bob',age: 30,gender: 'male'});constresponse1=awaitgraphql({
schema,source: 'query { userMany { _id name } }',});console.dir(response1,{depth: 5});constresponse2=awaitgraphql({
schema,source: 'query($id: MongoID!) { userById(_id: $id) { _id name } }',variableValues: {id: bob._id},});console.dir(response2,{depth: 5});constresponse3=awaitgraphql({
schema,source: 'mutation($id: MongoID!, $name: String) { userUpdateOne(filter: {_id: $id}, record: { name: $name }) { record { _id name } } }',variableValues: {id: bob._id,name: 'bill'},});console.dir(response3,{depth: 5});disconnect();})();
The text was updated successfully, but these errors were encountered:
I'm trying to use wrapResolverResolve for an ObjectTypeComposer created using composeMongoose function, but I get the error:
But when I use the same but with a composeWithMongoose it works. (example bellow)
The text was updated successfully, but these errors were encountered: