[ GraphQL ] 데이터 조회해보기

SeungJin·2022년 8월 15일
0

GraphQL

목록 보기
2/3

My-Project/graphql-server/index.jsx

const { ApolloServer, gql } = require('apollo-server');
const { readFileSync } = require('fs')
const {join} = require('path')

// The GraphQL schema
const typeDefs = gql`
  type Query {
    hello: String,
    books:[Book],
  },
  type Book {
    bookId: Int,
    title: String,
    message: String,
    author: String,
    url: String
  }
`;

// A map of functions which return data for the schema.
const resolvers = {
  Query: {
    hello: () => 'Hello World',
    books: () => {
      return JSON.parse(readFileSync(join(__dirname, "books.json")).toString());
    },
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
  playground:true,
});

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});

My-Project/graphql-server/books.json(더미 데이터)

[
  {
    "bookId": 1,
    "title": "test",
    "message": "message test",
    "author": "author test",
    "url": "url test"
  }
]

결과

처음 배우는 거라 뭐가 뭔지 잘 모르겠지만 조금씩 요청과 응답 작성방법이 이해가 가는 것 같네요.

profile
혼자 공부해 보고 적어두는 블로그입니다 문제 있다고 생각되시는 부분이 있으면 피드백이라도 남겨주시면 감사하겠습니다

0개의 댓글