Skip to content

User

reference
1 min readUpdated

Kind: Graphql Type

Source: atloria-monorepo/apps/parser-orchestrator/test/fixtures/sample-projects/fullstack-nextjs-nestjs/backend/api/schema.graphql

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

mermaid
graph 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

ts
import { 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 User as the hub type: keep resolver logic for posts, profile, followers, and following consistent with the underlying data model (IDs, join tables, pagination if applicable).
  • Ensure followerCount/followingCount are derived efficiently (prefer aggregated queries over loading full followers/following lists just to count).
  • Respect nullability: profile can be null, but list fields like posts are non-null lists of non-null items ([Post!]!) and should always resolve to an array.
  • Avoid leaking sensitive fields: email is non-nullable, so enforce authorization checks in the User query resolver if email visibility is restricted.
  • Keep role aligned with the Role enum 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?

Download as PDF