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
@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-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 / story.jsx
Last active November 26, 2018 17:27
How to use the ApolloMockingProvider in a storybook example
storiesOf('TodoList', module)
.add('renders a list', () => {
const customResolvers = {
Todo: () => ({
text: 'My todo item text',
}),
};
return (
<ApolloMockingProvider customResolvers={customResolvers}>
@stubailo
stubailo / test.jsx
Last active November 26, 2018 17:27
A Jest test with ApolloMockingProvider
it('renders a list', async () => {
const customResolvers = {
Todo: () => ({
text: 'My todo item',
}),
};
const wrapper = mount(
<ApolloMockingProvider customResolvers={customResolvers}>
<TodoList />
@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 / 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 / ApolloMockingProvider.jsx
Created November 26, 2018 17:12
The implementation of the ApolloMockingProvider
const ApolloMockingProvider = (props) => {
const mocks = mergeResolvers(globalMocks, props.customResolvers);
addMockFunctionsToSchema({ schema, mocks });
const client = new ApolloClient({
link: new SchemaLink({ schema }),
cache: new InMemoryCache(),
});
@stubailo
stubailo / apollo-mocking-provider.jsx
Created November 26, 2018 17:11
How we use a mocking provider to mock data
import TodoList from './TodoList';
// Specify some resolvers for this instance
const customResolvers = {
Query: () => ({
todoItems: [
{ completed: true },
{ completed: false },
]
})
@stubailo
stubailo / graphql-tools-mock-resolvers.js
Created November 26, 2018 17:10
Customizing graphql-tools mocks
import faker from 'faker';
const mocks = {
Todo: () => ({
text: () => faker.sentence(),
completed: () => faker.random.boolean(),
}),
User: () => ({
name: () => faker.name.findName()
})
@stubailo
stubailo / apollo-link-schema.js
Created November 26, 2018 17:09
How to import your mocked schema and pass it to Apollo Client