7

データは確実にサーバーから取得されていますが、GraphQL はタイトルとコンテンツのプロパティを null として解決しています - のコンソール ログはそれを確認します。_id プロパティのみが graphql から返されます。これがクエリから返されるものjson { listings: [ { _id: '56e6c94f1cf94a7c0a4e22ba', title: null, content: null } ] }です。すべてが正しく設定されていることがわかる限り、タイトルとコンテンツに GraphQLIDType を指定して、タイプの違いを排除しようとしました。

私はgraphqlクエリを持っています:

query(`
  query findListings {
    listings(offset: 1) {
      _id,
      title,
      content
    }
  }
`).then((json) => {
  console.log('json', json.data)
})

私のルートクエリタイプ:

const QueryType = new GraphQLObjectType({
  name: 'Query',
  fields: {
    listings: {
      name: 'listings',
      type: new GraphQLList(ListingType),
      args: {
        limit: {
          type: GraphQLInt
        },
        offset: {
          type: GraphQLInt
        }
      },
      resolve(source, args, info) {
        const { fieldASTs } = info
        const projection = getProjection(fieldASTs[0])

        return ListingModel.find({}, projection).then(listing => {
          console.log(listing)
          return listing
        })
      }
    }
  }
})

そして私の「リストタイプ」:

const ListingType = new GraphQLObjectType({
  name: 'Listing',
  fields: {
    _id: {
      type: GraphQLID
    },
    title: {
      type: GraphQLString
    },
    content: {
      type: GraphQLString
    }
  }
})
4

1 に答える 1

1

これは古いことは知っていますが、リストのクエリがnullを返すときに同様の問題がありました。

配列をクエリすると、オブジェクト {key: yourArray} が取得されるため、次のようになります。

return ListingModel.find({}, projection).then(listing => {
 console.log(listing)
 return {listing: listing}
于 2016-12-07T00:41:57.807 に答える