Skip to content

Instantly share code, notes, and snippets.

@saiumesh535
Created February 10, 2018 19:53
Show Gist options
  • Save saiumesh535/a92412cc8688543110e9cf389e5e0bbb to your computer and use it in GitHub Desktop.
Save saiumesh535/a92412cc8688543110e9cf389e5e0bbb to your computer and use it in GitHub Desktop.
GraphQL very basic example.
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!'));
@sainik18
Copy link

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!
}

type DoSometing{
    all : String!
}

type Query{
    getNames: DoSometing
}

type Mutation{
    addName(input: Name): DoSometing
}

`)

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'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment