GraphQL Query Issue Array of Objects

My GraphQL sandbox localhost at port 4000

Problem : I am querying for data and not sure how to construct the typeDefs for Graphql and mongoose description of the database. It provides me an array/list of objects and that is what I do not understand how to construct.

Operation

query test {
  experiences {
    experience
  }
}

Error :

{
  "data": {
    "experiences": [
      {
        "experience": null
      }
    ]
  },
  "errors": [
    {
      "message": "String cannot represent value: { pastProjects: [Array], AAA: [Array], audioServer: [Array], BBB: [Array], assetMap: [Array], CCC: [Array], chicmedia: [Array], chicmedia2: [Array], songspub: [Array], indelible: [Array], DDD: [Array], foundations: [Array], vistarr: [Array], ctny: [Array], sportCut: [Array], EEE: [Array] }",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "experiences",
        0,
        "experience"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "stacktrace": [
          "GraphQLError: String cannot represent value: { pastProjects: [Array], AAA: [Array], audioServer: [Array], BBB: [Array], assetMap: [Array], CCC: [Array], chicmedia: [Array], chicmedia2: [Array], songspub: [Array], indelible: [Array], DDD: [Array], foundations: [Array], vistarr: [Array], ctny: [Array], sportCut: [Array], EEE: [Array] }",
          
      
Books.js

const pastProjectsSchema =  new mongoose.Schema({
    experience: {
        pastProjects:{
            1:[]
        }
    }
        

})

export const Experience = mongoose.model( "Experience", pastProjectsSchema , "experience");

typeDefs.js

import gql from 'graphql-tag';

export const typeDefs = gql`
    type Query {
        experiences:[Experience]
    }

    type Experience{
        experience: String
    }
`;

resolvers.js

import { Experience} from './models/Book.js'

export const resolvers = {
    Query: {
        experiences: async() => await Experience.find({})
    }
};

Short Sample MongoosedDB Data

      {
        "experience": {
          "pastProjects": [
            {
              "1": [
                {
                  "title": "One"
                }
              ]
            },
            {
              "2": [
                {
                  "title:": "TWO"
                }
              ]
            }
     	]
      }
    }
          

]SOLVED SOLUTION [

I took some time to restructure the JSON.
It looks like this in MongoDB.

              {
    	"pastprojects": [{
    			"title": "One Title",
    			"main": "- Blah Line 1<br>- Blah Line 2",
    			"sub": "Sub One Description"
    
    		},
    		{
    			"title": "Two Title",
    			"main": "- Blah Line 1<br>- Blah Line 2",
    			"sub": "Sub Two Description"
    
    		}
    
    	]
    }

This is my mongoose.model in file Book.js:

        import mongoose from "mongoose";
    const pastProjectsSchema =  new mongoose.Schema({
            pastprojects:[
                    {   
                        title: String,
                        main: String,
                        sub: String
                    }
                ]
                
        })
        
        export const Experience = mongoose.model( "Experience", pastProjectsSchema , "pastprojects");
This is my GraphQl typeDefs.js:
            import gql from 'graphql-tag';   
        export const typeDefs = gql`
            type Query {
                experiences: [Experience]
            }
            type Experience {
                pastprojects: [ProjectDetail]
              }
            
              type ProjectDetail {
                title: String
                main: String
                sub: String
              }
               
        `;

This is my resolver.js :

        import { Experience} from './models/Book.js'
    
    export const resolvers = {
        Query: {
            experiences: async() => await Experience.find({}),
        }
    };

Hope this help someone.

1 Like