Skip to content

Post

reference
1 min readUpdated

Kind: Graphql Type

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

Blog post

Post is a GraphQL object type representing a blog post in the backend API, including its core content (title, content) and publication state (published, publishedAt). It acts as an aggregate root that links to related entities like author, comments, likes, and tags, while also exposing derived data such as commentCount. This type is typically returned by post-related queries and mutations and drives client rendering of post detail and list views.

Diagram

mermaid
graph LR
  Post[Post]
  User[User]
  Comment[Comment]
  Like[Like]
  Tag[Tag]

  Post -->|author: User!| User
  Post -->|comments: [Comment!]!| Comment
  Post -->|likes: [Like!]!| Like
  Post -->|tags: [Tag!]!| Tag

  Post -->|id: ID!| ID[(ID)]
  Post -->|title: String!| String[(String)]
  Post -->|content: String!| String2[(String)]
  Post -->|published: Boolean!| Bool[(Boolean)]
  Post -->|publishedAt: DateTime| DateTime[(DateTime)]
  Post -->|commentCount: Int!| Int[(Int)]

Usage

ts
// Example using graphql-request in a Next.js/NestJS-compatible setup.
import { GraphQLClient, gql } from "graphql-request";

const client = new GraphQLClient(process.env.API_URL!, {
  headers: {
    // e.g. Authorization: `Bearer ${token}`,
  },
});

const GET_POST = gql`
  query GetPost($id: ID!) {
    post(id: $id) {
      id
      title
      content
      published
      publishedAt
      commentCount
      author {
        id
        name
      }
      tags {
        id
        name
      }
      comments {
        id
        content
        author {
          id
          name
        }
      }
      likes {
        id
        user {
          id
          name
        }
      }
    }
  }
`;

type GetPostResult = {
  post: {
    id: string;
    title: string;
    content: string;
    published: boolean;
    publishedAt: string | null;
    commentCount: number;
    author: { id: string; name: string };
    tags: Array<{ id: string; name: string }>;
    comments: Array<{ id: string; content: string; author: { id: string; name: string } }>;
    likes: Array<{ id: string; user: { id: string; name: string } }>;
  };
};

export async function fetchPost(id: string) {
  const data = await client.request<GetPostResult>(GET_POST, { id });
  return data.post;
}

// Usage:
// const post = await fetchPost("post_123");
// console.log(post.title, post.commentCount);

AI Coding Instructions

  • Treat Post as an aggregate: keep author, comments, likes, and tags resolved consistently (avoid partial resolver behavior that returns null for non-nullable fields).
  • Ensure commentCount: Int! stays in sync with comments (compute via DB aggregation where possible; avoid N+1 counting per post).
  • Respect publication semantics: if published is false, publishedAt should typically be null (enforce in mutations and resolvers).
  • When adding fields, keep GraphQL nullability intentional: only mark as non-null (!) if the resolver/data layer can guarantee it under all conditions.

Relationships

  • TYPE_OF → DateTime
  • TYPE_OF → User
  • TYPE_OF → Comment
  • TYPE_OF → Like
  • TYPE_OF → Tag
  • TYPE_OF → DateTime
  • TYPE_OF → DateTime

Referenced By

  • post (TYPE_OF)
  • createPost (TYPE_OF)
  • updatePost (TYPE_OF)
  • newPost (TYPE_OF)
  • User (TYPE_OF)
  • Comment (TYPE_OF)
  • Like (TYPE_OF)
  • Tag (TYPE_OF)
  • PostConnection (TYPE_OF)

Was this page helpful?

Download as PDF