[Apollo Server] Get started with Apollo Server
Posted answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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}`); });
以上是关于[Apollo Server] Get started with Apollo Server的主要内容,如果未能解决你的问题,请参考以下文章