Created
June 14, 2021 07:19
-
-
Save kuc-arc-f/19fb8f9a5c56f72bfdc981bf368c235b to your computer and use it in GitHub Desktop.
Apollo server + express , GraphQL sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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