Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Created June 14, 2021 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuc-arc-f/19fb8f9a5c56f72bfdc981bf368c235b to your computer and use it in GitHub Desktop.
Save kuc-arc-f/19fb8f9a5c56f72bfdc981bf368c235b to your computer and use it in GitHub Desktop.
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