javascript nodejs graphql
const typeDefs = gql`
type Book {
title: String!
author: String!
}
type Query {
books: [Book]
bookByTitle(title: String!): Book
}
type Mutation {
addBook(title: String!, author: String!): Book
}
`;
const resolvers = {
Query: {
books: () => books,
bookByTitle: (_, { title }) => books.find(book => book.title === title),
},
Mutation: {
addBook: (_, { title, author }) => {
const newBook = { title, author };
books.push(newBook);
return newBook;
},
},
};
In the GraphQL Playground, you can use variables for more dynamic queries: