GitHub API v4:查询全用户类型且获详细信息的替代方案问询
repositoryOwner Query Absolutely! The key here is leveraging inline fragments with the repositoryOwner interface, which lets you fetch type-specific fields for both individual users and organizational accounts (including companies/enterprises) in a single query.
Since repositoryOwner is an interface implemented by both User and Organization (and EnterpriseAccount for GitHub Enterprise instances), you can conditionally request the detailed fields you need for each type. Here's a practical example query:
query GetOwnerDetails($login: String!) { repositoryOwner(login: $login) { __typename login avatarUrl # Common fields shared by all repository owners ... on User { # User-specific detailed fields bio websiteUrl location name email } ... on Organization { # Organization/company-specific detailed fields description websiteUrl location name email } ... on EnterpriseAccount { # Optional: For GitHub Enterprise accounts description websiteUrl name } } }
How this works:
- The
__typenamefield tells you whether the result is aUser,Organization, orEnterpriseAccount, so you can handle the response appropriately in your code. - Shared fields (like
login,avatarUrl) are fetched once at the top level. - Type-specific fields (like
biofor users,descriptionfor orgs) are included in their respective inline fragments.
This approach gives you the best of both worlds: you can query any repository owner (regardless of type) and retrieve the detailed profile information that would only be available via the user query for individual accounts.
When processing the response, just check the __typename value to access the correct set of fields. For example, if __typename is User, you'll have access to bio; if it's Organization, you'll use description instead.
内容的提问来源于stack exchange,提问作者user1449456

