Skip to content

Instantly share code, notes, and snippets.

{
book(id: "1") {
id
name
genre
}
}
$ npm start
> server-graphql@1.0.0 start /Users/zegnus/Development/graph-ql-end-to-end-android/server-express-graphql
> nodemon server.js
[nodemon] 1.18.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
listening requests on 4000
npm start
module.exports = new GraphQLSchema({
query: RootQuery
})
book(id: "2") {
name
genre
}
RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
book: {
type: BookType,
args: { id: { type: GraphQLString } },
resolve(parent, args) {
// find data
return lodash.find(books, {id: args.id})
}
lodash = require('lodash')
var books = [
{ name: "Book 1", genre: 'Fantasy', id: '1' },
{ name: "Book 2", genre: 'Fantasy', id: '2' },
{ name: "Book 3", genre: 'Sci-Fi', id: '3' },
]
BookType = new GraphQLObjectType({
name: 'Book',
fields: () => ({
id: { type: GraphQLString },
name: { type: GraphQLString },
genre: { type: GraphQLString }
})
})
graphql = require('graphql')
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema
} = graphql;
# Install node.js from https://nodejs.org/en/
mkdir server
cd server
npm init # yes to everything
npm install express
npm install nodemon # will restart the server automatically on any file change
npm install graphql express-graphql
npm install lodash # this will help with our in-memory database queries