Remove redundancy

This commit is contained in:
Robert Schäfer 2019-02-18 13:25:11 +01:00
parent f17242b824
commit f024bd3821
3 changed files with 30 additions and 48 deletions

View File

@ -1,7 +1,4 @@
import gql from 'graphql-tag'
import asyncForEach from '../../helpers/asyncForEach'
const seed = {
export default {
Badge: require('./badges.js').default,
Category: require('./categories.js').default,
Tags: require('./tags.js').default,
@ -19,25 +16,3 @@ const seed = {
// Reports: require('./reports.js').default
}
let data = {}
export default async function (client) {
// iterate through seeds
await asyncForEach(Object.keys(seed), async key => {
const mutations = seed[key]
try {
const res = await client
.mutate({
mutation: gql(mutations(data))
})
data[key] = Object.assign(data[key] || {}, res.data)
} catch (err) {
/* eslint-disable-next-line no-console */
console.error(err)
process.exit(1)
}
})
/* eslint-disable-next-line no-console */
console.log('Seeded Data...')
}

View File

@ -8,11 +8,7 @@ import fetch from 'node-fetch'
dotenv.config()
if (process.env.NODE_ENV === 'production') {
throw new Error('YOU CAN`T RUN FACTORIES IN PRODUCTION MODE')
}
const client = new ApolloClient({
const apolloClient = new ApolloClient({
link: new HttpLink({ uri: 'http://localhost:4001', fetch }),
cache: new InMemoryCache()
})
@ -28,7 +24,7 @@ const buildMutation = (model, parameters) => {
}
const create = (model, parameters) => {
return client.mutate({ mutation: gql(buildMutation(model, parameters)) })
return apolloClient.mutate({ mutation: gql(buildMutation(model, parameters)) })
}
const cleanDatabase = async () => {
@ -44,6 +40,8 @@ const cleanDatabase = async () => {
}
export {
driver,
apolloClient,
create,
buildMutation,
cleanDatabase

View File

@ -1,19 +1,28 @@
import ApolloClient from 'apollo-client'
import dotenv from 'dotenv'
import fetch from 'node-fetch'
import { HttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import Seed from './data/index'
import { apolloClient } from './factories'
import gql from 'graphql-tag'
import asyncForEach from '../helpers/asyncForEach'
import seed from './data'
dotenv.config()
(async function () {
if (process.env.NODE_ENV === 'production') {
throw new Error('YOU CAN`T SEED IN PRODUCTION MODE')
}
// prefer factories
let data = {}
// legacy seeds
await asyncForEach(Object.keys(seed), async key => {
const mutations = seed[key]
try {
const res = await apolloClient
.mutate({
mutation: gql(mutations(data))
})
data[key] = Object.assign(data[key] || {}, res.data)
} catch (err) {
/* eslint-disable-next-line no-console */
console.error(err)
process.exit(1)
}
})
/* eslint-disable-next-line no-console */
console.log('Seeded Data...')
})()
const client = new ApolloClient({
link: new HttpLink({ uri: process.env.GRAPHQL_URI, fetch }),
cache: new InMemoryCache()
})
Seed(client)