GraphQL

GraphQL 간단한 서버 만들기 1

dlwltn98 2023. 1. 19. 18:23

* 실습 환경 

    vscode, nodeJS, apolloServer

 

const typeDefs = gql``

const server = new ApolloServer({ typeDefs });
server.listen().then(({ url }) => {
  console.log(`Running on ${url}`);
});
` ` 안에 graphql의 schema definition language가 있어야 함 
    => data의 shape을 graphql에게 설명
 
 
const typeDefs = gql`
    type User {
        id: ID
        username: String
    }
    type Tweet {
        id: ID
        text: String
        author: User
    }
    type Query {
        allTweets: [Tweet!]!
        tweet(id: ID): Tweet
    }
    type Mutation {
        postTweet(text: String!, userId: ID!): Tweet!
        deleteTweet(id: ID!): Boolean!
    }
`

* type Query : 사용자가 뭔가를 요청할 수 있는 기능 정의 ( 예시 : Rest API - GET )

* type Mutation : 사용자가 보낸 데이터로 DB 나 서버 등을 변형 시켜야 하는 기능 정의하는 곳 ( 예시 : Rest API - POST, PUT, DELETE )

* Object types : 가장 기본적인 구성 요소 -> User, Tweet

* Scalar types : Object types과 함께 사용됨

    1. Int

    2. Float

    3. String 

    4. Boolean

    5. ID : 데이터를 가져올 때 사용하는 key처럼 식별 가능한 고유 식별자

* ! (느낌표) : 느낌표를 붙이면 required 가 됨

          allTweets: [Tweet!]! -> 반환 필수 (non-nullable)

 

 

** Apollo는 자체적인 Stydio를 가지고 있어서 http://localhost:4000/ 로 접근하면 작성한 코드 테스트 가능

 

출처 : https://graphql.org/learn/

 

'GraphQL' 카테고리의 다른 글

GraphQL 간단한 서버 만들기 3  (0) 2023.01.23
GraphQL 간단한 서버 만들기 2  (0) 2023.01.22
GraphQL 이란?  (0) 2023.01.19