https://www.howtographql.com/graphql-js/1-getting-started/
진행하면서 개념학습을 위해 정리하는 글.
npm install apollo-server
npm install graphql
const { ApolloServer } = require('apollo-server');
// 1
const typeDefs = `
type Query {
info: String!
}
`
// 2
const resolvers = {
Query: {
info: () => `This is the API of a Hackernews Clone`
}
}
// 3
const server = new ApolloServer({
typeDefs,
resolvers,
})
server
.listen()
.then(({ url }) =>
console.log(`Server is running on ${url}`)
);
- typeDefs는 Graphql의 schema를 정의하기위 사용되며, Query 안에 info는 String! 이어여 한다고 정의한것이다.
- resolvers는 해당 query가 들어왔을때 어떤 결과를 보여줘야하는지 정의해주는것이다.
- apollo-server를 지정하여 어떠 API를 호출할 것인가 정의해준다.
node src/index.js 를 실행하면 로컬호트:4000 에 연결되면서 graphql playground 으로 링크를 발생한다.
여기서 query에 info 를 실행하면 info 에 관한 글이 나오는것을 확인 할 수 있다.
GraphQL API의 기본에는 GraphQL Schema 라는것이 존재한다.
GraphQL schemas are usually written in the GraphQL Schema Definition Language (SDL). SDL has a type system that allows you to define data structures (just like other strongly typed programming languages such as Java, TypeScript, Swift, Go, etc.).
GraphQL schema 는 type 기반으로 작성된 데이타 구조라고 한다. (Java,TypeScript,Swift,Go)등등 과 같은 류의 언어 라고 한다.
모든 스키마는 총 세가지의 특성이 존재한다고 한다.
Query, Mutation, Subscription