Skip to content

Instantly share code, notes, and snippets.

View LawJolla's full-sized avatar

Dennis Walsh LawJolla

View GitHub Profile
@LawJolla
LawJolla / schema.ex
Created April 10, 2017 23:44
Absinthe Root Query Pagination
defmodule WkGraphql.Schema do
use Absinthe.Schema
import_types WkGraphql.Schema.Types
query do
@desc "Get a user"
field :user, :user do
arg :id, non_null(:integer)
resolve &WkGraphql.UserResolver.find/2
@LawJolla
LawJolla / gatsby-node.js
Created September 9, 2017 16:08
Gatsby + GraphQL for Gatsby-node
const GraphQLClient = require('graphql-request').GraphQLClient;
const crypto = require('crypto');
const path = require('path');
module.exports.sourceNodes = async (
{ boundActionCreators, getNode, hasNodeChanged },
{ endpoint, token }
) => {
const { createNode } = boundActionCreators
@LawJolla
LawJolla / gatsby-node.js
Last active June 25, 2018 18:52
Gatsby + API / GraphQL Call
const GraphQLClient = require('graphql-request').GraphQLClient;
const crypto = require('crypto');
const path = require('path');
module.exports.sourceNodes = async ({ boundActionCreators }) => {
const { createNode } = boundActionCreators
const client = new GraphQLClient(process.env.GRAPHCOOL_API)
const data = await client.request(vehicles)
@LawJolla
LawJolla / gatsby-node.js
Created September 18, 2017 05:15
Gatsby Create pages from API
// Lightweight GraphQL generic client
const GraphQLClient = require('graphql-request').GraphQLClient;
const crypto = require('crypto');
const path = require('path');
const api = require('./api_constants');
//GraphQL query string to get all inventory, filtering by a specific dealer
const vehicles = `
{
allDealerships(filter:{ name:"Wheel Kinetics"}) {
@LawJolla
LawJolla / gatsby-ssr.js
Created September 18, 2017 06:19
Apollo + Gatsby SSR
import React from 'react'
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
import ApolloClient, { createNetworkInterface, } from 'apollo-client'
import { ApolloProvider, getDataFromTree } from 'react-apollo'
// Apollo Setup
const networkInterface = createNetworkInterface({
uri: process.env.GRAPHCOOL_API
})
@LawJolla
LawJolla / gatsby-browser.js
Created September 18, 2017 06:34
Gatsby + Apollo Browser API
import React from 'react'
import { Router } from 'react-router-dom'
import ApolloClient, { createNetworkInterface } from 'apollo-client'
import { ApolloProvider } from 'react-apollo'
import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-ws'
const networkInterface = createNetworkInterface({
// endpoint comes from .env.development/production
uri: process.env.GRAPHCOOL_API
})
@LawJolla
LawJolla / gatsby-ssr.js
Created October 2, 2017 19:34
Gatsby + Apollo + Styled Components SSR
import React from "react";
import { renderToString } from "react-dom/server";
import ApolloClient, { createNetworkInterface } from "apollo-client";
import { ApolloProvider, getDataFromTree } from "react-apollo";
import { ServerStyleSheet, StyleSheetManager } from "styled-components";
// function to generate hydrated state for client side Apollo
function makeApolloState(ssrClient) {
const state = { apollo: ssrClient.getInitialState() }
// appends apollo state to the global client window object
@LawJolla
LawJolla / scratch.graphql
Last active March 1, 2018 14:49
Directive Permission Schema
type Query {
vehicles(dealership: ID!): [Vehicle!]!
}
type Mutation {
updateVehicleAskingPrice(id: ID!, askingPrice: Int!): Vehicle
}
type Vehicle {
id: ID!
@LawJolla
LawJolla / scratch_33.es6
Created February 26, 2018 23:24
updateAskingPrice with permissions
const Mutation = {
updateVehicleAskingPrice: async (parent, { id, askingPrice }, context, info) => {
const userId = getUserId(context)
const isRequestingUserManager = await context.db.exists.User({
id: userId,
role: `MANAGER`
})
if (isRequestingUserManager) {
return await context.db.mutation.updateVehicle({
where: { id },
@LawJolla
LawJolla / scratch_33.es6
Created February 26, 2018 23:26
Query Schema with permissions
const Query = {
vehicles: async (parent, args, context, info) => {
const vehicles = await context.db.query.vehicles({
where: { dealership: args.id }
})
const user = getUser(context)
return vehicles.map(vehicle => ({
...vehicle,
costBasis: