Skip to content

Commit

Permalink
docs(testing): Add example for SimpleGraphQLClient.fileUploadMutation
Browse files Browse the repository at this point in the history
  • Loading branch information
toBeOfUse committed Nov 6, 2024
1 parent 660cfd5 commit d2b416f
Showing 1 changed file with 49 additions and 18 deletions.
67 changes: 49 additions & 18 deletions packages/testing/src/simple-graphql-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export class SimpleGraphQLClient {
'Apollo-Require-Preflight': 'true',
};

constructor(private vendureConfig: Required<VendureConfig>, private apiUrl: string = '') {}
constructor(
private vendureConfig: Required<VendureConfig>,
private apiUrl: string = '',
) {}

/**
* @description
Expand Down Expand Up @@ -136,15 +139,13 @@ export class SimpleGraphQLClient {
async asUserWithCredentials(username: string, password: string) {
// first log out as the current user
if (this.authToken) {
await this.query(
gql`
mutation {
logout {
success
}
await this.query(gql`
mutation {
logout {
success
}
`,
);
}
`);
}
const result = await this.query(LOGIN, { username, password });
if (result.login.channels?.length === 1) {
Expand All @@ -170,15 +171,13 @@ export class SimpleGraphQLClient {
* Logs out so that the client is then treated as an anonymous user.
*/
async asAnonymousUser() {
await this.query(
gql`
mutation {
logout {
success
}
await this.query(gql`
mutation {
logout {
success
}
`,
);
}
`);
}

private async makeGraphQlRequest(
Expand Down Expand Up @@ -214,7 +213,36 @@ export class SimpleGraphQLClient {
* Perform a file upload mutation.
*
* Upload spec: https://github.com/jaydenseric/graphql-multipart-request-spec
*
* Discussion of issue: https://github.com/jaydenseric/apollo-upload-client/issues/32
*
* @param mutation - GraphQL document for a mutation that has input files
* with the Upload type.
* @param filePaths - Array of paths to files, in the same order that the
* corresponding Upload fields appear in the variables for the mutation.
* @param mapVariables - Function that must return the variables for the
* mutation, with `null` as the value for each `Upload` field.
*
* @example
* // Testing a custom mutation:
* const result = await client.fileUploadMutation({
* mutation: gql`
* mutation AddSellerImages($input: AddSellerImagesInput!) {
* addSellerImages(input: $input) {
* id
* name
* }
* }
* `,
* filePaths: ['./images/profile-picture.jpg', './images/logo.png'],
* mapVariables: () => ({
* name: "George's Pans",
* profilePicture: null, // corresponds to filePaths[0]
* branding: {
* logo: null // corresponds to filePaths[1]
* }
* })
* });
*/
async fileUploadMutation(options: {
mutation: DocumentNode;
Expand Down Expand Up @@ -256,7 +284,10 @@ export class SimpleGraphQLClient {
}

export class ClientError extends Error {
constructor(public response: any, public request: any) {
constructor(
public response: any,
public request: any,
) {
super(ClientError.extractMessage(response));
}
private static extractMessage(response: any): string {
Expand Down

0 comments on commit d2b416f

Please sign in to comment.