Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Created Jun 14, 2021
Embed
What would you like to do?
Apollo server + express , GraphQL sample
// Apollo server + express , GraphQL sample
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const typeDefs = gql`
type Todo {
id: Int!
title: String
}
type Query {
hello: String
todo(id: Int): Todo
todos: [Todo]
}
`;
// Provide resolver functions for your schema fields
const todos = [
{
id: 1,
title: 'task-test-1',
},
{
id: 2,
title: 'task-test-2',
},
{
id: 3,
title: 'task-test-3',
},
{
id: 4,
title: 'task-test-4',
}
]
const resolvers = {
Query: {
hello: () => 'Hello world!',
todo(parent, args, context, info){
return todos.find(todo => todo.id === args.id);
},
todos: () => {
return todos
},
},
};
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () => {
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment