Develop GraphQL Web APIs using Apollo Server

donvitocodes

donvitocodes

Posted on August 24, 2019

Develop GraphQL Web APIs using Apollo Server

I've been learning GraphQL since it is more flexible than REST APIs. GraphQL has been initially developed and used by Facebook in 2012. In 2018, the GraphQL project was moved to the GraphQL foundation (under the Linux Foundation).

Compared to REST, I guess one obvious and major advantage which I've really loved so far is the ability to only retrieve data fields you need for your application. There are a few interesting frameworks you can start with when building your GraphQL APIs.

If you want to build from scratch, you can choose from these 3 server frameworks that I know of.

Apollo Server https://apollographql.com/docs/apollo-server
Express GraphQL Server https://github.com/graphql/express-graphql
Prisma https://www.prisma.io/with-graphql

I've chosen Apollo for this example since Apollo is one of the pioneers in the GraphQL space. Also, the code is very simple. In this example, we'll be using NodeJS. I am using node v11.4.0 for this example. So here it goes...

Below is the source code in Glitch. Feel free to remix! :)

All source code for this example is available in my github repo.

GitHub logo donvito / graphql-server-apollo-example

Here is a simple example of how to use Apollo GraphQL server

graphql-server-apollo-example

I've created an example of a GraphQL server using Apollo GraphQL server. I hope it can be useful to someone who is learning GraphQL with Apollo https://www.apollographql.com/docs/apollo-server/

Install dependencies

npm install

Run the example

node index.js

Deploying using Glitch

You can run the server code in glitch. It has been tested to be working fine. You can fork the repository in your github account and import the repo in glitch. Glitch is free hosting for nodejs code.

Here is the full code in this example:

const { ApolloServer, gql } = require('apollo-server')
const typeDefs = gql`
  type Job {
    id: Int
    position: String
    company: String
    description: String
    location: String
    employmentType: String
    skillsRequired: [String]
  }

  type Query {
    job(id: Int!): [Job],
    jobs: [Job]
  }
`;

const jobs = [
  {
    id: 1,
    position: 'Software Engineer',
    company: 'Apple',
    description: 'job description',
    skillsRequired: ['Go', 'GraphQL'],
    location: 'location',
    employmentType: 'full-time',
  },
  {
    id:
…

Full blog post here
http://www.melvinvivas.com/graphql-api-using-apollo-server-example

💖 💪 🙅 🚩
donvitocodes
donvitocodes

Posted on August 24, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related