Why does my GraphQL query to return one record fai

2019-08-27 17:00发布

I have a Mongo database with a collection called 'words' which contains documents like this:

{
  _id: "xxxx",
  word: "AA",
  definition: "Cindery lava"
}

I have a node app that I am using to query and display information from the words collection, with GraphQL. I have created a GraphQL schema and Mongoose model, as shown below.

// Schema
const WordType = new GraphQLObjectType({
    name: 'Word',
    fields: () => ({
        id: {type: GraphQLID},
        word: { type: GraphQLString },
        definition: { type: GraphQLString },
    })
})

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        detailsForWord: {
            type: WordType,
            args: {word: {type: GraphQLString}},
            resolve(parent, args) {
                return Word.find({word: args.word});
            }
        },
        allWords: {
            type: new GraphQLList(WordType),
            resolve(parent, args) {
                return Word.find({}).limit(100);
            }
        }
    }
});


// model 
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const wordSchema = new Schema({
    word: String,
    definition: String,
});

My problem is that the "allWords" query works perfectly but the "detailsForWord" does not work at all, and I have no idea why.

In GraphiQL I am using these queries:

{
  allWords {
    word
    definition
  }
}

... and

{
  detailsForWord(word: "AA") {
    word
    definition
  }
}

The former returns records, but the latter always returns the following in GraphiQL:

{
  "data": {
    "detailsForWord": {
      "id": null,
      "word": null,
      "definition": null
    }
  }
}

Any ideas why the "detailsForWord" query is failing?

0条回答
登录 后发表回答