是否可以使用GraphQllist从多个表获取数据



在GraphQl中,我们可以在GraphQllist中写入对象类型并获取所有字段。我正在使用关联,它正在加入两个表,但我无法获取两个表的字段。它只采用我在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;
})
}

我可以在这里写入类型:新的graphqllist(电影,moviestream)??

我已经尝试过,但它不起作用。请给我一些想法,我该如何获取两个桌子的字段?

在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模式文档中了解有关此信息的更多信息,并在GraphQl.js文档中阅读有关GraphQlinterfaceType和GraphQlunionType的信息。

最新更新