Created
February 10, 2018 19:53
-
-
Save saiumesh535/a92412cc8688543110e9cf389e5e0bbb to your computer and use it in GitHub Desktop.
GraphQL very basic example.
This file contains hidden or 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 graphqlHTTP = require('express-graphql'); | |
const { buildSchema } = require('graphql'); | |
const app = express(); | |
let updatedCount = 0; | |
let updatedDate = new Date(); | |
class GetCount{ | |
constructor(){ | |
this.counter = updatedCount; | |
this.date = new Date(); | |
} | |
} | |
const schema = buildSchema(` | |
input Count{ | |
counter: Int! | |
} | |
type GetCount{ | |
counter: Int! | |
updatedDate: String! | |
} | |
type Query { | |
getCount: GetCount | |
} | |
type Mutation{ | |
updateCounter(input: Count): GetCount | |
} | |
`) | |
const rootValue = { | |
getCount: ( ) => new GetCount(), | |
updateCounter: ({input}) => { | |
updatedCount = updatedCount + input.counter; | |
return new GetCount(); | |
} | |
} | |
app.use('/graphQl',graphqlHTTP({ | |
graphiql: true, | |
schema, | |
rootValue | |
})) | |
app.listen(3000,()=> console.log('hey!')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');
var app = express();
const names = ["default"];
class DoSometing{
constructor(){
this.all = names.toString();
}
}
const schema = buildSchema(`
input Name{
name: String!
}
`)
const rootValue = {
addName: ({input}) =>{
names.push(input.name);
return new DoSometing();
},
getNames: () => new DoSometing(),
}
app.use('/graphql',graphqlHTTP({
graphiql: true,
schema,
rootValue
}))
app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'));