Skip to content

Commit

Permalink
Tweak docs for endpoints (#785)
Browse files Browse the repository at this point in the history
* ✏️ DOC: tweaks

* ✏️ FIX: \t to ' '
  • Loading branch information
jycouet authored Dec 21, 2022
1 parent 20e257a commit 44f8acc
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 14 deletions.
1 change: 1 addition & 0 deletions e2e/sveltekit/src/lib/utils/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const routes = {
Stores_SSR_One_Store_Multivariables: '/stores/ssr-one-store-multivariables',
Stores_Fragment_Null: '/stores/fragment-null',
Stores_Metadata: '/stores/metadata',
Stores_action_mutation: '/stores/action-mutation',
Stores_Endpoint_Query: '/stores/endpoint-query',
Stores_Endpoint_Mutation: '/stores/endpoint-mutation',
Stores_Session: '/stores/session',
Expand Down
26 changes: 26 additions & 0 deletions e2e/sveltekit/src/routes/stores/action-mutation/+page.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { graphql } from '$houdini';
import { fail } from '@sveltejs/kit';

/** @type {import('./$types').Actions} */
export const actions = {
add: async (event) => {
const data = await event.request.formData();

const name = data.get('name')?.toString();

if (!name) {
return fail(403, { name: '*' });
}

const actionMutation = graphql(`
mutation ActionMutation($name: String!) {
addUser(name: $name, birthDate: 254143016000, snapshot: "ActionMutation") {
id
name
}
}
`);

return await actionMutation.mutate({ name });
}
};
21 changes: 21 additions & 0 deletions e2e/sveltekit/src/routes/stores/action-mutation/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script>
import { enhance } from '$app/forms';
/** @type {import('./$types').ActionData} */
export let form;
</script>

<form method="POST" action="?/add" use:enhance>
<label>
Name
<input name="name" type="name" />
</label>
<span id="name-error"
>{#if form?.name}{form?.name}{/if}</span
>
<button type="submit">Add</button>
</form>

<div id="result">
{form?.addUser?.name || 'No user added'}
</div>
36 changes: 36 additions & 0 deletions e2e/sveltekit/src/routes/stores/action-mutation/spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { sleep } from '@kitql/helper';
import { test } from '@playwright/test';
import { routes } from '../../../lib/utils/routes.js';
import { expectToBe, goto } from '../../../lib/utils/testsHelper.js';

test.describe('action-mutation', () => {
test('happy path action-mutation ', async ({ page }) => {
await goto(page, routes.Stores_action_mutation);

await expectToBe(page, 'No user added');

// click the button
await Promise.all([
page.waitForResponse((res) => res.url().endsWith('action-mutation?/add'), { timeout: 1000 }),
page.getByRole('button', { name: 'Add' }).click()
]);

// a start should be displayed
await expectToBe(page, '*', 'span[id=name-error]');

// fill the input
await page.getByLabel('Name').fill('My New Name');

// add
await Promise.all([
page.waitForResponse((res) => res.url().endsWith('action-mutation?/add'), { timeout: 1000 }),
page.getByRole('button', { name: 'Add' }).click()
]);

// wait for the message to appear
await sleep(500);

// check that we have the right data
await expectToBe(page, 'My New Name');
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { GQL_Hello } from '$houdini';
import type { RequestEvent } from '@sveltejs/kit';

export async function load(event: RequestEvent) {
/** @type {import('./$types').PageServerLoad} */
export const load = async (event) => {
const { data } = await GQL_Hello.fetch({ event });

return { hello: data?.hello };
}
};
6 changes: 3 additions & 3 deletions site/src/routes/api/graphql/+page.svx
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ Houdini generates runtime representations of every enum in your schema. They can
`$houdini`:

```javascript
import { MyEnum, type UpdateStore } from '$houdini'
import { MyEnum } from '$houdini'

const update: UpdateStore = graphql`
const update = graphql(`
mutation Update($input: MyEnum!) {
update(input: $input)
}
`
`)

function onClick() {
update.mutate({ variables: { input: MyEnum.Value1 } })
Expand Down
34 changes: 34 additions & 0 deletions site/src/routes/api/mutation/+page.svx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,40 @@ can be used to configure its behavior. The following values can be provided:
- mutations usually do best when combined with at least one fragment grabbing
the information needed for the mutation (for an example of this pattern, see below.)

## Form actions

Using a mutation inside an action endpoint looks very similar to anywhere else.
For reference, it would look like:

```js
import { graphql } from '$houdini';
import { fail } from '@sveltejs/kit';

/** @type {import('./$types').Actions} */
export const actions = {
add: async (event) => {
const data = await event.request.formData();

const name = data.get('name')?.toString();

if (!name) {
return fail(403, { name: '*' });
}

const actionMutation = graphql(`
mutation ActionMutation($name: String!) {
addUser(name: $name, birthDate: 254143016000, snapshot: "ActionMutation") {
id
name
}
}
`);

return await actionMutation.mutate({ name });
}
};
```

## Updating fields

When a mutation is responsible for updating fields of entities, houdini
Expand Down
9 changes: 3 additions & 6 deletions site/src/routes/api/query/+page.svx
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,12 @@ are handed in your route function:
```javascript
import { MyQueryStore } from '$houdini'

export async function get(event) {
/** @type {import('./$types').PageServerLoad} */
export const load = (event) => {
const myQuery = new MyQueryStore()
const { data } = await myQuery.fetch({ event })

return {
body: {
data
}
}
return { data }
}
```

Expand Down
2 changes: 1 addition & 1 deletion site/src/routes/guides/contributing/+page.svx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ To understand how this works, consider this query:
<script>
import { query, graphql } from '$houdini'

const SpeciesInfo = graphql`
const SpeciesInfo = graphql(`
query SpeciesInfo {
species(id: 1) {
name
Expand Down
2 changes: 1 addition & 1 deletion site/src/routes/guides/typescript/+page.svx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ If you are working with inline stores, you don't have to provide types when usin
```typescript
import { graphql } from '$houdini'

const store: AllItemsStore = graphql(`
const store = graphql(`
query AllItems {
items {
id
Expand Down

2 comments on commit 44f8acc

@vercel
Copy link

@vercel vercel bot commented on 44f8acc Dec 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 44f8acc Dec 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs-next – ./site

docs-next-kohl.vercel.app
docs-next-houdinigraphql.vercel.app
docs-next-git-main-houdinigraphql.vercel.app

Please sign in to comment.