Integration tests with test server in GraphQL - Mini Project: Build & Apply
Create a GraphQL schema string called typeDefs that defines a Book type with fields id (ID!), title (String!), and author (String!). Also define a Query type with a field book that takes an id argument of type ID! and returns a Book.
Use backticks to create a multi-line string. Define the Book type and Query type exactly as described.
Create a constant called resolvers that defines a Query resolver with a book function. This function takes parent, args, and returns a book object with id, title, and author fields. Return the book with id equal to args.id, title as "Test Book", and author as "John Doe".
Define the resolvers object with a Query field. The book resolver returns a fixed book object using args.id.
Create a constant called server that is a new ApolloServer instance. Pass typeDefs and resolvers as options to the constructor.
Import ApolloServer and create a new instance with the schema and resolvers.
Write an async function called testBookQuery. Inside, call server.executeOperation with a query string that requests book(id: "1") and asks for id, title, and author. Await the result and assign it to a variable called response. Then check that response.data.book.id equals "1", response.data.book.title equals "Test Book", and response.data.book.author equals "John Doe".
Use server.executeOperation with a query string. Check the response data fields for expected values.