0

我正在学习 GraphQL。刚刚使用 Apollo-server-express 和 Knex.js 和 PostgreSQL 制作了一个简单的 API。我在通过 ID 查询用户时遇到了一个奇怪的问题,它返回null.

这是用户模式。

// user.schema.ts
  extend type Query {
    users: [User!]!
    user(id: ID): User
  }

这是用户解析器。

// user.resolver.ts

    user: async (_: any, { id }: { id: number }, { dataSources }: any) => {
      await dataSources.userAPI.findOne({ id });
    }

这是用户的数据源。

  async findOne({ id: idArg }: { id?: number } = {}) {
    const user: User = await this.knex('user')
      .where('id', idArg)
      .first();

    if (!user) return;
    console.log('@ USER HERE > ', user)
    return user;
  }

注意:console.log返回user值。

@ USER HERE >  {
  id: 1,
  first_name: 'Some',
  last_name: 'One',
  email: 's@one.com',
  password: '$2a$08$C8PP992UaUX.T90mxm2yD.RKB.ZnAx.gzMpK796JQei4H1BvVNxBG',
  role: 'staff',
  is_active: false,
  created_at: 2019-09-17T16:03:06.949Z,
  updated_at: 2019-09-17T16:03:06.949Z
}

但不知何故,它返回null到 GraphQL 游乐场。

{
  "data": {
    "user": null
  }
}

它与缓存有关吗?因为我测试了 Apollo 文档中的一些缓存控件。这是我findOne之前在用户解析器中添加的;

      // Add cache hints dynamically, this will hide result and return null
      // info.cacheControl.setCacheHint({ maxAge: 60, scope: 'PRIVATE' });
      await dataSources.userAPI.findOne({ id });

之后它开始返回null,即使在我将其移除后也是如此。我对 Post 有相同的代码,但没有对其进行测试info.cache。通过 Post ID 查询工作正常。

请帮我。

4

0 回答 0