Type as property 使用 graphql-yoga 和 prisma 解析为 null

Posted

技术标签:

【中文标题】Type as property 使用 graphql-yoga 和 prisma 解析为 null【英文标题】:Type as property resolves as null with graphql-yoga and prisma 【发布时间】:2019-12-18 09:51:27 【问题描述】:

我正在使用 prisma 服务器和 graphql-yoga(基于 node.js 的 graphql 服务器),并且我第一次使用具有其他类型作为属性的类型。这是一种方式,所以我相信没有关系。在我的例子中,Product 可以在 FavoriteList 中(所以属于?)但 FavoriteList 没有相反的关系。

type Product 
  id: ID!
  name: String!
  releaseDate: DateTime!
  brand: Brand!
  createdAt: DateTime!
  image: Image!
  barcode: String


type FavoriteList 
  id: ID!
  products: [Product]

在我的 prisma 管理员上,我可以通过连接 [Product] 将记录添加到 FavoriteList,在操场上我可以使用 FavoriteLists 查询来查询。但是,当我为我的 graphql-yoga 服务器创建查询(和解析器)时,产品解析为 null。我正在使用的查询:

# prisma server & graphql server
query favoriteList 
  favoriteLists 
    id
    products 
      id
      name
    
  


My setup for the graphql-server

type Query 
  // ... other queries
  favoriteLists: [FavoriteList]


// resovler in Query.js
async favoriteLists(root, args, context, info) 
  return context.prisma.favoriteLists(, info)
,

所以有相同的查询,我想知道我是否必须在我的 graphql-server 中做其他事情?据我所知,Product 无法解析FavoriteList 类型。提前致谢。

【问题讨论】:

【参考方案1】:

我不同意并说您应该在 Product 和 FavouriteList 之间建立关系。当您将其分解时,一个产品“属于”一个或多个 FavouriteList,而一个 FavouriteList“拥有”多个产品。所以我会这样定义你的数据模型:

type Product 
  id: ID!
  name: String!
  releaseDate: DateTime!
  brand: Brand!
  createdAt: DateTime!
  image: Image!
  barcode: String
  favouriteLists: [FavouriteList!]! @relation(name: "ProductToFavouriteList")



type FavoriteList 
  id: ID!
  products: [Product!]! @relation(name: "ProductToFavouriteList")

您的查询和解析器看起来没问题,只是您的解析器应该返回context.prisma.query.favoriteLists(, info)

【讨论】:

以上是关于Type as property 使用 graphql-yoga 和 prisma 解析为 null的主要内容,如果未能解决你的问题,请参考以下文章

Type as property 使用 graphql-yoga 和 prisma 解析为 null

ECharts——运行错误[TypeError: Cannot read property ‘type‘ of undefined]解决方案

ECharts——运行错误[TypeError: Cannot read property ‘type‘ of undefined]解决方案

如何在Swagger中描述多态端点?

Graph network classification(As a beginner, continue to update)

tensorflow-新计算图