DeveloperBreeze

Graphql Tutorial Development Tutorials, Guides & Insights

Unlock 1+ expert-curated graphql tutorial tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your graphql tutorial skills on DeveloperBreeze.

Building a GraphQL API with Node.js and Apollo Server

Tutorial August 12, 2024
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: