0

I am using mongoose and graphql on the backend and I am having some trouble returning a nested mongoose object in the graphql resolver. I am able to save the data to the database with a graphql mutation but when I try to return the data I get back null in the graphql playground.

If anyone is able to help this would be much appreciated.

Mongoose schema

Graphql schema

Graphql mutation and response

If yo look at my mongoose schema you will see that I have username nested inside the customer object. I do not know how to get access to this in graphql, if anyone is able to provide me with the solution it would be of grat help. Thanks a million

1 Answers1

0

As you can see in the example I have provided below. The following properties, customer, address, shippingAddress, social, payment, userFollowers, and userFollowing are all nested obects within my mongoose schema. The below example shows how I was able to access these nested objects using graphql. As you can see you have to use a custom type.

const UserType = new GraphQLObjectType({ name: 'User', fields: () => ({ id: { type: GraphQLID }, date: { type: GraphQLString }, customer: { type: CustomerType }, address: { type: AddressType }, shippingAddress: { type: ShippingAddressType }, social: { type: SocialType }, payment: { type: PaymentType }, userFollowers: { type: UserFollowersType }, userFollowing: { type: UserFollowingType },

})

});

This is the custom customer type. I had to do one of there for each nested property.

const CustomerType = new GraphQLObjectType({ name: 'Customer', fields: () => ({ username: { type: GraphQLString }, email: { type: GraphQLString }, password: { type: GraphQLString }, gender: { type: GraphQLString }, dob: { type: GraphQLString }, bio: { type: GraphQLString } }) });

Dharman
  • 30,962
  • 25
  • 85
  • 135