diff options
Diffstat (limited to 'src/services/graphql')
| -rw-r--r-- | src/services/graphql/client.ts | 12 | ||||
| -rw-r--r-- | src/services/graphql/homepage.ts | 36 | 
2 files changed, 48 insertions, 0 deletions
| diff --git a/src/services/graphql/client.ts b/src/services/graphql/client.ts new file mode 100644 index 0000000..160021b --- /dev/null +++ b/src/services/graphql/client.ts @@ -0,0 +1,12 @@ +import { GraphQLClient } from 'graphql-request'; + +export const getGraphQLClient = () => { +  const apiUrl: string = process.env.NEXT_PUBLIC_GRAPHQL_API || ''; +  console.log(apiUrl); + +  if (!apiUrl) throw new Error('API URL not defined.'); + +  const graphQLClient = new GraphQLClient(apiUrl); + +  return graphQLClient; +}; diff --git a/src/services/graphql/homepage.ts b/src/services/graphql/homepage.ts new file mode 100644 index 0000000..6ea71ac --- /dev/null +++ b/src/services/graphql/homepage.ts @@ -0,0 +1,36 @@ +import { gql } from 'graphql-request'; +import { +  fetchHomePageReturn, +  getHomePageReturn, +  HomePage, +  HomePageResponse, +} from '@ts/types/homepage'; +import { getGraphQLClient } from './client'; + +export const fetchHomepage: fetchHomePageReturn = async () => { +  const client = getGraphQLClient(); +  const query = gql` +    query HomePage { +      nodeByUri(uri: "/") { +        ... on Page { +          id +          content +        } +      } +    } +  `; + +  try { +    const response: HomePageResponse = await client.request(query); +    return response; +  } catch (error) { +    console.error(JSON.stringify(error, undefined, 2)); +    process.exit(1); +  } +}; + +export const getHomePage: getHomePageReturn = async () => { +  const rawHomePage = await fetchHomepage(); +  const homePage: HomePage = rawHomePage.nodeByUri; +  return homePage; +}; | 
