GraphQL Assignment-1
Basic Questions
- Install dependencies: express, graphql, and express-graphql.
- Create a basic GraphQL server that runs on port 4000.
- Define a GraphQL schema with type Book { id, title, author }.
- Write a query to fetch all books with id, title, and author.
- Create a resolver that returns a static array of 3 books.
- Define a query hello: String! that always returns “Hello GraphQL”.
- Add a field year to the Book type and fetch it.
- Create a type User { id, name, email } and fetch all users.
- Define a root query getUser(id:ID!): User and resolve it.
- Add a resolver to return a user by ID from a static list.
- Create mutation addBook(title:String!,author:String!): Book.
- Implement resolver for addBook that pushes into an array.
- Test addBook mutation from GraphQL Playground.
- Define schema with two queries: getBooks and getUsers.
- Write a query that fetches nested fields: book title and author.
- Create a resolver that filters books by author.
- Write mutation deleteBook(id:ID!): Boolean.
- Define schema type Post { id, title, content }.
- Add resolver for getPost(id:ID!): Post.
- Test query for getPost in Playground.
Intermediate Questions
- Extend User type with field posts: [Post].
- Write query that fetches a user and their posts.
- Implement resolver to return posts based on userId.
- Create mutation updateBook(id:ID!,title:String): Book.
- Implement resolver to update book title in array.
- Define schema Comment { id, text, postId }.
- Write query to fetch all comments for a given post.
- Add resolver for comments(postId:ID!): [Comment].
- Create mutation addUser(name:String!,email:String!): User.
- Add resolver for deleteUser(id:ID!): Boolean.
- Write query with arguments: getBooksByYear(year:Int!): [Book].
- Implement resolver for getBooksByYear.
- Add subscription bookAdded: Book.
- Setup resolver for subscription to return new books.
- Write query fetching nested data: user → posts → comments.
- Add default resolver for query serverTime: String.
- Use GraphQL Playground to test multiple queries at once.
- Write query alias to fetch the same field twice with different args.
- Implement resolver for query searchBooks(keyword:String!): [Book].
- Write query that fetches only id of all books.
Advanced Questions
- Setup Apollo Server instead of express-graphql.
- Add middleware for logging queries executed.
- Write mutation bulkAddBooks(books:[BookInput]!): [Book].
- Define input BookInput { title:String!, author:String! }.
- Implement resolver to handle bulk book insertion.
- Add subscription userAdded: User.
- Write query using fragments to fetch user id and name.
- Implement resolver with async function simulating DB delay.
- Integrate GraphQL server with in-memory database array.
- Write query that fetches deeply nested structure using fragments.