Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Created June 16, 2018 12:28
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/eaac40d34ae5118fe19e86c2df0202a9 to your computer and use it in GitHub Desktop.
Save dhavaln/eaac40d34ae5118fe19e86c2df0202a9 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 usersData = [{_id: 1000, name: 'Dhaval', email: 'dhaval@appgambit.com'}];
const blogsData = [{_id: 1, title: 'Hello', content: 'World', owner: usersData[0]}];
// Simple Blog schema with ID, Title and Content fields
const typeDefs = `
type User{
_id: Int,
name: String,
email: String
}
type Blog{
_id: Int,
title: String!,
content: String!,
owner: User
}
type Query{
blogs: [Blog],
blog(_id: Int): Blog,
users: [User],
user(_id: Int): User
}
`;
// 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);
},
users(root, {}, context, info){
return usersData;
},
user(root, {_id}, context, info){
return usersData.find((u) => u._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