Created
June 14, 2018 12:53
-
-
Save dhavaln/d04216394b50093f84c6d8e7e15646c5 to your computer and use it in GitHub Desktop.
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
const express = require('express'); | |
const graphql = require('express-graphql'); | |
const graphqlTools = require('graphql-tools'); | |
const app = express(); | |
// Sample Data | |
const blogsData = [{_id: 1, title: 'Hello', content: 'World'}]; | |
// Simple Blog schema with ID, Title and Content fields | |
const typeDefs = ` | |
type Blog{ | |
_id: Int, | |
title: String!, | |
content: String! | |
} | |
type Query{ | |
blogs: [Blog], | |
blog(_id: Int): Blog | |
} | |
`; | |
// Resolver to match the GraphQL query and return data | |
const resolvers = { | |
Query: { | |
blogs(root, args, context, info) { | |
return blogsData | |
}, | |
blog(root, {_id}, context, info){ | |
return blogsData.find((b) => b._id == _id); | |
} | |
} | |
}; | |
// Build the schema with Type Definitions and Resolvers | |
const schema = graphqlTools.makeExecutableSchema({typeDefs, resolvers}); | |
app.use('/graphiql', graphql({ | |
graphiql: true, | |
schema | |
})); | |
app.listen(3000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment