Skip to content

Commit

Permalink
Merge pull request #3 from cofacts/nextjs-fetch
Browse files Browse the repository at this point in the history
Support Nextjs fetch options in graphql-request
  • Loading branch information
MrOrz authored Feb 14, 2024
2 parents 7fb1dad + 51e29fd commit 5af8fa5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
21 changes: 21 additions & 0 deletions app/lib/gql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { GraphQLClient } from 'graphql-request';
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
export { graphql } from '@/typegen/gql';

/**
* @param operation
* @param variables - the variables for the operation
* @param fetchOptions - the options for NextJS's fetch
* @returns graphql-request's request result
* @see https://github.com/dotansimha/graphql-typed-document-node?tab=readme-ov-file#how-can-i-support-this-in-my-library
*/
export function gql<TData = any, TVariables = Record<string, any>>(
operation: TypedDocumentNode<TData, TVariables>,
variables?: TVariables,
fetchOptions?: Parameters<typeof fetch>[1]
) {
const client = new GraphQLClient(process.env.COFACTS_API_URL ?? '', {
fetch: (input, init) => fetch(input, { ...init, ...fetchOptions }),
});
return client.request(operation, { ...variables });
}
20 changes: 11 additions & 9 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { request } from 'graphql-request';
import { graphql } from '@/typegen/gql';
import { gql, graphql } from '@/app/lib/gql';

async function getData() {
return request(
process.env.COFACTS_API_URL ?? '',
return gql(
graphql(/* GraphQL */ `
query LoadAPIStats {
allArticles: ListArticles {
query LoadAPIStats($bar: Boolean!) {
allArticles: ListArticles @skip(if: $bar) {
totalCount
}
allRepliedArticles: ListArticles {
allRepliedArticles: ListArticles(filter: { replyCount: { GTE: 1 } }) {
totalCount
}
articlesHasUsefulReplies: ListArticles {
articlesHasUsefulReplies: ListArticles(
filter: { hasArticleReplyWithMorePositiveFeedback: true }
) {
totalCount
}
}
`)
`),
{ bar: false },
{ next: { revalidate: 10 /* Only cache for 10 seconds */ } }
);
}

Expand Down
4 changes: 2 additions & 2 deletions typegen/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/
* Therefore it is highly recommended to use the babel or swc plugin for production.
*/
const documents = {
"\n query LoadAPIStats {\n allArticles: ListArticles {\n totalCount\n }\n allRepliedArticles: ListArticles {\n totalCount\n }\n articlesHasUsefulReplies: ListArticles {\n totalCount\n }\n }\n ": types.LoadApiStatsDocument,
"\n query LoadAPIStats($bar: Boolean!) {\n allArticles: ListArticles @skip(if: $bar) {\n totalCount\n }\n allRepliedArticles: ListArticles(filter: { replyCount: { GTE: 1 } }) {\n totalCount\n }\n articlesHasUsefulReplies: ListArticles(\n filter: { hasArticleReplyWithMorePositiveFeedback: true }\n ) {\n totalCount\n }\n }\n ": types.LoadApiStatsDocument,
};

/**
Expand All @@ -33,7 +33,7 @@ export function graphql(source: string): unknown;
/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function graphql(source: "\n query LoadAPIStats {\n allArticles: ListArticles {\n totalCount\n }\n allRepliedArticles: ListArticles {\n totalCount\n }\n articlesHasUsefulReplies: ListArticles {\n totalCount\n }\n }\n "): (typeof documents)["\n query LoadAPIStats {\n allArticles: ListArticles {\n totalCount\n }\n allRepliedArticles: ListArticles {\n totalCount\n }\n articlesHasUsefulReplies: ListArticles {\n totalCount\n }\n }\n "];
export function graphql(source: "\n query LoadAPIStats($bar: Boolean!) {\n allArticles: ListArticles @skip(if: $bar) {\n totalCount\n }\n allRepliedArticles: ListArticles(filter: { replyCount: { GTE: 1 } }) {\n totalCount\n }\n articlesHasUsefulReplies: ListArticles(\n filter: { hasArticleReplyWithMorePositiveFeedback: true }\n ) {\n totalCount\n }\n }\n "): (typeof documents)["\n query LoadAPIStats($bar: Boolean!) {\n allArticles: ListArticles @skip(if: $bar) {\n totalCount\n }\n allRepliedArticles: ListArticles(filter: { replyCount: { GTE: 1 } }) {\n totalCount\n }\n articlesHasUsefulReplies: ListArticles(\n filter: { hasArticleReplyWithMorePositiveFeedback: true }\n ) {\n totalCount\n }\n }\n "];

export function graphql(source: string) {
return (documents as any)[source] ?? {};
Expand Down
6 changes: 4 additions & 2 deletions typegen/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1390,10 +1390,12 @@ export type YdocVersion = {
snapshot: Maybe<Scalars['String']['output']>;
};

export type LoadApiStatsQueryVariables = Exact<{ [key: string]: never; }>;
export type LoadApiStatsQueryVariables = Exact<{
bar: Scalars['Boolean']['input'];
}>;


export type LoadApiStatsQuery = { allArticles: { totalCount: number } | null, allRepliedArticles: { totalCount: number } | null, articlesHasUsefulReplies: { totalCount: number } | null };


export const LoadApiStatsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"LoadAPIStats"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"allArticles"},"name":{"kind":"Name","value":"ListArticles"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"totalCount"}}]}},{"kind":"Field","alias":{"kind":"Name","value":"allRepliedArticles"},"name":{"kind":"Name","value":"ListArticles"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"totalCount"}}]}},{"kind":"Field","alias":{"kind":"Name","value":"articlesHasUsefulReplies"},"name":{"kind":"Name","value":"ListArticles"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"totalCount"}}]}}]}}]} as unknown as DocumentNode<LoadApiStatsQuery, LoadApiStatsQueryVariables>;
export const LoadApiStatsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"LoadAPIStats"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"bar"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Boolean"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"allArticles"},"name":{"kind":"Name","value":"ListArticles"},"directives":[{"kind":"Directive","name":{"kind":"Name","value":"skip"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"if"},"value":{"kind":"Variable","name":{"kind":"Name","value":"bar"}}}]}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"totalCount"}}]}},{"kind":"Field","alias":{"kind":"Name","value":"allRepliedArticles"},"name":{"kind":"Name","value":"ListArticles"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filter"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"replyCount"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"GTE"},"value":{"kind":"IntValue","value":"1"}}]}}]}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"totalCount"}}]}},{"kind":"Field","alias":{"kind":"Name","value":"articlesHasUsefulReplies"},"name":{"kind":"Name","value":"ListArticles"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filter"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"hasArticleReplyWithMorePositiveFeedback"},"value":{"kind":"BooleanValue","value":true}}]}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"totalCount"}}]}}]}}]} as unknown as DocumentNode<LoadApiStatsQuery, LoadApiStatsQueryVariables>;

0 comments on commit 5af8fa5

Please sign in to comment.