GraphQL Assignment-1

Basic Questions

  1. Install dependencies: express, graphql, and express-graphql.
  2. Create a basic GraphQL server that runs on port 4000.
  3. Define a GraphQL schema with type Book { id, title, author }.
  4. Write a query to fetch all books with id, title, and author.
  5. Create a resolver that returns a static array of 3 books.
  6. Define a query hello: String! that always returns “Hello GraphQL”.
  7. Add a field year to the Book type and fetch it.
  8. Create a type User { id, name, email } and fetch all users.
  9. Define a root query getUser(id:ID!): User and resolve it.
  10. Add a resolver to return a user by ID from a static list.
  11. Create mutation addBook(title:String!,author:String!): Book.
  12. Implement resolver for addBook that pushes into an array.
  13. Test addBook mutation from GraphQL Playground.
  14. Define schema with two queries: getBooks and getUsers.
  15. Write a query that fetches nested fields: book title and author.
  16. Create a resolver that filters books by author.
  17. Write mutation deleteBook(id:ID!): Boolean.
  18. Define schema type Post { id, title, content }.
  19. Add resolver for getPost(id:ID!): Post.
  20. Test query for getPost in Playground.

 Intermediate Questions

  1. Extend User type with field posts: [Post].
  2. Write query that fetches a user and their posts.
  3. Implement resolver to return posts based on userId.
  4. Create mutation updateBook(id:ID!,title:String): Book.
  5. Implement resolver to update book title in array.
  6. Define schema Comment { id, text, postId }.
  7. Write query to fetch all comments for a given post.
  8. Add resolver for comments(postId:ID!): [Comment].
  9. Create mutation addUser(name:String!,email:String!): User.
  10. Add resolver for deleteUser(id:ID!): Boolean.
  11. Write query with arguments: getBooksByYear(year:Int!): [Book].
  12. Implement resolver for getBooksByYear.
  13. Add subscription bookAdded: Book.
  14. Setup resolver for subscription to return new books.
  15. Write query fetching nested data: user → posts → comments.
  16. Add default resolver for query serverTime: String.
  17. Use GraphQL Playground to test multiple queries at once.
  18. Write query alias to fetch the same field twice with different args.
  19. Implement resolver for query searchBooks(keyword:String!): [Book].
  20. Write query that fetches only id of all books.

 Advanced Questions

  1. Setup Apollo Server instead of express-graphql.
  2. Add middleware for logging queries executed.
  3. Write mutation bulkAddBooks(books:[BookInput]!): [Book].
  4. Define input BookInput { title:String!, author:String! }.
  5. Implement resolver to handle bulk book insertion.
  6. Add subscription userAdded: User.
  7. Write query using fragments to fetch user id and name.
  8. Implement resolver with async function simulating DB delay.
  9. Integrate GraphQL server with in-memory database array.
  10. Write query that fetches deeply nested structure using fragments.