sarkistlt / mongoose-schema-to-graphql

Use Mongoose schema to generate graphQL type.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

field type must be Output Type but got: undefined

MilosStanic opened this issue · comments

Hi,

Whatever I try with my Mongoose schemas, I always get this error.
Error: Query.LMEs field type must be Output Type but got: undefined.

var LMEQueries = {
  LMELastMonthAverages: {
    type: LMEAgType,
    resolve: LME.lastmonthavgall
  },
  LMEs: {
    type: LMEType,
    resolve: LME.allLMEs
  }
};


let RootQuery = new GraphQLObjectType({
  name: 'Query',      //Return this type of object
  fields: () => ({    // list all queries here with links to resolvers
    LMEAveragesLastMonth: LMEQueries.LMELastMonthAverages,
    LMEs: LMEQueries.LMEs,
    Users: UserQueries.allUsers
  })
});

...

let schema = new GraphQLSchema({
  query: RootQuery/*,
  mutation: RootMutation*/
});

And the Schema is (shortened)

var LMESchema = new Schema({
    date: Schema.Types.Date,
    copper: [Copper],
    alAlloy: [AlAlloy],
    naAlloy: [NAAlloy],
    aluminium: [Aluminium],
    zinc: [Zinc],
    lead: [Lead],
    tin: [Tin],
    nickel: [Nickel],
    globalSteel: [GlobalSteel],
    cobalt: [Cobalt],
    molybdenum: [Molybdenum],
    forex: [Forex]
});

// each of the arrays are defined as a separate types e.g.

var Copper = new Schema ({
    cashBuyer: Number,
    cashSeller: Number,
    threeMBuyer: Number,
    threeMSeller: Number,
    december1Buyer: Number,
    december1Seller: Number,
    december2Buyer: Number,
    december2Seller: Number,
    december3Buyer: Number,
    december3Seller: Number,
    ABR: Number
});

LMESchema.set('toJSON', { getters: true });

var LME = mongoose.model('LME', LMESchema);

module.exports = LME;

module.exports.allLMEs = function(){
  return new Promise(
    (resolve, reject) => {
      LME.find().then((err, results)=> err ? reject(err): resolve(results) );
    }
  );
}

and finally

var createType = require('mongoose-schema-to-graphql').default;
var LMESchema = require('./LMESchema');

let queryConfig = {
  name: 'LME',
  description: 'LME type',
  class: 'GraphQLObjectType',
  schema: LMESchema
  //exclude: ['mongooseId']
};


let LMEType = createType(queryConfig);

module.exports = LMEType;

I looked at your source, and I don't see a case where it returns undefined.
Can you guess what this is about?

Thanks.

I solved my problem. Sort of.
I was supposed to pass LMESchema.schema to the queryConfig object.
It works, except that my numbers are now integers. And I need them in floating point. I read the README, but I have like 40 properties that need to be extend-ed. So it kinda beats the point of the whole exercise.

glad it works, for floating numbers I mentioned it in article and readme. Mongoose doesn't support floating type so it always transform it to integer. But you can force it to be floating like in example below:

import { GraphQLFloat } from 'graphql';

var createType = require('mongoose-schema-to-graphql').default;
var LMESchema = require('./LMESchema');

let queryConfig = {
  name: 'LME',
  description: 'LME type',
  class: 'GraphQLObjectType',
  schema: LMESchema
  extend: {
      queryConfig: {type: GraphQLFloat}
    }
};


let LMEType = createType(queryConfig);

module.exports = LMEType;

So it will overwrite queryConfig with extended props.

Also if you use babel this syntaxes will be preferred.

import { GraphQLFloat } from 'graphql';
import createType from 'mongoose-schema-to-graphql';
import LMESchema from './LMESchema';

export default createType({
  name: 'LME',
  description: 'LME type',
  class: 'GraphQLObjectType',
  schema: LMESchema
  extend: {
      queryConfig: {type: GraphQLFloat}
    }
});

Thanks for the reply.

Are you sure you meant :

extend: {
      queryConfig: {type: GraphQLFloat}
    }

I mean queryConfig. It doesn't work.
I just tested it.
If I need to extend each field like that, there's about 40 of them. And they're located in the subproperties, i.e. subtypes.

oops sorry instead of queryConfig: {type: GraphQLFloat} should be [your prop that should be floating]: {type: GraphQLFloat}

Okay, I thought so.
In that case, there's too many of them, so it's easier for me to write the full Graphql schema instead of using this lib.
But, the lib is quite useful indeed. I will probably be using it on other schemas, e.g. User schema.

Thanks again.

I've just saw your message about 40 props. in that case if there any common props within types, you can create one base extend object and reuse it.

const commonProps = {
  [props]: {type: GraphQLFloat},
...
}
import commonExtend from 'commonExtend';

export default createType({
  name: 'LME',
  description: 'LME type',
  class: 'GraphQLObjectType',
  schema: LMESchema
  extend: {
     ...commonProps,
      [other props]: {type: GraphQLFloat}
    }
});