Skip to content

Commit 8527bc5

Browse files
committed
add next auth session access token to graphql function
1 parent 4441d31 commit 8527bc5

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

lib/api.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,31 @@ import React from 'react';
22
import { type TypedDocumentNode } from '@graphql-typed-document-node/core';
33
import { QueryClient } from '@tanstack/react-query';
44
import { request } from 'graphql-request';
5+
import { getSession } from 'next-auth/react';
56

67
import { gqlConfig } from '@/config/site';
78

89
// create a wrapper function for graphql-request
910
// that will be used by react-query
11+
1012
export async function GraphQL<TResult, TVariables>(
1113
document: TypedDocumentNode<TResult, TVariables>,
1214
...[variables]: TVariables extends Record<string, never> ? [] : [TVariables]
1315
) {
16+
const session = await getSession();
17+
18+
const headers = {
19+
...gqlConfig.headers,
20+
...(session ? { Authorization: session?.access_token } : {}),
21+
};
22+
1423
const data = await request(
1524
`${process.env.NEXT_PUBLIC_BACKEND_GRAPHQL_URL}`,
1625
document,
1726
{
1827
...variables,
1928
},
20-
{ ...gqlConfig.headers }
29+
headers
2130
);
2231
return data;
2332
}

0 commit comments

Comments
 (0)