是否可以使用GraphQList从多个表中获取数据
在GraphQL中,我们可以在GraphQList中写入对象类型并获取所有字段。我正在使用关联,它正在连接两个表,但我无法获取两个表的字段。它只接受我在GraphQList中编写的字段,因为我需要数据列表 这是密码是否可以使用GraphQList从多个表中获取数据,graphql,sequelize.js,sequelize-cli,express-graphql,sequelize-typescript,Graphql,Sequelize.js,Sequelize Cli,Express Graphql,Sequelize Typescript,在GraphQL中,我们可以在GraphQList中写入对象类型并获取所有字段。我正在使用关联,它正在连接两个表,但我无法获取两个表的字段。它只接受我在GraphQList中编写的字段,因为我需要数据列表 这是密码 films table: module.exports =(sequelize, DataTypes) => { const films = sequelize.define( 'films', { id:{ type:
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;
})
}
我可以在这里写类型:新图形列表(电影、电影流)
我试过了,但没用。请告诉我如何获取这两个表的字段???在GraphQL中实现这一点的主要方法有两种:联合和接口 接口是GraphQL模式中的两个或多个对象类型共享某些字段(特征)的地方。例如,您可能有一个
产品
界面用于商店中的所有商品,其中每个产品都有一个价格
、条形码
、和货架
。您的所有产品,如洗发水
,面包
,LawnChair
,都将实现此接口
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.js文档中的和。我可以在node js中完成吗?是;答案底部的链接指向您将在Node.js中使用的GraphQL.js文档。