Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Created June 14, 2018 12:53
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 dhavaln/d04216394b50093f84c6d8e7e15646c5 to your computer and use it in GitHub Desktop.
Save dhavaln/d04216394b50093f84c6d8e7e15646c5 to your computer and use it in GitHub Desktop.
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