simple-graphql-client.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { DocumentNode } from 'graphql';
  2. import { GraphQLClient } from 'graphql-request';
  3. import gql from 'graphql-tag';
  4. import { print } from 'graphql/language/printer';
  5. import { Curl } from 'node-libcurl';
  6. import { CREATE_ASSETS } from '../../admin-ui/src/app/data/definitions/product-definitions';
  7. import { CreateAssets, ImportInfo } from '../../shared/generated-types';
  8. import { SUPER_ADMIN_USER_IDENTIFIER, SUPER_ADMIN_USER_PASSWORD } from '../../shared/shared-constants';
  9. import { getConfig } from '../src/config/config-helpers';
  10. import { createUploadPostData } from './create-upload-post-data';
  11. // tslint:disable:no-console
  12. /**
  13. * A minimalistic GraphQL client for populating and querying test data.
  14. */
  15. export class SimpleGraphQLClient {
  16. private client: GraphQLClient;
  17. private authToken: string;
  18. private channelToken: string;
  19. constructor(private apiUrl: string = '') {
  20. this.client = new GraphQLClient(apiUrl);
  21. }
  22. setAuthToken(token: string) {
  23. this.authToken = token;
  24. this.setHeaders();
  25. }
  26. getAuthToken(): string {
  27. return this.authToken;
  28. }
  29. setChannelToken(token: string) {
  30. this.channelToken = token;
  31. this.setHeaders();
  32. }
  33. /**
  34. * Performs both query and mutation operations.
  35. */
  36. async query<T = any, V = Record<string, any>>(query: DocumentNode, variables?: V): Promise<T> {
  37. const queryString = print(query);
  38. const result = await this.client.rawRequest<T>(queryString, variables);
  39. const authToken = result.headers.get(getConfig().authOptions.authTokenHeaderKey);
  40. if (authToken != null) {
  41. this.setAuthToken(authToken);
  42. }
  43. return result.data as T;
  44. }
  45. async queryStatus<T = any, V = Record<string, any>>(query: DocumentNode, variables?: V): Promise<number> {
  46. const queryString = print(query);
  47. const result = await this.client.rawRequest<T>(queryString, variables);
  48. return result.status;
  49. }
  50. uploadAssets(filePaths: string[]): Promise<CreateAssets.Mutation> {
  51. return this.fileUploadMutation({
  52. mutation: CREATE_ASSETS,
  53. filePaths,
  54. mapVariables: fp => ({
  55. input: fp.map(() => ({ file: null })),
  56. }),
  57. });
  58. }
  59. importProducts(csvFilePath: string): Promise<{ importProducts: ImportInfo }> {
  60. return this.fileUploadMutation({
  61. mutation: gql`
  62. mutation ImportProducts($csvFile: Upload!) {
  63. importProducts(csvFile: $csvFile) {
  64. importedCount
  65. errors
  66. }
  67. }
  68. `,
  69. filePaths: [csvFilePath],
  70. mapVariables: () => ({ csvFile: null }),
  71. });
  72. }
  73. /**
  74. * Uses curl to post a multipart/form-data request to the server. Due to differences between the Node and browser
  75. * environments, we cannot just use an existing library like apollo-upload-client.
  76. *
  77. * Upload spec: https://github.com/jaydenseric/graphql-multipart-request-spec
  78. * Discussion of issue: https://github.com/jaydenseric/apollo-upload-client/issues/32
  79. */
  80. private fileUploadMutation(options: {
  81. mutation: DocumentNode;
  82. filePaths: string[];
  83. mapVariables: (filePaths: string[]) => any;
  84. }): Promise<any> {
  85. const { mutation, filePaths, mapVariables } = options;
  86. return new Promise((resolve, reject) => {
  87. const curl = new Curl();
  88. const postData = createUploadPostData(mutation, filePaths, mapVariables);
  89. const processedPostData = [
  90. {
  91. name: 'operations',
  92. contents: JSON.stringify(postData.operations),
  93. },
  94. {
  95. name: 'map',
  96. contents:
  97. '{' +
  98. Object.entries(postData.map)
  99. .map(([i, path]) => `"${i}":["${path}"]`)
  100. .join(',') +
  101. '}',
  102. },
  103. ...postData.filePaths,
  104. ];
  105. curl.setOpt(Curl.option.URL, this.apiUrl);
  106. curl.setOpt(Curl.option.VERBOSE, false);
  107. curl.setOpt(Curl.option.TIMEOUT_MS, 30000);
  108. curl.setOpt(Curl.option.HTTPPOST, processedPostData);
  109. curl.setOpt(Curl.option.HTTPHEADER, [
  110. `Authorization: Bearer ${this.authToken}`,
  111. `${getConfig().channelTokenKey}: ${this.channelToken}`,
  112. ]);
  113. curl.perform();
  114. curl.on('end', (statusCode, body) => {
  115. curl.close();
  116. resolve(JSON.parse(body).data);
  117. });
  118. curl.on('error', err => {
  119. curl.close();
  120. console.log(err);
  121. reject(err);
  122. });
  123. });
  124. }
  125. async asUserWithCredentials(username: string, password: string) {
  126. // first log out as the current user
  127. if (this.authToken) {
  128. await this.query(
  129. gql`
  130. mutation {
  131. logout
  132. }
  133. `,
  134. );
  135. }
  136. const result = await this.query(
  137. gql`
  138. mutation($username: String!, $password: String!) {
  139. login(username: $username, password: $password) {
  140. user {
  141. id
  142. identifier
  143. channelTokens
  144. }
  145. }
  146. }
  147. `,
  148. {
  149. username,
  150. password,
  151. },
  152. );
  153. }
  154. async asSuperAdmin() {
  155. await this.asUserWithCredentials(SUPER_ADMIN_USER_IDENTIFIER, SUPER_ADMIN_USER_PASSWORD);
  156. }
  157. async asAnonymousUser() {
  158. await this.query(
  159. gql`
  160. mutation {
  161. logout
  162. }
  163. `,
  164. );
  165. }
  166. private setHeaders() {
  167. const headers: any = {
  168. [getConfig().channelTokenKey]: this.channelToken,
  169. };
  170. if (this.authToken) {
  171. headers.Authorization = `Bearer ${this.authToken}`;
  172. }
  173. this.client.setHeaders(headers);
  174. }
  175. }