0

The following code creates the queries and interacts with my API as I see message: 'Cast to ObjectId failed for value "58d" at path "_id" foron my api logs when I type a random number in to graphqli.

So I know its hitting the API using node-fetch but don't know why it isn't returning my name. I'm using:

{
  language(id:"58d80f843ad415941d8f908b"){
    name
  }
}

I have build my API with mongoose and NodeJS. Can someone please help :(

var fetch = require('node-fetch');
var { GraphQLSchema } = require('graphql');
var { GraphQLObjectType } = require('graphql');
var { GraphQLString } = require('graphql');

var BASE_URL = "http://localhost:3030";

function getLanguageByURL(relativeURL){
  return fetch(`${BASE_URL}${relativeURL}`)
     .then(res => res.json())
     .then(json => json.language)
}
var LanguageType = new GraphQLObjectType({
  name: 'Language',
  description: '...',

  fields: () => ({
    name: {type: GraphQLString},
  }),
});


var QueryType = new GraphQLObjectType({
  name: 'Query',
  description: '...',

  fields: () => ({
    language: {
        type: LanguageType,
        args: {
            id: {type: GraphQLString},
        },

        resolve: (root, args) => 
            getLanguageByURL(`/languages/${args.id}`)
    }
  }),
});

module.exports.schema = new GraphQLSchema({
  query: QueryType,
});
Mohammed
  • 555
  • 1
  • 6
  • 19
  • the `id` you are providing is not a valid mongodb `ObjectId`, and update the the resolver, i guess you are passing that `id` by value to `_id`, so it's throwing an `error`. !Add the resolver in Q! – parwatcodes Mar 28 '17 at 04:21

1 Answers1

0

I was getting node-fetch to return the wrong data in my getLanguageByURL function where I was converting the response into JSON which is great but right after I was doing (json => json.language)which was going into that json and asking for a "language" object which didn't exist in my array of objects. I removed this line and my query works perfectly.

Mohammed
  • 555
  • 1
  • 6
  • 19