[Apollo Server] Get started with Apollo Server

Get started with apollo server with node.js:

 

Install:

npm install --save apollo-server graphql

 

index.js:

const { ApolloServer, gql } = require('apollo-server');

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


const typeDefs = gql`
  # Comments in GraphQL are defined with the hash (#) symbol.
  type Book {
    "Title of the book, this will appear in graphql playground"
    title: String
    author: String
  }

  # The "Query" type is the root of all GraphQL queries.
  # (A "Mutation" type will be covered later on.)
  type Query {
    books: [Book]
  }
`;

// Resolvers define the technique for fetching the types in the
// schema.  We'll retrieve books from the "books" array above.
const resolvers = {
  Query: {
    books: () => books,
  },
};

const server = new ApolloServer({ typeDefs, resolvers });


server.listen().then(({ url }) => {
  console.log(`🚀  Server ready at ${url}`);
});

 

posted @ 2019-01-11 16:16  Zhentiw  阅读(166)  评论(0)    收藏  举报