Kind: Graphql Type
User account
User is a GraphQL object type representing a user account in the API, including identity fields (id, email, name), authorization context (role), and social/content relationships (profile, posts, followers/following). It serves as the central node for querying user-centric data and derived counts (followerCount, followingCount) in the graph.
Diagram
mermaidgraph LR User[User] Role[Role] Profile[Profile] Post[Post] Follow[Follow] User -->|role: Role!| Role User -->|profile: Profile| Profile User -->|posts: [Post!]!| Post User -->|followers: [Follow!]!| Follow User -->|following: [Follow!]!| Follow User -->|followerCount: Int!| followerCount[(Int)] User -->|followingCount: Int!| followingCount[(Int)]
Usage
tsimport { gql, GraphQLClient } from "graphql-request";
const client = new GraphQLClient(process.env.API_URL!, {
headers: { authorization: `Bearer ${process.env.API_TOKEN}` },
});
const GetUserWithRelations = gql`
query GetUserWithRelations($id: ID!) {
user(id: $id) {
id
email
name
role
profile {
id
bio
}
posts {
id
title
}
followerCount
followingCount
}
}
`;
type GetUserWithRelationsResult = {
user: {
id: string;
email: string;
name: string;
role: "ADMIN" | "USER" | string;
profile: { id: string; bio?: string | null } | null;
posts: Array<{ id: string; title: string }>;
followerCount: number;
followingCount: number;
} | null;
};
async function fetchUser(id: string) {
const data = await client.request<GetUserWithRelationsResult>(GetUserWithRelations, { id });
return data.user;
}
// Example call
fetchUser("user_123").then(console.log).catch(console.error);
AI Coding Instructions
- Treat
Useras the hub type: keep resolver logic forposts,profile,followers, andfollowingconsistent with the underlying data model (IDs, join tables, pagination if applicable). - Ensure
followerCount/followingCountare derived efficiently (prefer aggregated queries over loading fullfollowers/followinglists just to count). - Respect nullability:
profilecan benull, but list fields likepostsare non-null lists of non-null items ([Post!]!) and should always resolve to an array. - Avoid leaking sensitive fields:
emailis non-nullable, so enforce authorization checks in theUserquery resolver if email visibility is restricted. - Keep
rolealigned with theRoleenum values; validate and migrate consistently when adding new roles.
Relationships
- TYPE_OF →
Role - TYPE_OF →
Profile - TYPE_OF →
Post - TYPE_OF →
Follow - TYPE_OF →
Follow - TYPE_OF →
DateTime - TYPE_OF →
DateTime
Referenced By
user(TYPE_OF)searchUsers(TYPE_OF)createUser(TYPE_OF)updateUser(TYPE_OF)Post(TYPE_OF)Comment(TYPE_OF)Like(TYPE_OF)Follow(TYPE_OF)Follow(TYPE_OF)UserConnection(TYPE_OF)AuthPayload(TYPE_OF)
Was this page helpful?