Skip to content

Instantly share code, notes, and snippets.

@kkworden
kkworden / index.js
Last active December 10, 2018 19:29
Execute GraphQL Query Inside of Endpoint
'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());
@kkworden
kkworden / index.js
Last active December 10, 2018 19:29
Basic Express and GraphQL Setup
'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());
@kkworden
kkworden / schema.js
Last active December 10, 2018 19:29
GraphQL Schema Separate File
const buildSchema = require('graphql').buildSchema;
const schema = `
type Query {
hello: String
}
`;
module.exports = buildSchema(schema);
@kkworden
kkworden / resolvers.js
Created December 10, 2018 19:29
Resolvers for the schema
const resolvers = {
hello: () => {
return 'Hello world!';
},
};
module.exports = resolvers;
@kkworden
kkworden / index.js
Last active December 10, 2018 20:40
Updated express route to use express-graphql middleware
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 / schema.js
Created December 10, 2018 20:56
Schema with send and receive operations
const buildSchema = require('graphql').buildSchema;
const schema = `
type Query {
messages: [String]
}
type Mutation {
sendMessage(nickname: String!, message: String!): Boolean
}
@kkworden
kkworden / resolvers.js
Created December 10, 2018 21:04
Resolvers for the query and mutation operations
let messages = [];
const resolvers = {
messages: () => {
return messages;
},
sendMessage: ({nickname, message}) => {
messages.push(`${nickname}: ${message}`);
return true;
},
@kkworden
kkworden / script.js
Created December 12, 2018 22:39
Script code for the GraphQL chat API
function getChatroom() {
$.post('/api', { query: '{ messages }' }, function(data) {
$('#messages').html(data.data.messages);
});
}
setInterval(function() {
getChatroom();
}, 2000);
@kkworden
kkworden / index.html
Last active December 12, 2018 22:48
Basic HTML page for the GraphQL chat app
<!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 / script.js
Created December 13, 2018 18:00
Sending chat messages for the GraphQL API
// Sending messages
function sendMessage(nickname, message) {
if (!nickname || nickname == '') {
nickname = '(nobody)';
}
$.post('/api', { query: `mutation { sendMessage(nickname: "${nickname}", message: "${message}")}`}, getChatroom);
}
$('#send').click(function() {