[Apollo Server] Get started with Apollo Server

本文介绍了如何使用Node.js和Apollo Server快速搭建一个GraphQL服务。通过安装必要的包,定义数据模型和解析器,最终实现了一个可以查询书籍信息的简单GraphQL API。

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

 

转载于:https://www.cnblogs.com/Answer1215/p/10255745.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值