是否可以使用 GraphQLList 从多个表中获取数据 [英] Is it possible to fetch data from multiple tables using GraphQLList

查看:25
本文介绍了是否可以使用 GraphQLList 从多个表中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In GraphQL we can write the object type in GraphQLList and fetch all the fields. I am using Association and it is joining the two tables but I am unable to fetch the field of both the tables. It only takes the fields what I have written in GraphQLList.As I want the list of data.

这是代码

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)??

Can I write here type: new GraphQLList(Films, MovieStream)??

我试过了,但没有用.请给我一些想法如何获取两个表的字段???

I have tried but It does not work. Please give me some idea how do I fetch the fields of both the table???

推荐答案

在 GraphQL 中有两种主要方法可以实现这一点:联合和接口.

There's two main ways of achieving this in GraphQL: unions and interfaces.

接口是 GraphQL 架构中的两个或多个对象类型共享某些字段(特征)的地方.例如,您可能有一个 Product 界面,用于您商店中的所有商品,其中每个商品都有一个 pricebarcode货架位置.你所有的产品,比如 ShampooBreadLawnChair 都会实现这个接口.

An interface is where two or more object types in your GraphQL schema share certain fields (characteristics). For example you might have a Product interface for all the items in your shop, where every product has a price, barcode, and shelfLocation. All your products, such as Shampoo, Bread, LawnChair would then implement this interface.

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!]
}

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

A union is where you declare that something can return more than one object type, but those types don't have to have any properties in common.

type Shark {
  name: String
  numberOfTeeth: Int
}

type Shoe {
  brand: String
  size: String
}

union SharkOrShoe = Shark | Shoe

extend type Query {
  searchSharksAndShoes(phrase: String!): [SharkOrShoe!]
}

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

In both cases you can query type specific fields using fragments or inline fragments:

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 架构文档 中了解更多相关信息,并阅读GraphQLInterfaceTypeGraphQLUnionType 在 GraphQL.js 文档中.

You can learn more about this in the GraphQL schema documentation and read about GraphQLInterfaceType and GraphQLUnionType in the GraphQL.js documentation.

这篇关于是否可以使用 GraphQLList 从多个表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆