Skip to content

Instantly share code, notes, and snippets.

View stubailo's full-sized avatar
📈
Working on Flipturn!

Sashko Stubailo stubailo

📈
Working on Flipturn!
View GitHub Profile
const fetch = require("node-fetch");
const { introspectionQuery } = require("graphql");
const fs = require("fs");
fetch("https://1jzxrj179.lp.gql.zone/graphql", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query: introspectionQuery })
})
.then(res => res.json())
@stubailo
stubailo / ErrorProvider.jsx
Created November 26, 2018 17:14
A provider for mocking errors with Apollo Client
export const ErrorProvider = (props) => {
// This is just a link that swallows all operations and returns the same thing
// for every request: The specified error.
const link = new ApolloLink((operation) => {
return new Observable((observer) => {
observer.next({
errors: props.graphQLErrors || [
{message: 'Unspecified error from ErrorProvider.'},
],
});
@stubailo
stubailo / fetch-graphql.js
Created September 5, 2017 08:15
Call a GraphQL API with fetch
require('isomorphic-fetch');
fetch('https://1jzxrj179.lp.gql.zone/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: '{ posts { title } }' }),
})
.then(res => res.json())
.then(res => console.log(res.data));
@stubailo
stubailo / graphql-field-finder.js
Last active November 9, 2020 18:55
Find all GraphQL queries in your codebase that use a certain field
// This is a script that looks for usage of a specific field in GraphQL
// queries in your codebase.
//
// First, add a .graphqlconfig in your directory pointing to your schema
// Then, run the script with:
//
// node graphql-field-finder.js Field.name
//
// It will output a list of files and queries that contain the field you're
// looking for:
@stubailo
stubailo / graphql-subscriptions.js
Last active October 12, 2020 22:40
A very basic demo of GraphQL subscriptions with the graphql-subscriptions package
// npm install graphql graphql-tools graphql-subscriptions
const { PubSub, SubscriptionManager } = require('graphql-subscriptions');
const { makeExecutableSchema } = require('graphql-tools');
// Our "database"
const messages = [];
// Minimal schema
const typeDefs = `
type Query {
@stubailo
stubailo / result.json
Created May 15, 2018 23:43
Introspection query result
{
"__schema": {
"queryType": {
"name": "Query"
},
"mutationType": {
"name": "Mutation"
},
"subscriptionType": null,
"types": [
const { buildClientSchema, printSchema } = require("graphql");
const fs = require("fs");
const introspectionSchemaResult = JSON.parse(fs.readFileSync("result.json"));
const graphqlSchemaObj = buildClientSchema(introspectionSchemaResult);
const sdlString = printSchema(graphqlSchemaObj);
@stubailo
stubailo / LoadingProvider.jsx
Created November 26, 2018 17:13
A provider that lets you mock loading state
const LoadingProvider = (props) => {
const link = new ApolloLink((operation) => {
return new Observable(() => {});
});
const client = new ApolloClient({
link,
cache: new InMemoryCache(),
});
@stubailo
stubailo / airport-code-to-lat-lng.json
Created February 23, 2020 17:40
Mapping of airport code to latitude, longitude pair
{
"0": [
-16.38360023498535,
-58.31719970703125
],
"UTK": [
11.222,
169.852005
],
"OCA": [
@stubailo
stubailo / graphql-subscriptions-client.js
Created October 2, 2016 16:51
Use subscriptions-transport-ws to run a GraphQL subscription from a JavaScript client.
// To run this code yourself:
// 1. Download and run the demo server: https://github.com/apollostack/frontpage-server
// 2. Use create-react-app to create a build system
// 3. npm install subscriptions-transport-ws
// 4. Replace all of the code in src/ with just this file
import { Client } from 'subscriptions-transport-ws';
const client = new Client('ws://localhost:8090');