Skip to content

Instantly share code, notes, and snippets.

@Kmaschta
Created March 6, 2018 07:11
Show Gist options
  • Save Kmaschta/9d4e6c5a68c9978177d42a068dfed2f6 to your computer and use it in GitHub Desktop.
Save Kmaschta/9d4e6c5a68c9978177d42a068dfed2f6 to your computer and use it in GitHub Desktop.
Apollo Memory Debug
require('isomorphic-fetch');
const express = require('express');
const { makeExecutableSchema } = require('graphql-tools');
const graphqlHTTP = require('express-graphql');
const books = [
{ title: "Harry Potter and the Sorcerer's stone", author: "J.K. Rowling" },
{ title: "Jurassic Park", author: "Michael Crichton" },
];
const typeDefs = `
type Query { books: [Book] }
type Book { title: String, author: String }
`;
const resolvers = {
Query: { books: () => books },
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const app = express();
app.use('/graphql', graphqlHTTP({
schema,
graphiql: true
}));
const { ApolloClient } = require('apollo-client');
const { HttpLink } = require('apollo-link-http');
const { InMemoryCache } = require('apollo-cache-inmemory');
const gql = require('graphql-tag');
const client = new ApolloClient({
link: new HttpLink({ uri: 'http://localhost:3000/graphql' }),
cache: new InMemoryCache()
});
const query = gql`
query Query {
books {
title
author
}
}
`;
app.use('/test', (req, res) =>
client
.query({ query })
.then(({ data }) => {
console.log('Queries', client.queryManager.queries);
res.send(data).end();
})
.catch((err) => {
console.error('Error', err);
res.status(500).end();
})
);
app.listen(3000, () => {
console.log('API available on on port 3000. Press Ctrl+C to stop it.');
});
{
"dependencies": {
"apollo-cache-inmemory": "1.1.9",
"apollo-client": "2.2.5",
"apollo-link-http": "1.5.3",
"express": "4.16.2",
"express-graphql": "0.6.12",
"graphql": "^0.12",
"graphql-tag": "2.8.0",
"graphql-tools": "2.21.0",
"isomorphic-fetch": "2.2.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment