Skip to content

Instantly share code, notes, and snippets.

Avatar

Kenny Worden kkworden

View GitHub Profile
@kkworden
kkworden / script.js
Created Dec 13, 2018
Fixes the chatroom
View script.js
function getChatroom() {
$.post('/api', { query: '{ messages }' }, function(data) {
var str = '';
for (var message of data.data.messages) {
str += `${message}<br />`;
}
$('#messages').html(str);
});
}
@kkworden
kkworden / script.js
Created Dec 13, 2018
Sending chat messages for the GraphQL API
View script.js
// Sending messages
function sendMessage(nickname, message) {
if (!nickname || nickname == '') {
nickname = '(nobody)';
}
$.post('/api', { query: `mutation { sendMessage(nickname: "${nickname}", message: "${message}")}`}, getChatroom);
}
$('#send').click(function() {
@kkworden
kkworden / script.js
Created Dec 12, 2018
Script code for the GraphQL chat API
View script.js
function getChatroom() {
$.post('/api', { query: '{ messages }' }, function(data) {
$('#messages').html(data.data.messages);
});
}
setInterval(function() {
getChatroom();
}, 2000);
@kkworden
kkworden / index.html
Last active Dec 12, 2018
Basic HTML page for the GraphQL chat app
View index.html
<!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>
@kkworden
kkworden / resolvers.js
Created Dec 10, 2018
Resolvers for the query and mutation operations
View resolvers.js
let messages = [];
const resolvers = {
messages: () => {
return messages;
},
sendMessage: ({nickname, message}) => {
messages.push(`${nickname}: ${message}`);
return true;
},
@kkworden
kkworden / schema.js
Created Dec 10, 2018
Schema with send and receive operations
View schema.js
const buildSchema = require('graphql').buildSchema;
const schema = `
type Query {
messages: [String]
}
type Mutation {
sendMessage(nickname: String!, message: String!): Boolean
}
@kkworden
kkworden / resolvers.js
Created Dec 10, 2018
Resolvers for the schema
View resolvers.js
const resolvers = {
hello: () => {
return 'Hello world!';
},
};
module.exports = resolvers;
@kkworden
kkworden / schema.js
Last active Dec 10, 2018
GraphQL Schema Separate File
View schema.js
const buildSchema = require('graphql').buildSchema;
const schema = `
type Query {
hello: String
}
`;
module.exports = buildSchema(schema);
@kkworden
kkworden / index.js
Last active Dec 10, 2018
Updated express route to use express-graphql middleware
View index.js
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,
@kkworden
kkworden / index.js
Last active Dec 10, 2018
Execute GraphQL Query Inside of Endpoint
View index.js
'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());
You can’t perform that action at this time.