vulcan-next icon indicating copy to clipboard operation
vulcan-next copied to clipboard

hasMany relationship doesnt load related filds

Open webface opened this issue 4 years ago • 2 comments
trafficstars

I created 2 models and a has many relationship, the relationship can be seen in the graphql explorer but values come back as null

import SimpleSchema from "simpl-schema";
import mongoose from "mongoose";
/*

Define a sub-schema for addresses

*/
const profileSchema = {
  name: {
    type: String,
    optional: false,
    canRead: ["guests"],
    canUpdate: ["admin"],
    canCreate: ["admin"],
    max: 100, // limit street address to 100 characters
  },
  titles: {
    type: Array,
    optional: true,
    input: "checkboxgroup",
    canCreate: ["admins"],
    canUpdate: ["admins"],
    canRead: ["guests"],
  },
  "titles.$": {
    type: String,
    canRead: ["guests"],
    optional: true,
  },
  about: {
    type: String,
    optional: true,
    canRead: ["guests"],
    canUpdate: ["admin"],
    canCreate: ["admin"],
  },
  skillsIds: {
    type: Array,
    optional: true,
    canRead: ["guests"],
    relation: {
      fieldName: "icon",
      typeName: "[Icon]",
      kind: "hasMany",
    },
  },
  "skillsIds.$": {
    type: String,
    optional: true,
  },
};

export default profileSchema;

icon field is null in a custom query and icon is not present on the profile object of user.

Your session: {"_id":"607a106d9ade7a4eca2eebd5","email":"[email protected]","password":null,"isAdmin":true,"createdAt":"2021-04-16T22:32:13.162Z","hash":"c28b9c9a283cd4034970f3e9220990ac86155795053fdd602be5cd81c430328a9db2d5e0aa3af4092bc452eb4e225cd4394c7c9c86b242121212ea635890d878","salt":"fc01a70c352522ac7d17f5ff12b5de92","__v":0,"profile":{"name":"Yellow Squad","about":"Default Value","titles":["Tommy Gun"," Developer Dude"],"skillsIds":["607a7ee40eb9820075105c8b","607a7ee40eb9820075105c2f"]}}

webface avatar Apr 19 '21 21:04 webface

Screen Shot 2021-04-19 at 5 35 21 PM

webface avatar Apr 19 '21 21:04 webface

const typeDefs = gql`
  type Query {
    restaurants: [Restaurant]
    resume: User
  }
  type Restaurant {
    _id: ID!
    name: String
  }
  type User{
    _id:ID
    email:String
    profile:profileSchema
  }
  type profileSchema{
      name: String
      titles: [String]
      about: String
      skillsIds:[ID]
      icon: [Icon]
  }
  type Icon{
    _id:ID
    name:String
    icon: String
    version: String
  }
  type Mutation {
      updateProfile(
        name: String!
        titles: [String]
        about: String!
        skillsIds: [ID]
      ):Boolean
    }
`;
const resolvers = {
  Query: {
    // Demo with mongoose
    // Expected the database to be setup with the demo "restaurant" API from mongoose
    async restaurants() {
      try {
        const db = mongoose.connection;
        const restaurants = db.collection("restaurants");
        // @ts-ignore
        const resultsCursor = (await restaurants.find(null, null)).limit(5);
        const results = await resultsCursor.toArray();
        return results;
      } catch (err) {
        console.log("Could not fetch restaurants", err);
        throw err;
      }
    },
    async resume() {
      try {
        const db = mongoose.connection;
        const users = db.collection("vulcanusers");
        // @ts-ignore
        const admin = (await users.findOne({email:"[email protected]"}));
        
        return admin;
      } catch (err) {
        console.log("Could not fetch resume", err);
        throw err;
      }
    },
  },

webface avatar Apr 19 '21 21:04 webface