본문 바로가기
푸닥거리

apollo graphql server

by [김경민]™ ┌(  ̄∇ ̄)┘™ 2022. 4. 27.
728x90

apollo graphql server

 

 

mkdir graphql-server-example

cd graphql-server-example

 

npm init --yes

 

npm install apollo-server graphql

touch index.js

 

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

// A schema is a collection of type definitions (hence "typeDefs")
// that together define the "shape" of queries that are executed against
// your data.
const typeDefs = gql`
  # Comments in GraphQL strings (such as this one) start with the hash (#) symbol.

  # This "Book" type defines the queryable fields for every book in our data source.
  type Book {
    title: String
    author: String
  }

  # The "Query" type is special: it lists all of the available queries that
  # clients can execute, along with the return type for each. In this
  # case, the "books" query returns an array of zero or more Books (defined above).
  type Query {
    books: [Book]
  }
`;

const books = [
    {
        title: 'The Awakening',
        author: 'Kate Chopin',
    },
    {
        title: 'City of Glass',
        author: 'Paul Auster',
    },
];

// Resolvers define the technique for fetching the types defined in the
// schema. This resolver retrieves books from the "books" array above.
const resolvers = {
    Query: {
      books: () => books,
    },
};

// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
const server = new ApolloServer({ typeDefs, resolvers });

// The `listen` method launches a web server.
server.listen().then(({ url }) => {
  console.log(`🚀  Server ready at ${url}`);
});
 

node index.js

 

🚀 Server ready at http://localhost:4000/

 

 

https://www.apollographql.com/docs/apollo-server/getting-started

 

Get started with Apollo Server

This tutorial assumes that you are familiar with the command line and JavaScript, and that you have a recent version of Node.js (12+) installed. Your project directory now contains a package.json file. Run the following command to install both of these dep

www.apollographql.com

 

728x90

댓글