Skip to content

Instantly share code, notes, and snippets.

View jbaxleyiii's full-sized avatar
💼
Moved to management

James Baxley jbaxleyiii

💼
Moved to management
View GitHub Profile
const express = require("express");
const bodyParser = require("body-parser");
const { graphqlExpress, graphiqlExpress } = require("apollo-server-express");
// import schema from generated bucklescript files
const { schema } = require("./src/Schema.bs.js");
// Initialize the app
const app = express();
open Data;
let typeDef = {|
type Mutation {
upvotePost(postId: Int!): Post
}
|};
type upvotePostArgument = {. "postId": int};
type author = {. "id": int, "firstName": string, "lastName": string};
let authors = [|
{"id": 1, "firstName": "Tom", "lastName": "Coleman"},
{"id": 2, "firstName": "Sashko", "lastName": "Stubailo"},
{"id": 3, "firstName": "Mikhail", "lastName": "Novikov"}
|];
type post = {. "id": int, "authorId": int, "title": string, "votes": ref(int)};
Mutation: {
upvotePost: (_, { postId }) => {
const post = find(posts, { id: postId });
if (!post) {
throw new Error(`Couldn't find post with id ${postId}`);
}
post.votes += 1;
return post;
},
},
open Data;
let typeDef = {|
type Author {
id: Int!
firstName: String
lastName: String
posts: [Post] # the list of Posts by this author
}
|};
/*
*
* @rest directive
*
* arguments:
* - route: path to rest endpoint. This could be a path or a full url. If a path, add to
* the endpoint given on link creation or from the context
* - provides: a map of variables to url params
* - method: the HTTP method to send the request via (i.e GET, PUT, POST)
* - type: The GraphQL type this will return
class TodoItem extends Component {
render() {
return (
<Mutation
document={MUTATION}
render={({ result, mutate, inFlight }) => {
return (
<div>
{/* my ui goes here */}
{inFight ? <div /> : <span />}
/*
*
* This payload happens per request result of a query. They can be kept for historical
* review and playback of requests / results, or only the last one can be shown.
*
* It includes as much details as I think we can one day support. At this point in time, I
* worry about shipping for hack week as it will take a good chunk of changes to support
* fully.
*
*/
import { ApolloLink, Observable } from 'apollo-link'
export class TimeoutLink extends ApolloLink {
constructor({ interval } = { interval: 1000 }) {
this.interval = interval;
}
request(operation, forward){
return new Observable(obs, => {
@jbaxleyiii
jbaxleyiii / rest.js
Created October 18, 2017 15:02
Rest Link
const USER_QUERY = gql`
query RestData($email: String!){
user(email: $email) @rest(route: '/users/email/:email', email: $email) {
id
firstName
friends @rest(route: '/users/friends/:id') @provides(vars: ['id']) {
firstName
}
}
`;