簡體   English   中英

GraphQL 解析器奇怪地將某些字段返回為 null

[英]GraphQL resolver oddly returns some fields as null

在簡要概述中,我正在嘗試解析用戶列表。 我有一個 Apollo 服務器,它用我自己的方式拼接了一個 accounts-js 架構。 所以我擴展了 User 類型以包含一些額外的字段。 不過,在這種情況下,我只能返回一些字段,即 rest null。

getUsers: async (_, __, ctx) => {
    let action = await usersModel.find();
    console.log(action)
    return action;  
},

我的me查詢確實返回了所有數據而不返回單個 null 字段。

  extend input CreateUserInput {
    isAdmin: Boolean!
  }

  extend type User {
    isAdmin: Boolean
    profile: Profile
    keys: [Key]
  }

  type Profile {
    avatar: String
    bio: String
  }

  type Key {
    id: ID
    livenet: Boolean
    nickname: String
  }
    
  type Query {
    getUsers: [User]
    me: User
  }

但是,當我在查詢getUsers的解析器中使用console.log(action)時,這些是在我的控制台中返回的文檔。

[ { profile: { bio: 'haha' },
    _id: 5f0a901bdcf725204446c949,
    isAdmin: true,
    services: { password: [Object], email: [Object] },
    createdAt: 1594527771625,
    updatedAt: 1594691054105,
    username: 'ayooo',
    emails: [ [Object] ],
    keys: [ [Object], [Object] ] },
  { profile: { bio: 'testing' },
    _id: 5f0a9439abfce521aba79b2c,
    isAdmin: false,
    services: { password: [Object], email: [Object] },
    createdAt: 1594528825858,
    updatedAt: 1594762680766,
    username: 'lol',
    emails: [ [Object] ],
    keys: [ [Object] ] } ]

文本

如果我下載我的整體 GraphQL 架構,它看起來像這樣:

directive @auth on FIELD_DEFINITION | OBJECT
input AuthenticateParamsInput {
  access_token: String
  access_token_secret: String
  provider: String
  password: String
  user: UserInput
  code: String
}

input CreateUserInput {
  isAdmin: Boolean!
  username: String
  email: String
  password: String
}

type CreateUserResult {
  userId: ID
  loginResult: LoginResult
}

type EmailRecord {
  address: String
  verified: Boolean
}

type ImpersonateReturn {
  authorized: Boolean
  tokens: Tokens
  user: User
}

input ImpersonationUserIdentityInput {
  userId: String
  username: String
  email: String
}

type Key {
  id: ID
  livenet: Boolean
  exchange: String
  nickname: String
  apiKey: String
  apiSecret: String
}

type LoginResult {
  sessionId: String
  tokens: Tokens
  user: User
}

type Mutation {
  setBio(bio: String): ID
  setAvatar(avatar: String): ID
  changeUsername(username: String): ID
  setKey(
    livenet: Boolean
    exchange: String
    nickname: String
    apiKey: String
    apiSecret: String
  ): ID
  createUser(user: CreateUserInput!): CreateUserResult
  verifyEmail(token: String!): Boolean
  resetPassword(token: String!, newPassword: String!): LoginResult
  sendVerificationEmail(email: String!): Boolean
  sendResetPasswordEmail(email: String!): Boolean
  addEmail(newEmail: String!): Boolean
  changePassword(oldPassword: String!, newPassword: String!): Boolean
  twoFactorSet(secret: TwoFactorSecretKeyInput!, code: String!): Boolean
  twoFactorUnset(code: String!): Boolean
  impersonate(
    accessToken: String!
    impersonated: ImpersonationUserIdentityInput!
  ): ImpersonateReturn
  refreshTokens(accessToken: String!, refreshToken: String!): LoginResult
  logout: Boolean
  authenticate(
    serviceName: String!
    params: AuthenticateParamsInput!
  ): LoginResult
  verifyAuthentication(
    serviceName: String!
    params: AuthenticateParamsInput!
  ): Boolean
}

type Profile {
  avatar: String
  bio: String
}

type Query {
  getUsers: [User]
  getProfile: Profile
  serverTime: String
  me: User
  twoFactorSecret: TwoFactorSecretKey
  getUser: User
}

type Tokens {
  refreshToken: String
  accessToken: String
}

type TwoFactorSecretKey {
  ascii: String
  base32: String
  hex: String
  qr_code_ascii: String
  qr_code_hex: String
  qr_code_base32: String
  google_auth_qr: String
  otpauth_url: String
}

input TwoFactorSecretKeyInput {
  ascii: String
  base32: String
  hex: String
  qr_code_ascii: String
  qr_code_hex: String
  qr_code_base32: String
  google_auth_qr: String
  otpauth_url: String
}

type User {
  username: String
  isAdmin: Boolean
  profile: Profile
  keys: [Key]
  id: ID!
  emails: [EmailRecord!]
}

input UserInput {
  id: ID
  email: String
  username: String
}

請注意,getUser 或我的查詢會返回所有請求的數據。 getUsers 只會返回一點點,主要是 null 用於我需要在前端查詢的重要內容。 我正在嘗試在我的管理面板的表格中列出用戶。 也許這不是最好的方法? 有人告訴我!

在此處輸入圖像描述

與查詢me或返回用戶的getUser不同。 :/

type Query {
    getUsers: [User]
    me: User
  }

您的查詢getUsers返回User類型的數組。

  extend type User {
    isAdmin: Boolean
    profile: Profile
    keys: [Key]
  }

為什么要extend 是否存在任何早期的User定義? ...無論如何graphQL 服務器只返回返回類型中定義的字段 您的User類型只有isAdminprofilekeys屬性。 只會返回那些字段。

解決方案:擴展您的User類型定義以獲取其他現有的數據庫字段。

所以在剔除很多其他東西之后,我意識到這是由於我沒有完全定義我的 mongoose 架構!


mongoose.connect(process.env.MONGO_URI, {
  useNewUrlParser: true,
  useFindAndModify: false,
  useCreateIndex: true,
  useUnifiedTopology: true,
});

var usersSchema = new mongoose.Schema({
  username: String,
  isAdmin: Boolean,
  emails: [{ 
    verified: Boolean, 
    address: String 
  }],
  profile: {
    avatar: String,
    bio: String,
  },
  keys: [{ 
    nickname: String,
    exchange: String,
    livenet: Boolean,
    apiKey: String,
    apiSecret: String,
  }]
});

export const usersModel = mongoose.model('Users', usersSchema, 'users');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM