Replit Agent prompt to build a GraphQL API
GraphQL scaffolds go wrong at the resolver layer, not the schema. This prompt has Replit Agent stand up an Apollo endpoint on Express with cursor pagination, DataLoader batching, and structured userErrors, then prove each piece in the playground. You get a queryable API with seeded data and a checkpoint habit before every schema change.
Last updated
Spin up a GraphQL API in this Repl with Apollo Server mounted on Express at /graphql, backed by the built-in Postgres database. Plan the schema, show it to me, then build.
Schema: a {{resource}} type with id, name, status, and created_at, plus a related comments type to force one join. Queries: single fetch by id, and a paginated list using cursor-based pagination with first and after arguments returning edges and pageInfo. Mutations: create, update, delete, each returning the affected node and a userErrors array instead of throwing on validation problems.
Resolvers: batch the comments lookup with DataLoader so listing many {{resource}} records issues two SQL queries, not one per row. Log SQL in dev so I can confirm the count from the workspace console.
Errors: malformed input returns userErrors with field and message, unknown ids return null with a NOT_FOUND extension code, unexpected failures return a generic message and log the stack server-side.
Tooling: leave the GraphQL playground enabled in dev so I can run queries straight from the webview, and add a seed script inserting twenty rows so pagination is testable immediately.
Done when: I can paste a list query with first: 10 into the playground, page through with the returned cursor, run one mutation with a deliberate validation error, and see userErrors instead of a 500. Snapshot a checkpoint before any schema change after that point.Customize it
Runs in your browser. Nothing you type here is sent anywhere.
Spin up a GraphQL API in this Repl with Apollo Server mounted on Express at /graphql, backed by the built-in Postgres database. Plan the schema, show it to me, then build. Schema: a users type with id, name, status, and created_at, plus a related comments type to force one join. Queries: single fetch by id, and a paginated list using cursor-based pagination with first and after arguments returning edges and pageInfo. Mutations: create, update, delete, each returning the affected node and a userErrors array instead of throwing on validation problems. Resolvers: batch the comments lookup with DataLoader so listing many users records issues two SQL queries, not one per row. Log SQL in dev so I can confirm the count from the workspace console. Errors: malformed input returns userErrors with field and message, unknown ids return null with a NOT_FOUND extension code, unexpected failures return a generic message and log the stack server-side. Tooling: leave the GraphQL playground enabled in dev so I can run queries straight from the webview, and add a seed script inserting twenty rows so pagination is testable immediately. Done when: I can paste a list query with first: 10 into the playground, page through with the returned cursor, run one mutation with a deliberate validation error, and see userErrors instead of a 500. Snapshot a checkpoint before any schema change after that point.
Same task in other tools
Questions about this prompt
How do I know the DataLoader batching actually works?
The prompt requires SQL logging in dev. Run the list query in the playground, then count statements in the workspace console. Two queries for a list with comments means batching works, one query per row means it does not.
How do I grow the schema past one type?
Add one type at a time and reuse the same pagination and userErrors patterns so the API stays consistent. Take the checkpoint first, as the prompt instructs, so a bad schema migration is a one-click rollback.
The playground returns 500s on bad input instead of userErrors, why?
The resolvers are throwing on validation failures. Ask the agent to move validation into the mutation body and reserve thrown errors for unexpected faults, then rerun the deliberate bad mutation from the done-when list.