grapql은 REST API의 비효율적인 fetching을 해결하고자 만들어진 API Query Language(질의어) 이다.
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.
그리고 여기에서 사용할 graphql-yoga는 React의 CRA와 같이 간단하게 서버를 세팅하는데 도움을 준다.
Fully-featured GraphQL Server with focus on easy setup, performance & great developer experience
typeDefs와 resolvers를 옵션으로 설정한 GraphQLServer로 서버 인스턴스를 생성한 후, 서버를 실행한다.
index.js
import { GraphQLServer } from 'graphql-yoga';
import resolvers from './graphql/resolvers';
const server = new GraphQLServer({
typeDefs: 'graphql/schema.graphql',
resolvers,
});
server.start(() => console.log('Graphql Server Running'));
resolvers.js
const resolvers = {
Query: {
name: () => 'Jo',
},
};
export default resolvers;
schema.graphql
type Query {
name: String!
}