簡體   English   中英

在GraphQL中,如何實現可選查詢?

[英]In GraphQL, how do I implement optional queries?

編輯:由於目前尚不清楚,我特別在問有關數據庫請求的信息,我應該在用於查詢后端的resolve函數中提出該數據庫請求。

我正在實現GraphQL查詢,並且在確切了解我應該在給定查詢中進行多少次調用時遇到了麻煩。 例如,這是一個包含用戶和俱樂部的數據結構,用戶可以加入該俱樂部:

User {
  UserID
  Clubs
  OtherStuff
}

Club{
  ClubID
  ClubName
}

我可以對數據庫進行以下調用:

  • 獲取所有用戶ID
  • 獲取有關UserID的信息
  • 獲取用戶的所有ClubID
  • 獲取有關ClubID的信息

我不明白的是,是否應該在每次查詢用戶時都撥打所有這些電話。 如果某人僅查詢UserID,則檢索其他所有內容似乎是巨大的浪費。 Clubs for User應該是一個完全獨立的GraphQL查詢嗎,Clubs應該只返回ClubID,還是可以有效地在Clubs字段中提供完整的Club信息?

編輯2:好的,這是我的代碼。 我已經評論了我不確定該怎么做的部分。

const QueryType = new GraphQLObjectType({
  name: 'Query',
  fields: {
    User: userQueryField,
    Club: clubQueryField
  }
});

const clubQueryField = {
  type: new GraphQLList(ClubType),
  args: {
    UserID: {
      description: 'Returns all clubs the user is a part of',
      type: GraphQLID
    }
  },
  resolve(root, {UserID}) {
    //.....
  }
};


const userQueryField = {
  type: new GraphQLList(UserType),
  args: {
    UserID: {
      description: 'If omitted, returns a list of all users. \
      Otherwise, returns the user with the provided ID',
      type: GraphQLID
    }
  },
  resolve(root, {UserID}) {
    if(UserID){
      return new Promise((resolve, reject) => {
        getUserInfo(UserID, function(response){

          //HERE: I have tried different things, like clubQueryField(UserID) and clubQueryField.resolve, but with no luck
          UserID.Clubs = clubQueryField
          resolve([response]);
        });
      });
    }
  }
};

編輯3:UserType定義。

const UserType = new GraphQLObjectType({
  name: 'User',
  fields: () => ({
    UserID: {
      type: GraphQLID
    },
    Clubs: {
      type: new GraphQLList(ClubType)
    }
  })
});

GraphQL具有延遲加載功能,可處理查詢中的其他字段。 如果用戶編寫以下查詢:

query{
   users{
    id
   }
}

它獲取用戶ID,並且顯然不會尋找其他字段。 當然,這絕對取決於您的解析器。 如果您編寫的解析器是這樣的: users { resolve: body => get_user_id} ,它將從數據庫中獲取用戶ID。 希望這可以幫助!

GraphQL API按照類型和字段進行組織,我建議為用戶類型和俱樂部類型創建單獨的查詢,如果有人僅查詢userId,則他們將使用用戶查詢。

查詢用戶:

query {
  users {
   id
  }
}

查詢俱樂部:

query {
  club {
   clubID
   clubName
   userId
  }
}

如果有人試圖查詢用戶/俱樂部,則可以將俱樂部類型添加到用戶類型中,如下所示:

query {
  users {
   id
   club {
    clubID
    clubName
   }
  }
}

在您的用戶類型中,將有一個名為club的字段,它將是club類型,而在解析器中,它將使用obj.userId作為參數來調用club查詢, obj.userId的優點在於,您可以讓客戶端決定要取。

您可以在此處看到嵌套查詢的示例

編輯:然后在您的情況下,您可以修改userType以包括該用戶所屬的俱樂部,您不需要在clubQueryField該條件,該條件將取決於客戶clubQueryField在執行的查詢,所以可以說您的用戶類型是:

用戶類型:

export default new GraphQLObjectType({
  name: 'UserType',
  fields: {
    userId: {
      description: 'user id',
      type: GraphQLString
    },
    email: {
      description: 'user email',
      type: GraphQLString
    },
    clubs: {
      description: 'User clubs',
      type: new GraphQLList(ClubType),
      resolve: (obj, args, _) => {
        return getUserClubs(obj.userId);
      }
    },
  }
});

然后,如果您的客戶僅需要用戶,它將請求以下內容:

  query {
    User {
     userId
     email
     }
  }

如果客戶需要用戶和俱樂部,它將請求以下內容:

query {
    User {
     userId
     email
     clubs {
        field1
        field2
       }
     }
  }

暫無
暫無

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

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