-
Start a new CodeSandbox
-
Select a new Apollo Server project
-
Update the schema:
const typeDefs = gql` type Book { title: String author: String } type Query { books: [Book] hello: String } `;
-
Add a
books
constant:const books = [ { title: "The Awakening", author: "Kate Chopin" }, { title: "City of Glass", author: "Paul Auster" } ];
-
Add a new Query resolver:
const resolvers = { Query: { hello: (root, args, context) => "Hello world!", books: () => books } };
-
Try to make this GraphQL Query through the Graphiql client in the browser panel:
{ books{ title author } }
Let's enhance the schema to display the authors as a new Author
type and as a list of Authors.
-
Update the
typeDefs
to include the newAuthor
type:const typeDefs = gql` type Author { name: String } type Book { title: String author: [Author] } type Query { books: [Book] hello: String } `;
-
Update the
books
database.const books = [ { title: "The Awakening", author: [{ name: "Kate Chopin" }] }, { title: "City of Glass", author: [{ name: "Paul Auster" }] }, { title: "Harry Potter and the Philisopher's Stone", author: [{ name: "J.K. Rowling" }] } ];
-
Reload the Graphiql client in the browser panel and try to make this GraphQL query:
{ books{ title author { name } } }