码迷,mamicode.com
首页 > 其他好文 > 详细

[Apollo Server] Get started with Apollo Server

时间:2019-01-11 17:11:55      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:server   ted   over   div   ret   listen   author   retrieve   this   

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}`);
});

 

[Apollo Server] Get started with Apollo Server

标签:server   ted   over   div   ret   listen   author   retrieve   this   

原文地址:https://www.cnblogs.com/Answer1215/p/10255745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!