📄 Table of Contents

Get started with Apollo Server

Create a new project & install dependencies

* mkdir demo
* cd demo
* npm init --yes → creates package.json
* npm install apollo-server graphql
* touch index.js

Define your GraphQL schema

const { ApolloServer, gql } = require('apollo-server');// A schema is a collection of type definitions (hence "typeDefs")
// that together define the "shape" of queries that are executed against
// your data.
# Comments in GraphQL strings (such as this one) start with the hash (#) symbol. # This "Book" type defines the queryable fields for every book in our data source.const typeDefs = gql`
type Book {
title: String
author: String
}
# The "Query" type is special: it lists all of the available queries that
# clients can execute, along with the return type for each. In this
# case, the "books" query returns an array of zero or more Books (defined above).
type Query {
books: [Book]
}
`;

Define your data set

const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: 'J.K. Rowling',
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];

Define a resolver

// Resolvers define the technique for fetching the types defined in the
// schema. This resolver retrieves books from the "books" array above.
const resolvers = {
Query: {
books: () => books,
},
};

Create an instance of ApolloServer

// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
const server = new ApolloServer({ typeDefs, resolvers });// The `listen` method launches a web server.server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});

Start the server

node index.js
🚀 Server ready at http://localhost:4000/

Execute your first query

{
books {
title
author
}
}

Build a GraphQL Server from Scratch using Express- (Part 1)

References:

Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.