From efed6c0a820c5c47e097fa29455157bbd318ffca Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 17 Dec 2021 23:28:17 +0100 Subject: chore: create mutation to send mail from contact form --- src/services/graphql/contact.ts | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/services/graphql/contact.ts (limited to 'src/services/graphql') diff --git a/src/services/graphql/contact.ts b/src/services/graphql/contact.ts new file mode 100644 index 0000000..4699688 --- /dev/null +++ b/src/services/graphql/contact.ts @@ -0,0 +1,49 @@ +import { SendMailReturn, SentEmailResponse } from '@ts/types/contact'; +import { gql } from 'graphql-request'; +import { getGraphQLClient } from './client'; + +export const sendMail: SendMailReturn = async ( + subject: string, + body: string, + replyTo: string, + mutationId: string +) => { + const client = getGraphQLClient(); + const mutation = gql` + mutation SendEmail( + $subject: String! + $body: String! + $replyTo: String! + $mutationId: String! + ) { + sendEmail( + input: { + clientMutationId: $mutationId + body: $body + replyTo: $replyTo + subject: $subject + } + ) { + clientMutationId + message + sent + origin + replyTo + to + } + } + `; + + const variables = { subject, body, replyTo, mutationId }; + + try { + const response: SentEmailResponse = await client.request( + mutation, + variables + ); + return response.sendEmail; + } catch (error) { + console.error(error, undefined, 2); + process.exit(1); + } +}; -- cgit v1.2.3