simple-graphql-client.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. const response = JSON.parse(body);
  117. if (response.errors && response.errors.length) {
  118. const error = response.errors[0];
  119. console.log(JSON.stringify(error.extensions, null, 2));
  120. throw new Error(error.message);
  121. }
  122. resolve(response.data);
  123. });
  124. curl.on('error', err => {
  125. curl.close();
  126. console.log(err);
  127. reject(err);
  128. });
  129. });
  130. }
  131. async asUserWithCredentials(username: string, password: string) {
  132. // first log out as the current user
  133. if (this.authToken) {
  134. await this.query(
  135. gql`
  136. mutation {
  137. logout
  138. }
  139. `,
  140. );
  141. }
  142. const result = await this.query(
  143. gql`
  144. mutation($username: String!, $password: String!) {
  145. login(username: $username, password: $password) {
  146. user {
  147. id
  148. identifier
  149. channelTokens
  150. }
  151. }
  152. }
  153. `,
  154. {
  155. username,
  156. password,
  157. },
  158. );
  159. }
  160. async asSuperAdmin() {
  161. await this.asUserWithCredentials(SUPER_ADMIN_USER_IDENTIFIER, SUPER_ADMIN_USER_PASSWORD);
  162. }
  163. async asAnonymousUser() {
  164. await this.query(
  165. gql`
  166. mutation {
  167. logout
  168. }
  169. `,
  170. );
  171. }
  172. private setHeaders() {
  173. const headers: any = {
  174. [getConfig().channelTokenKey]: this.channelToken,
  175. };
  176. if (this.authToken) {
  177. headers.Authorization = `Bearer ${this.authToken}`;
  178. }
  179. this.client.setHeaders(headers);
  180. }
  181. }