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
function getChatroom() { | |
$.post('/api', { query: '{ messages }' }, function(data) { | |
var str = ''; | |
for (var message of data.data.messages) { | |
str += `${message}<br />`; | |
} | |
$('#messages').html(str); | |
}); | |
} |
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
// Sending messages | |
function sendMessage(nickname, message) { | |
if (!nickname || nickname == '') { | |
nickname = '(nobody)'; | |
} | |
$.post('/api', { query: `mutation { sendMessage(nickname: "${nickname}", message: "${message}")}`}, getChatroom); | |
} | |
$('#send').click(function() { |
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
function getChatroom() { | |
$.post('/api', { query: '{ messages }' }, function(data) { | |
$('#messages').html(data.data.messages); | |
}); | |
} | |
setInterval(function() { | |
getChatroom(); | |
}, 2000); |
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
<!DOCTYPE html> | |
<html> | |
<b>Nickname:</b> | |
<input id="nickname" type="text" size="20" /> | |
<b>Message:</b> | |
<input id="message" type="text" size="75" /> | |
<button id="send">Send message</button> |
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
let messages = []; | |
const resolvers = { | |
messages: () => { | |
return messages; | |
}, | |
sendMessage: ({nickname, message}) => { | |
messages.push(`${nickname}: ${message}`); | |
return true; | |
}, |
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 buildSchema = require('graphql').buildSchema; | |
const schema = ` | |
type Query { | |
messages: [String] | |
} | |
type Mutation { | |
sendMessage(nickname: String!, message: String!): Boolean | |
} |
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 resolvers = { | |
hello: () => { | |
return 'Hello world!'; | |
}, | |
}; | |
module.exports = resolvers; |
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 buildSchema = require('graphql').buildSchema; | |
const schema = ` | |
type Query { | |
hello: String | |
} | |
`; | |
module.exports = buildSchema(schema); |
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 graphqlHTTP = require('express-graphql'); | |
const schema = require('./schema'); | |
const resolvers = require('./resolvers'); | |
// The rest of our old code... | |
app.use('/api', graphqlHTTP({ | |
schema: schema, | |
rootValue: resolvers, | |
graphiql: true, |
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
'use strict'; | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const graphql = require('graphql'); | |
const app = express(); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(bodyParser.json()); |
NewerOlder