mock-data.service.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { CreateAddressInput, CreateCustomerInput } from '@vendure/common/lib/generated-types';
  2. import faker from 'faker/locale/en_GB';
  3. import gql from 'graphql-tag';
  4. import { SimpleGraphQLClient } from '../simple-graphql-client';
  5. /* eslint-disable no-console */
  6. /**
  7. * A service for creating mock data via the GraphQL API.
  8. */
  9. export class MockDataService {
  10. apiUrl: string;
  11. constructor(private client: SimpleGraphQLClient, private logging = true) {
  12. // make the generated results deterministic
  13. faker.seed(1);
  14. }
  15. static getCustomers(
  16. count: number,
  17. ): Array<{ customer: CreateCustomerInput; address: CreateAddressInput }> {
  18. faker.seed(1);
  19. const results: Array<{ customer: CreateCustomerInput; address: CreateAddressInput }> = [];
  20. for (let i = 0; i < count; i++) {
  21. const firstName = faker.name.firstName();
  22. const lastName = faker.name.lastName();
  23. const customer: CreateCustomerInput = {
  24. firstName,
  25. lastName,
  26. emailAddress: faker.internet.email(firstName, lastName),
  27. phoneNumber: faker.phone.phoneNumber(),
  28. };
  29. const address: CreateAddressInput = {
  30. fullName: `${firstName} ${lastName}`,
  31. streetLine1: faker.address.streetAddress(),
  32. city: faker.address.city(),
  33. province: faker.address.county(),
  34. postalCode: faker.address.zipCode(),
  35. countryCode: 'GB',
  36. };
  37. results.push({ customer, address });
  38. }
  39. return results;
  40. }
  41. /**
  42. * @deprecated
  43. * Use `MockDataService.getCustomers()` and create customers directly with CustomerService.
  44. */
  45. async populateCustomers(count: number = 5): Promise<any> {
  46. for (let i = 0; i < count; i++) {
  47. const firstName = faker.name.firstName();
  48. const lastName = faker.name.lastName();
  49. const query1 = gql`
  50. mutation CreateCustomer($input: CreateCustomerInput!, $password: String) {
  51. createCustomer(input: $input, password: $password) {
  52. ... on Customer {
  53. id
  54. emailAddress
  55. }
  56. }
  57. }
  58. `;
  59. const variables1 = {
  60. input: {
  61. firstName,
  62. lastName,
  63. emailAddress: faker.internet.email(firstName, lastName),
  64. phoneNumber: faker.phone.phoneNumber(),
  65. },
  66. password: 'test',
  67. };
  68. const customer: { id: string; emailAddress: string } | void = await this.client
  69. .query(query1, variables1)
  70. .then(
  71. (data: any) => data.createCustomer,
  72. err => this.log(err),
  73. );
  74. if (customer) {
  75. const query2 = gql`
  76. mutation ($customerId: ID!, $input: CreateAddressInput!) {
  77. createCustomerAddress(customerId: $customerId, input: $input) {
  78. id
  79. streetLine1
  80. }
  81. }
  82. `;
  83. const variables2 = {
  84. input: {
  85. fullName: `${firstName} ${lastName}`,
  86. streetLine1: faker.address.streetAddress(),
  87. city: faker.address.city(),
  88. province: faker.address.county(),
  89. postalCode: faker.address.zipCode(),
  90. countryCode: 'GB',
  91. },
  92. customerId: customer.id,
  93. };
  94. await this.client.query(query2, variables2).catch(err => this.log(err));
  95. }
  96. }
  97. this.log(`Created ${count} Customers`);
  98. }
  99. private log(...args: any[]) {
  100. if (this.logging) {
  101. console.log(...args);
  102. }
  103. }
  104. }