【问题标题】:Is it possible to fetch data from multiple tables using GraphQLList是否可以使用 GraphQLList 从多个表中获取数据
【发布时间】:2019-08-02 16:11:21
【问题描述】:

在 GraphQL 中,我们可以在 GraphQLList 中编写对象类型并获取所有字段。我正在使用 Association 并且它正在加入两个表,但我无法获取两个表的字段。它只需要我在 GraphQLList 中编写的字段。因为我想要数据列表。

这里是代码

films table:

    module.exports =(sequelize, DataTypes) => {
  const films = sequelize.define(
    'films',
    {
      id:{
        type: DataTypes.INTEGER,
        primaryKey: true,
        allowNull: false,

      },
      name: {
        type: DataTypes.STRING,

      },
      },

  );

  films.associate = (models) => {


    films.hasMany(models.movie_stream, {
      foreignKey: 'movie_id',
    });

};

  return films;

}

movie_stream table:

    module.exports = (sequelize, DataTypes) => {
  const movie_streams = sequelize.define('movie_streams', {
    id:{
      type: DataTypes.INTEGER,
      primaryKey: true,
      allowNull: false,
    },
      movie_id: {
          type: DataTypes.STRING,
          foreignKey: "movie_id",

        },
     });

  movie_streams.associate = (models) => {

    movie_streams.hasMany(models.films, {
      foreignKey: 'id',
    });
  };

  return movie_streams;
};

Schema file:

movieList:{
      type: new GraphQLList(Films),
 resolve: (parent,args)=>{
return newdb.films.findAll({attributes:['id','name','permalink'],
where: {content_category_value:parent.id },
include: [{
    model:newdb.movie_stream,
    attributes:['id','movie_id'],
}],
}).then(data=>{
    return data;
})
}

我可以在这里输入:new GraphQLList(Films, MovieStream) 吗?

我试过了,但它不起作用。请给我一些想法如何获取两个表的字段???

【问题讨论】:

    标签: graphql sequelize.js sequelize-cli express-graphql sequelize-typescript


    【解决方案1】:

    在 GraphQL 中有两种主要的实现方式:联合和接口。

    接口是 GraphQL 架构中的两个或多个对象类型共享某些字段(特征)的地方。例如,您的商店中的所有商品可能都有一个Product 接口,其中每个产品都有一个pricebarcodeshelfLocation。您的所有产品,例如ShampooBreadLawnChair,都将实现此接口。

    interface Product {
      price: Float
      barcode: Int
      shelfLocation: ShelfLocation
    }
    
    type Bread implements Product {
      price: Float
      barcode: Int
      shelfLocation: ShelfLocation
      brand: String
      numberOfSlices: Int
      calories: Float
      bestBefore: Date
    }
    
    extend type Query {
      searchProducts(phrase: String!): [Product!]
    }
    

    联合是您声明某事物可以返回多个对象类型的地方,但这些类型不必具有任何共同的属性。

    type Shark {
      name: String
      numberOfTeeth: Int
    }
    
    type Shoe {
      brand: String
      size: String
    }
    
    union SharkOrShoe = Shark | Shoe
    
    extend type Query {
      searchSharksAndShoes(phrase: String!): [SharkOrShoe!]
    }
    

    在这两种情况下,您都可以使用片段或内联片段查询类型特定的字段:

    query {
      searchProducts(phrase: "tasty") {
        # shared fields
        __typename
        price
        barcode
        shelfLocation { aisle, position }
    
        # type specific fields
        ... on Bread { brand }
        ...breadFrag
      }
      searchSharksAndShoes(phrase: "sleek") {
        # only the introspection fields are shared in a union
        __typename
    
        # type specific fields
        ... on Shark { name, numberOfTeeth }
        ...shoeFrag
      }
    }
    
    fragment breadFrag on Bread {
      barcode
      bestBefore
    }
    
    fragment shoeFrag on Shoe {
      brand
      size
    }
    

    您可以在 GraphQL schema documentation 中了解更多信息,并在 GraphQL.js 文档中阅读 GraphQLInterfaceTypeGraphQLUnionType

    【讨论】:

    • 可以在node js中做吗?
    • 是的;答案底部的链接指向您将在 Node.js 中使用的 GraphQL.js 文档。
    猜你喜欢
    • 1970-01-01
    • 2018-03-20
    • 2020-04-26
    • 2022-10-01
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多