Skip to content

Commit

Permalink
a possible workarround for dotansimha/graphql-code-generator#8103
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiion committed Jul 15, 2022
1 parent f612508 commit d08f51e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
20 changes: 16 additions & 4 deletions graphql/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { useGraphQLContext } from '../context/graphql';
import { parse, print } from 'graphql'

export const useFetchData = <TData, TVariables>(
query: string,
): (() => Promise<TData>) => {
// FAILING: using a hook inside the fetcher
//const { endpoints } = useGraphQLContext();

const parsedQuery = parse(query)
const alreadyVisitedQueryNames: string[] = []
const fixedQuery = print({
...parsedQuery,
definitions: parsedQuery.definitions.filter(def => {
if(!(def.kind === 'FragmentDefinition' && alreadyVisitedQueryNames.includes(def.name.value))) {
if(def.kind === 'FragmentDefinition') alreadyVisitedQueryNames.push(def.name.value)
return true
}
})
})


// PASSING: mocking the data so we don't use a hook inside the fetcher
const endpoints = {
Expand All @@ -16,7 +28,7 @@ export const useFetchData = <TData, TVariables>(
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
query: fixedQuery,
variables,
}),
});
Expand Down
2 changes: 1 addition & 1 deletion graphql/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,4 @@ export const useCompanyQuery = <
variables === undefined ? ['company'] : ['company', variables],
useFetchData<CompanyQuery, CompanyQueryVariables>(CompanyDocument).bind(null, variables),
options
);
);
2 changes: 1 addition & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Head from 'next/head';
import Image from 'next/image';
import React, { useState } from 'react';
import {useCompanyQuery, useInfiniteCompanyQuery, useInfiniteUsersQuery} from '../graphql/generated';
import {useCompanyQuery} from '../graphql/generated';
import styles from '../styles/Home.module.css';

export default function Home() {
Expand Down

0 comments on commit d08f51e

Please sign in to comment.