Skip to content

Commit fb73b4b

Browse files
committed
refactor(website): use variables in graphql mutation
1 parent 015d429 commit fb73b4b

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

astro.config.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import node from '@astrojs/node';
33
import svelte from '@astrojs/svelte';
44
import tailwindcss from '@tailwindcss/vite';
55
import { defineConfig } from 'astro/config';
6+
import { JsxEmit } from 'typescript';
67

78
const PREVIEW = process.env.PREVIEW_DEPLOYMENT === 'true';
89

@@ -38,7 +39,8 @@ export default defineConfig({
3839

3940
vite: {
4041
define: {
41-
'process.env': process.env,
42+
'process.env.TWENTY_AUTH': `${process.env.TWENTY_AUTH}`,
43+
'process.env.TWENTY_GRAPHQL_URL': `${process.env.TWENTY_GRAPHQL_URL}`,
4244
},
4345
plugins: [tailwindcss()],
4446
},

src/pages/api/support-us.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const prerender = false;
22
import type { APIRoute } from 'astro';
33

44
import { gql, GraphQLClient } from 'graphql-request';
5+
56
export const POST: APIRoute = async ({ request }) => {
67
const requiredFields = [
78
'firstName',
@@ -69,31 +70,33 @@ export const POST: APIRoute = async ({ request }) => {
6970

7071
// With the replace method the the quotes from the property keys (from JSON) are removed, because graphql requires them unquoted
7172
const mutation = gql`
72-
mutation {
73-
createPerson(data: ${JSON.stringify(personData).replace(/"([^"]+)":/g, '$1:')}) {
73+
mutation SupportFromMutation(
74+
$personData: PersonCreateInput
75+
$fordermitgliedschaftData: FordermitgliedschaftCreateInput
76+
) {
77+
createPerson(data: $personData) {
7478
id
7579
createdAt
7680
}
77-
createFordermitgliedschaft(
78-
data: ${JSON.stringify(fordermitgliedschaftData).replace(/"([^"]+)":/g, '$1:')}
79-
) {
81+
createFordermitgliedschaft(data: $fordermitgliedschaftData) {
8082
id
81-
monatlicherForderbeitrag {
82-
amountMicros
83-
currencyCode
84-
}
83+
createdAt
8584
}
8685
}
8786
`;
8887
console.log(mutation);
88+
const variables = {
89+
fordermitgliedschaftData,
90+
personData,
91+
};
8992
//TODO: link the Fordermitgliedschaft with the person
9093
interface ResponseData {
9194
createPerson: {
9295
createdAt: string;
9396
id: string;
9497
};
9598
}
96-
const response = await graphQLClient.request<ResponseData>(mutation);
99+
const response = await graphQLClient.request<ResponseData>(mutation, variables);
97100
console.log(response);
98101
} catch (error) {
99102
console.error(error);

0 commit comments

Comments
 (0)