首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过nodejs从apollo-server获取服务器的所有数据

如何通过nodejs从apollo-server获取服务器的所有数据
EN

Stack Overflow用户
提问于 2021-10-24 10:52:52
回答 1查看 32关注 0票数 0

我想要获得所有的数据,如参与者,建议,用户。

产品包括建议。提议包括参与者。

如果我使用查询,我想要获取所有数据,测试查询只显示userId,但是我想要所有关于用户测试查询信息只显示proposeId,但是我想要所有关于提议的信息

prisma模型

代码语言:javascript
复制
model Product {
  id       Int       @id @default(autoincrement())
  prodName String
  prodCode String
  posts    Post[]
  holdings Holding[]
  proposes Propose[]
}
model Propose {
  id           Int           @id @default(autoincrement())
  user         User          @relation(fields: [userId], references: [id])
  userId       Int
  product      Product       @relation(fields: [productId], references: [id])
  productId    Int
  title        String
  content      String
  totalAmt     Int
  participants Participant[]
  createdAt    DateTime      @default(now())
}
model Participant {
  id             Int      @id @default(autoincrement())
  user           User     @relation(fields: [userId], references: [id])
  userId         Int
  propose        Propose  @relation(fields: [proposeId], references: [id])
  proposeId      Int
  amt            Int
  participatedAt DateTime @default(now())
}

类型

代码语言:javascript
复制
type Participant {
    id: Int!
    user: User
    propose: Propose
    amt: Int
    participatedAt: String
  }
  type seeParticipantResult {
    ok: Boolean!
    participants: [Participant]
    error: String
  }
  type Query {
    seeParticipant: seeParticipantResult
  }

查询

代码语言:javascript
复制
export default {
  Query: {
    seeParticipant: async (_, __, { loggedInUser }) => {
      try {
        const participants = await client.participant.findMany({
          where: {
            userId: loggedInUser.id,
          },
        });
        return {
          ok: true,
          participants,
        };
      } catch (e) {
        return {
          ok: false,
          error: e,
        };
      }
    },
  },
};

测试查询

代码语言:javascript
复制
query Query {
  seeParticipant {
    ok
    participants {
      id
      user {
        username
      }
      propose {
        product {
          prodName
          prodCode
        }
        title
      }
    }
  }
}

结果

代码语言:javascript
复制
  "data": {
    "seeParticipant": {
      "ok": true,
      "participants": [
        {
          "id": 1,
          "user": null,
          "propose": null
        }
      ]
    }
  }
}

它没有显示propose和user。

EN

回答 1

Stack Overflow用户

发布于 2021-10-26 07:37:32

Prisma不会在查询时自动返回relation对象。在使用GraphQL时,您应该为任何关系字段(如GraphQL模式中的Participant.user )创建单独的解析器函数。

这是Participant.user的解析器可能的样子

代码语言:javascript
复制
  Participant: {
    user: (parent, args, context) => {
      return client.participant.findUnique({where: {id: parent.id}}).user()
    }
  }

我使用Prisma fluent APIuserfindUnique查询中获取连接的participant

请注意,您需要对Propose执行类似的操作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69696017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档