GraphQL Changes

This commit is contained in:
SushantJadhav 2021-08-25 11:58:21 +05:30
parent 37bce41ec6
commit 05021b5529
2 changed files with 5 additions and 3 deletions

View File

@ -1,6 +1,6 @@
import type { CommerceAPI, CommerceAPIConfig } from '@commerce/api'
import { getCommerceApi as commerceApi } from '@commerce/api'
import createFetcher from './utils/fetch-local'
import fetchGraphqlApi from './utils/fetch-local'
import getAllPages from './operations/get-all-pages'
import getPage from './operations/get-page'
@ -17,7 +17,7 @@ const config: KiboCommerceConfig = {
cartCookie: process.env.KIBO_CART_COOKIE || 'kibo_car',
customerCookie: process.env.KIBO_CUSTOMER_COOKIE || 'kibo_customer',
cartCookieMaxAge: 2592000,
fetch: createFetcher(() => getCommerceApi().getConfig()),
fetch: fetchGraphqlApi(() => getCommerceApi().getConfig()),
}
const operations = {

View File

@ -8,9 +8,11 @@ const fetchGraphqlApi: (getConfig: () => KiboCommerceConfig) => GraphQLFetcher =
async (query: string, { variables, preview } = {}, fetchOptions) => {
const config = getConfig()
const res = await fetch(config.commerceUrl, {
//const res = await fetch(config.commerceUrl + (preview ? '/preview' : ''), {
...fetchOptions,
method: 'POST',
headers: {
Authorization: `Bearer ${config.apiToken}`,
...fetchOptions?.headers,
'Content-Type': 'application/json',
},
@ -23,7 +25,7 @@ const fetchGraphqlApi: (getConfig: () => KiboCommerceConfig) => GraphQLFetcher =
const json = await res.json()
if (json.errors) {
throw new FetcherError({
errors: json.errors ?? [{ message: 'Failed to fetch for API' }],
errors: json.errors ?? [{ message: 'Failed to fetch KiboCommerce API' }],
status: res.status,
})
}