Skip to content

Instantly share code, notes, and snippets.

@ptb
Last active June 15, 2018 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptb/5b52d0c762dec57bc206c5c0e4d17349 to your computer and use it in GitHub Desktop.
Save ptb/5b52d0c762dec57bc206c5c0e4d17349 to your computer and use it in GitHub Desktop.
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless
*.js
!/index.js
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless
const { InMemoryCache } = require ("apollo-cache-inmemory")
const { ApolloClient } = require ("apollo-client")
const { withClientState } = require ("apollo-link-state")
const gql = require ("graphql-tag")
const c = new InMemoryCache ()
const defaults = {
"todos": [],
"visibilityFilter": "SHOW_ALL"
}
let nextTodoId = 0
const resolvers = {
"Mutation": {
"addTodo": (_, { text }, { cache }) => {
const query = gql `
query GetTodos {
todos @client {
id
text
completed
}
}
`
const previous = cache.readQuery ({ query })
const newTodo = {
"__typename": "TodoItem",
"completed": false,
"id": nextTodoId++,
text
}
const data = {
"todos": previous.todos.concat ([newTodo])
}
cache.writeData ({ data })
return newTodo
},
"toggleTodo": (_, variables, { cache }) => {
const id = `TodoItem:${variables.id}`
const fragment = gql `
fragment completeTodo on TodoItem {
completed
}
`
const todo = cache.readFragment ({ fragment, id })
const data = { ... todo, "completed": !todo.completed }
cache.writeData ({ data, id })
return null
}
}
}
const typeDefs = `
type Todo {
id: Int!
text: String!
completed: Boolean!
}
type Mutation {
addTodo(text: String!): Todo
toggleTodo(id: Int!): Todo
}
type Query {
visibilityFilter: String
todos: [Todo]
}
`
const stateLink = withClientState ({
"cache": c,
defaults,
resolvers,
typeDefs
})
const client = new ApolloClient ({
"cache": c,
"link": stateLink
})
client
.query ({ "query": gql `{ todos { text } }` })
.then (({ data }) => console.log (data))
"use strict"
require = require ("esm") (module)
module.exports = require ("./index.mjs")
export default {}
{
"babel": {
"plugins": [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-transform-modules-umd"
]
},
"dependencies": {
"esm": "^3"
},
"devDependencies": {
"@babel/cli": "latest",
"@babel/core": "latest",
"@babel/plugin-syntax-dynamic-import": "latest",
"@babel/plugin-transform-modules-umd": "latest"
},
"engines": {
"node": ">=8"
},
"main": "index",
"module": "index.mjs",
"publishConfig": {
"access": "public"
},
"scripts": {
"prepublishOnly": "babel --ignore 'index.mjs,node_modules' --out-dir '.' --relative '**/*.mjs'"
}
}
const {
InMemoryCache
} = require ("apollo-cache-inmemory")
const {
ApolloClient
} = require ("apollo-client")
const {
SchemaLink
} = require ("apollo-link-schema")
const gql = require ("graphql-tag")
const {
makeExecutableSchema
} = require ("graphql-tools")
const typeDefs = `
type Query {
author: Person
}
type Person {
name: String
}
`
const resolvers = {
"Query": {
"author": () => ({
"name": "billy"
})
}
}
const schema = makeExecutableSchema ({
resolvers,
typeDefs
})
const client = new ApolloClient ({
"cache": new InMemoryCache (),
"link": new SchemaLink ({ schema }),
"ssr": true
})
client
.query ({ "query": gql `{ author { name } }` })
.then (({ data }) => console.log (data))
const {
ApolloServer
} = require ("apollo-server")
const {
GraphQLList,
GraphQLObjectType,
GraphQLSchema,
GraphQLString
} = require ("graphql")
const schema = new GraphQLSchema ({
"query": new GraphQLObjectType ({
"name": "Schema",
"fields": {
"files": {
"resolve": () => [{ "hello": "there "}, { "hello": "now" }],
"type": new GraphQLList (new GraphQLObjectType ({
"name": "Files",
"fields": {
"hello": {
"type": GraphQLString
}
}
}))
}
}
})
})
new ApolloServer ({ schema })
.listen ()
.then (({ url }) => console.log (`🚀 Server ready at ${url}`))
const { InMemoryCache } = require ("apollo-cache-inmemory")
const { ApolloClient } = require ("apollo-client")
const { HttpLink } = require ("apollo-link-http")
const gql = require ("graphql-tag")
const fetch = require ("node-fetch")
const client = new ApolloClient ({
"cache": new InMemoryCache (),
"link": new HttpLink ({
"fetch": fetch,
"uri": "https://q80vw8qjp.lp.gql.zone/graphql"
})
})
client
.query ({ "query": gql `{ hello }` })
.then (({ data }) => console.log (data.hello))
const { InMemoryCache } = require ("apollo-cache-inmemory")
const { ApolloClient } = require ("apollo-client")
const { ApolloLink } = require ("apollo-link")
const { SchemaLink } = require ("apollo-link-schema")
const { withClientState } = require ("apollo-link-state")
const gql = require ("graphql-tag")
const { makeExecutableSchema } = require ("graphql-tools")
const cache = new InMemoryCache ()
const resolvers = {
"Query": {
"author": () => ({
"name": "billy"
})
}
}
const typeDefs = `
type Query {
author: Person
}
type Person {
name: String
}
`
const stateLink = withClientState ({
cache,
resolvers,
typeDefs
})
const schema = makeExecutableSchema ({
resolvers,
typeDefs
})
const client = new ApolloClient ({
"cache": new InMemoryCache (),
"link": ApolloLink.from ([
stateLink,
new SchemaLink ({ schema })
]),
"ssr": true
})
client
.query ({ "query": gql `{ author { name } }` })
.then (({ data }) => console.log (data))
// import { ApolloClient } from 'apollo-client';
// import { InMemoryCache } from 'apollo-cache-inmemory';
// import { ApolloLink } from 'apollo-link';
// import { withClientState } from 'apollo-link-state';
// import { HttpLink } from 'apollo-link-http';
// import { defaults, resolvers } from './resolvers/todos';
// const cache = new InMemoryCache();
// const stateLink = withClientState({ resolvers, cache, defaults });
// const client = new ApolloClient({
// cache,
// link: ApolloLink.from([stateLink, new HttpLink()]),
// });
// Your state link should come before your HttpLink so local queries and mutations are intercepted before they hit the network.
// fieldName: (obj, args, context, info) => result;
// (parent, args, context, info)
// (root, args, ctx/data/more, info)
// https://dev-blog.apollodata.com/the-future-of-state-management-dd410864cae2
module.exports = class {
constructor (options) {
this.options = options
}
apply (compiler) {
compiler.hooks.thisCompilation.tap ("AmoryStaticSitePlugin", (compilation) => {
compilation.hooks.optimizeAssets.tapAsync ("AmoryStaticSitePlugin", (assets, done) => {
done ()
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment