Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hasMany relationship doesnt load related filds #103

Open
webface opened this issue Apr 19, 2021 · 2 comments
Open

hasMany relationship doesnt load related filds #103

webface opened this issue Apr 19, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@webface
Copy link

webface commented Apr 19, 2021

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":"tommygunn@gmail.com","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 webface added the bug Something isn't working label Apr 19, 2021
@webface
Copy link
Author

webface commented Apr 19, 2021

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

@webface
Copy link
Author

webface commented Apr 19, 2021

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:"tommygun@gmail.com"}));
        
        return admin;
      } catch (err) {
        console.log("Could not fetch resume", err);
        throw err;
      }
    },
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants
@webface and others