graphql-compose-mongoose icon indicating copy to clipboard operation
graphql-compose-mongoose copied to clipboard

filter for array/list field does not work as expected

Open sunsf opened this issue 4 years ago • 0 comments

const { Schema } = require('mongoose')
const { SchemaComposer } = require('graphql-compose')
const { composeWithMongoose } = require('graphql-compose-mongoose')

const ArticleSchema = new Schema({
  title: {
    type: String,
  },
  content: {
    type: String,
  },
  viewers: {
    type: [String],
  },
}, {
})

const Article = mongoose.model('Article', ArticleSchema)
const ArticleTC = composeWithMongoose(Article)

schemaComposer.Query.addFields({
    articles: ArticleTC.getResolver('findMany'),
})

If I have the data as follow Articles:

[
  { title: 'a only', content: 'a', viewers: ['a'] },
  { title: 'b only', content: 'a', viewers: ['b'] },
  { title: 'all 1', content: 'all 1', viewers: ['a', 'b'] },
  { title: 'all 2', content: 'all 2', viewers: ['a', 'b'] },
]

if I use the query:

query {
  a: articles (filter: {
    viewers: "a"
  }) {
    title
  }
  b: articles (filter: {
    viewers: "b"
  }) {
    title
  }
}

expected result:

{
  "data": {
    "a": [
      {
        "title": "a only"
      },
      {
        "title": "all 1"
      },
      {
        "title": "all 2"
      }
    ],
    "b": [
      {
        "title": "b only"
      },
      {
        "title": "all 1"
      },
      {
        "title": "all 2"
      }
    ]
  }
}

but result becomes:

{
  "data": {
    "a": [
      {
        "title": "a only"
      },
      {
        "title": "all 1"
      },
      {
        "title": "all 2"
      }
    ],
    "b": [
      {
        "title": "b only"
      }
    ]
  }
}

Is it something wrong with my implementation or a bug?

sunsf avatar Apr 28 '21 19:04 sunsf