Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript GraphQL Query Issue Array of Objects

lindylex

New Coder
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" } ] } ] } }
 
Feel confident I got the structure correct for the mongoose.Schema for the number 1 array of objects.


const pastProjectsSchema = new mongoose.Schema({ experience: { pastProjects:[ { 1: [ { title: String } ] } ] } })
 
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({}), } };
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom