Skip to content

Commit c50cf53

Browse files
committed
fixup examples
1 parent b1dc9e1 commit c50cf53

File tree

2 files changed

+40
-22
lines changed
  • examples/query/react

2 files changed

+40
-22
lines changed

examples/query/react/authentication-with-extrareducers/src/features/auth/authSlice.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const slice = createSlice({
1515
builder.addMatcher(
1616
api.endpoints.login.matchFulfilled,
1717
(state, { payload }) => {
18-
state.token = payload.result.token
19-
state.user = payload.result.user
18+
state.token = payload.token
19+
state.user = payload.user
2020
}
2121
)
2222
},
Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { DocumentNode } from 'graphql'
2+
import { ClientError } from 'graphql-request'
13
import { BaseQueryFn, createApi } from '@reduxjs/toolkit/query/react'
2-
import { request, gql } from "graphql-request";
4+
import { request, gql } from 'graphql-request'
35

46
export const postStatuses = ['draft', 'published', 'pending_review'] as const
57

@@ -22,7 +24,7 @@ export interface Pagination {
2224

2325
export interface GetPostsResponse extends Pagination {
2426
data: {
25-
posts: Post[];
27+
posts: Post[]
2628
}
2729
}
2830

@@ -32,48 +34,64 @@ interface PostResponse {
3234
}
3335
}
3436

35-
const graphqlBaseQuery = ({ baseUrl }: { baseUrl: string; } = { baseUrl: '' }): BaseQueryFn<any, unknown, any> => async ({ body, variables }) => {
36-
const result = await request(baseUrl, body, variables);
37-
return { data: result };
38-
};
37+
export const graphqlBaseQuery = ({
38+
baseUrl,
39+
}: {
40+
baseUrl: string
41+
}): BaseQueryFn<
42+
{ document: string | DocumentNode; variables?: any },
43+
unknown,
44+
ClientError
45+
> => async ({ document, variables }) => {
46+
try {
47+
return { data: await request(baseUrl, document, variables) }
48+
} catch (error) {
49+
if (error instanceof ClientError) {
50+
return { error }
51+
}
52+
throw error
53+
}
54+
}
3955

4056
export const api = createApi({
4157
baseQuery: graphqlBaseQuery({
42-
baseUrl: "/graphql"
58+
baseUrl: '/graphql',
4359
}),
4460
endpoints: (builder) => ({
45-
getPosts: builder.query<GetPostsResponse, { page?: number; per_page?: number; }>({
61+
getPosts: builder.query<
62+
GetPostsResponse,
63+
{ page?: number; per_page?: number }
64+
>({
4665
query: ({ page, per_page }) => ({
47-
body: gql`
66+
document: gql`
4867
query GetPosts($page: Int = 1, $per_page: Int = 10) {
4968
posts(page: $page, per_page: $per_page) {
5069
id
5170
title
52-
},
71+
}
5372
}
5473
`,
5574
variables: {
5675
page,
57-
per_page
58-
}
76+
per_page,
77+
},
5978
}),
6079
}),
6180
getPost: builder.query<Post, string>({
6281
query: (id) => ({
63-
body: gql`
82+
document: gql`
6483
query GetPost($id: ID!) {
6584
post(id: ${id}) {
6685
id
6786
title
6887
body
6988
}
7089
}
71-
`
90+
`,
7291
}),
73-
transformResponse: (response: PostResponse) => response.data.post
74-
})
75-
})
76-
});
77-
92+
transformResponse: (response: PostResponse) => response.data.post,
93+
}),
94+
}),
95+
})
7896

79-
export const { useGetPostsQuery, useGetPostQuery } = api
97+
export const { useGetPostsQuery, useGetPostQuery } = api

0 commit comments

Comments
 (0)