|
@@ -27,7 +27,10 @@ export type QueryParams = { [key: string]: string | number };
|
|
|
|
|
|
|
|
// tslint:disable:no-console
|
|
// tslint:disable:no-console
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * @description
|
|
|
* A minimalistic GraphQL client for populating and querying test data.
|
|
* A minimalistic GraphQL client for populating and querying test data.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @docsCategory testing
|
|
|
*/
|
|
*/
|
|
|
export class SimpleGraphQLClient {
|
|
export class SimpleGraphQLClient {
|
|
|
private authToken: string;
|
|
private authToken: string;
|
|
@@ -36,20 +39,30 @@ export class SimpleGraphQLClient {
|
|
|
|
|
|
|
|
constructor(private vendureConfig: Required<VendureConfig>, private apiUrl: string = '') {}
|
|
constructor(private vendureConfig: Required<VendureConfig>, private apiUrl: string = '') {}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @description
|
|
|
|
|
+ * Sets the authToken to be used in each GraphQL request.
|
|
|
|
|
+ */
|
|
|
setAuthToken(token: string) {
|
|
setAuthToken(token: string) {
|
|
|
this.authToken = token;
|
|
this.authToken = token;
|
|
|
this.headers.Authorization = `Bearer ${this.authToken}`;
|
|
this.headers.Authorization = `Bearer ${this.authToken}`;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @description
|
|
|
|
|
+ * Returns the authToken currently being used.
|
|
|
|
|
+ */
|
|
|
getAuthToken(): string {
|
|
getAuthToken(): string {
|
|
|
return this.authToken;
|
|
return this.authToken;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /** @internal */
|
|
|
setChannelToken(token: string) {
|
|
setChannelToken(token: string) {
|
|
|
this.headers[this.vendureConfig.channelTokenKey] = token;
|
|
this.headers[this.vendureConfig.channelTokenKey] = token;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * @description
|
|
|
* Performs both query and mutation operations.
|
|
* Performs both query and mutation operations.
|
|
|
*/
|
|
*/
|
|
|
async query<T = any, V = Record<string, any>>(
|
|
async query<T = any, V = Record<string, any>>(
|
|
@@ -71,11 +84,19 @@ export class SimpleGraphQLClient {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @description
|
|
|
|
|
+ * Performs a query or mutation and returns the resulting status code.
|
|
|
|
|
+ */
|
|
|
async queryStatus<T = any, V = Record<string, any>>(query: DocumentNode, variables?: V): Promise<number> {
|
|
async queryStatus<T = any, V = Record<string, any>>(query: DocumentNode, variables?: V): Promise<number> {
|
|
|
const response = await this.request(query, variables);
|
|
const response = await this.request(query, variables);
|
|
|
return response.status;
|
|
return response.status;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @description
|
|
|
|
|
+ * Attemps to log in with the specified credentials.
|
|
|
|
|
+ */
|
|
|
async asUserWithCredentials(username: string, password: string) {
|
|
async asUserWithCredentials(username: string, password: string) {
|
|
|
// first log out as the current user
|
|
// first log out as the current user
|
|
|
if (this.authToken) {
|
|
if (this.authToken) {
|
|
@@ -91,10 +112,18 @@ export class SimpleGraphQLClient {
|
|
|
return result.login;
|
|
return result.login;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @description
|
|
|
|
|
+ * Logs in as the SuperAdmin user.
|
|
|
|
|
+ */
|
|
|
async asSuperAdmin() {
|
|
async asSuperAdmin() {
|
|
|
await this.asUserWithCredentials(SUPER_ADMIN_USER_IDENTIFIER, SUPER_ADMIN_USER_PASSWORD);
|
|
await this.asUserWithCredentials(SUPER_ADMIN_USER_IDENTIFIER, SUPER_ADMIN_USER_PASSWORD);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @description
|
|
|
|
|
+ * Logs out so that the client is then treated as an anonymous user.
|
|
|
|
|
+ */
|
|
|
async asAnonymousUser() {
|
|
async asAnonymousUser() {
|
|
|
await this.query(
|
|
await this.query(
|
|
|
gql`
|
|
gql`
|
|
@@ -140,6 +169,7 @@ export class SimpleGraphQLClient {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * @description
|
|
|
* Uses curl to post a multipart/form-data request to the server. Due to differences between the Node and browser
|
|
* Uses curl to post a multipart/form-data request to the server. Due to differences between the Node and browser
|
|
|
* environments, we cannot just use an existing library like apollo-upload-client.
|
|
* environments, we cannot just use an existing library like apollo-upload-client.
|
|
|
*
|
|
*
|