I have one collection call person, which have few field :-
{
"_id": {
"$oid": "5f26e8ef969cc80d9710b250"
},
"firstName": "xxxxxxx",
"lastName": "xxxxxx",
"email": "xxxxxxxx@gmail.com",
"userName": "xxxxxx",
"friends": [
{
"$numberInt": "2001" // person identity
},
{
"$numberInt": "1002" // person identity
}
],
"personIdentityCount": {
"$numberInt": "1000"
}
}
Friends is an array which have person Identity count which is unique and we can identify person from it means if we search 1002 person identity count it will give us a person data.
I have crated a schema and type:-
const personType=new GraphQLObjectType({
name: "Person",
fields:{
id: { type: GraphQLID },
firstName: { type: GraphQLString },
lastName: { type: GraphQLString },
email: { type: GraphQLString },
userName: { type: GraphQLString },
count: { type: GraphQLInt },
friends:{
type: new GraphQLList(PersonType), // getting an error " personType is not defined "
resolve: (person) => person.friends.map(id => getPersonByPersonIdentityCount(id))
}
}
});
I am getting error of personType is not defined in friends type({ type: new GraphQLList(PersonType)}).
===== Schema for GraphQL =====
const scheam = new GraphQLSchema({
query: new GraphQLObjectType({
name: "Query",
fields: {
people: {
type: GraphQLList(personType),
resolve: async (root, args, context, info) =>
{
return PersonModel.find().exec();
}
},
person: {
type: personType,
args: {
id: { type: GraphQLNonNull(GraphQLID) }
},
resolve: (root, args) => getPersonByPersonIdentityCount(args.id)
}
}
})
});
I want if I ask for friend in graphQl request then friends value should come.
#expected response
person(id:"5f26a1e8034dec6713cbd28e"){
id,
firstName,
friends{
id,
firstName,
lastName
}
}
}
I can solve this problem by database level but I want a solution in graphQL.