From 0811ca82485836de09bf491f9ab207260edb88ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Thu, 18 Apr 2019 16:23:28 +0200 Subject: [PATCH] Massive refactoring of factories cc @mattwr18 @tirokk @ulfgebhardt @appinteractive The factories don't use string interpolation now but they use variables. This resolves some errors of escaping strings when you send html along with `post.content`. It is much cleaner, too. --- backend/src/seed/factories/badges.js | 29 +++++----- backend/src/seed/factories/categories.js | 19 +++---- backend/src/seed/factories/comments.js | 24 ++++----- backend/src/seed/factories/index.js | 4 +- backend/src/seed/factories/notifications.js | 19 ++++--- backend/src/seed/factories/organizations.js | 25 ++++----- backend/src/seed/factories/posts.js | 40 +++++++++----- backend/src/seed/factories/reports.js | 20 +++---- backend/src/seed/factories/tags.js | 20 +++---- backend/src/seed/factories/users.js | 60 ++++++++++++--------- 10 files changed, 144 insertions(+), 116 deletions(-) diff --git a/backend/src/seed/factories/badges.js b/backend/src/seed/factories/badges.js index e24a67c21..6f5f8d69a 100644 --- a/backend/src/seed/factories/badges.js +++ b/backend/src/seed/factories/badges.js @@ -3,21 +3,26 @@ import uuid from 'uuid/v4' export default function (params) { const { id = uuid(), - key, + key = '', type = 'crowdfunding', status = 'permanent', - icon + icon = '/img/badges/indiegogo_en_panda.svg' } = params - return ` - mutation { - CreateBadge( - id: "${id}", - key: "${key}", - type: ${type}, - status: ${status}, - icon: "${icon}" - ) { id } + return { + mutation: ` + mutation( + $id: ID + $key: String! + $type: BadgeTypeEnum! + $status: BadgeStatusEnum! + $icon: String! + ) { + CreateBadge(id: $id, key: $key, type: $type, status: $status, icon: $icon) { + id + } + } + `, + variables: { id, key, type, status, icon } } - ` } diff --git a/backend/src/seed/factories/categories.js b/backend/src/seed/factories/categories.js index a4b448f4b..5c1b3ce10 100644 --- a/backend/src/seed/factories/categories.js +++ b/backend/src/seed/factories/categories.js @@ -8,14 +8,15 @@ export default function (params) { icon } = params - return ` - mutation { - CreateCategory( - id: "${id}", - name: "${name}", - slug: "${slug}", - icon: "${icon}" - ) { id, name } + return { + mutation: ` + mutation($id: ID, $name: String!, $slug: String, $icon: String!) { + CreateCategory(id: $id, name: $name, slug: $slug, icon: $icon) { + id + name + } } - ` + `, + variables: { id, name, slug, icon } + } } diff --git a/backend/src/seed/factories/comments.js b/backend/src/seed/factories/comments.js index 92dca5b14..9964d0559 100644 --- a/backend/src/seed/factories/comments.js +++ b/backend/src/seed/factories/comments.js @@ -7,19 +7,17 @@ export default function (params) { content = [ faker.lorem.sentence(), faker.lorem.sentence() - ].join('. '), - disabled = false, - deleted = false + ].join('. ') } = params - return ` - mutation { - CreateComment( - id: "${id}", - content: "${content}", - disabled: ${disabled}, - deleted: ${deleted} - ) { id } - } - ` + return { + mutation: ` + mutation($id: ID!, $content: String!) { + CreateComment(id: $id, content: $content) { + id + } + } + `, + variables: { id, content } + } } diff --git a/backend/src/seed/factories/index.js b/backend/src/seed/factories/index.js index 7c23226cb..a0cb310ab 100644 --- a/backend/src/seed/factories/index.js +++ b/backend/src/seed/factories/index.js @@ -71,8 +71,8 @@ export default function Factory (options = {}) { return this }, async create (node, properties) { - const mutation = this.factories[node](properties) - this.lastResponse = await this.graphQLClient.request(mutation) + const { mutation, variables } = this.factories[node](properties) + this.lastResponse = await this.graphQLClient.request(mutation, variables) return this }, async relate (node, relationship, properties) { diff --git a/backend/src/seed/factories/notifications.js b/backend/src/seed/factories/notifications.js index 2e2abdd55..f7797200f 100644 --- a/backend/src/seed/factories/notifications.js +++ b/backend/src/seed/factories/notifications.js @@ -6,12 +6,15 @@ export default function (params) { read = false } = params - return ` - mutation { - CreateNotification( - id: "${id}", - read: ${read}, - ) { id, read } - } - ` + return { + mutation: ` + mutation($id: ID, $read: Boolean) { + CreateNotification(id: $id, read: $read) { + id + read + } + } + `, + variables: { id, read } + } } diff --git a/backend/src/seed/factories/organizations.js b/backend/src/seed/factories/organizations.js index e0b2e52d4..dd4100b26 100644 --- a/backend/src/seed/factories/organizations.js +++ b/backend/src/seed/factories/organizations.js @@ -5,20 +5,17 @@ export default function create (params) { const { id = uuid(), name = faker.company.companyName(), - description = faker.company.catchPhrase(), - disabled = false, - deleted = false + description = faker.company.catchPhrase() } = params - return ` - mutation { - CreateOrganization( - id: "${id}", - name: "${name}", - description: "${description}", - disabled: ${disabled}, - deleted: ${deleted} - ) { name } - } - ` + return { + mutation: ` + mutation($id: ID!, $name: String!, $description: String!) { + CreateOrganization(id: $id, name: $name, description: $description) { + name + } + } + `, + variables: { id, name, description } + } } diff --git a/backend/src/seed/factories/posts.js b/backend/src/seed/factories/posts.js index 83d5844d7..cbc73dbf8 100644 --- a/backend/src/seed/factories/posts.js +++ b/backend/src/seed/factories/posts.js @@ -18,17 +18,31 @@ export default function (params) { deleted = false } = params - return ` - mutation { - CreatePost( - id: "${id}", - slug: "${slug}", - title: "${title}", - content: "${content}", - image: "${image}", - visibility: ${visibility}, - deleted: ${deleted} - ) { title, content } - } - ` + return { + mutation: ` + mutation( + $id: ID! + $slug: String + $title: String! + $content: String! + $image: String + $visibility: VisibilityEnum + $deleted: Boolean + ) { + CreatePost( + id: $id + slug: $slug + title: $title + content: $content + image: $image + visibility: $visibility + deleted: $deleted + ) { + title + content + } + } + `, + variables: { id, slug, title, content, image, visibility, deleted } + } } diff --git a/backend/src/seed/factories/reports.js b/backend/src/seed/factories/reports.js index ccf5299bd..130c20c37 100644 --- a/backend/src/seed/factories/reports.js +++ b/backend/src/seed/factories/reports.js @@ -6,15 +6,15 @@ export default function create (params) { id } = params - return ` - mutation { - report( - description: "${description}", - id: "${id}", - ) { - id, - createdAt + return { + mutation: ` + mutation($id: ID!, $description: String!) { + report(description: $description, id: $id) { + id + createdAt + } } - } - ` + `, + variables: { id, description } + } } diff --git a/backend/src/seed/factories/tags.js b/backend/src/seed/factories/tags.js index c603c5629..558b68957 100644 --- a/backend/src/seed/factories/tags.js +++ b/backend/src/seed/factories/tags.js @@ -3,15 +3,17 @@ import uuid from 'uuid/v4' export default function (params) { const { id = uuid(), - name + name = '#human-connection' } = params - return ` - mutation { - CreateTag( - id: "${id}", - name: "${name}", - ) { name } - } - ` + return { + mutation: ` + mutation($id: ID!, $name: String!) { + CreateTag(id: $id, name: $name) { + name + } + } + `, + variables: { id, name } + } } diff --git a/backend/src/seed/factories/users.js b/backend/src/seed/factories/users.js index 9fe957515..1bca0e243 100644 --- a/backend/src/seed/factories/users.js +++ b/backend/src/seed/factories/users.js @@ -10,34 +10,42 @@ export default function create (params) { password = '1234', role = 'user', avatar = faker.internet.avatar(), - about = faker.lorem.paragraph(), - disabled = false, - deleted = false + about = faker.lorem.paragraph() } = params - return ` - mutation { - CreateUser( - id: "${id}", - name: "${name}", - slug: "${slug}", - password: "${password}", - email: "${email}", - avatar: "${avatar}", - about: "${about}", - role: ${role}, - disabled: ${disabled}, - deleted: ${deleted} + return { + mutation: ` + mutation( + $id: ID! + $name: String + $slug: String + $password: String! + $email: String + $avatar: String + $about: String + $role: UserGroupEnum ) { - id - name - slug - email - avatar - role - deleted - disabled + CreateUser( + id: $id + name: $name + slug: $slug + password: $password + email: $email + avatar: $avatar + about: $about + role: $role + ) { + id + name + slug + email + avatar + role + deleted + disabled + } } - } - ` + `, + variables: { id, name, slug, password, email, avatar, about, role } + } }