graphql 연동이 안되요ㅠㅠ
425
작성한 질문수 12
섹션2의 MSW강의에서
graphql연동한이후부터 에러가 나서, 해결이 안되서 문의드리게되었습니다ㅠ(window환경입니다)
http://localhost:3000/products 호출시

browser-ponyfill.js:518 POST http://localhost:3000/ 404 (Not Found)
react_devtools_backend.js:4026 Error: GraphQL Error (Code: 404): {"response":{"error":"","status":404,"headers":{"map":{"access-control-allow-origin":"*","connection":"keep-alive","content-length":"0","date":"Tue, 15 Nov 2022 04:03:57 GMT","keep-alive":"timeout=5","content-type":"text/xml"}}},"request":{"query":"query GET_PRODUCTS {\n id\n imageUrl\n price\n title\n description\n createdAt\n}","variables":{}}}
at index.ts:497:11
at step (graphql-ws.ts:280:1)
at Object.next (graphql-ws.ts:280:1)
at fulfilled (graphql-ws.ts:280:1)콘솔로그에 위처럼 에러가 나옵니다ㅠ
vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
esbuild: {
define: {
this: "window",
},
},
server:{
host: 'localhost',
port: 3000
}
})
queryClient.ts
import { request, RequestDocument } from "graphql-request";
import { QueryClient } from "react-query";
export const getClient = (() => {
let client: QueryClient | null = null;
return () => {
if(!client) client = new QueryClient({
defaultOptions:{
queries: {
cacheTime: 1000 * 60 * 60 * 24,
staleTime: 1000 * 60,
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
}
}
});
return client;
}
})()
const BASE_URL = '/'
export const restFetcher = async ({
method,
path,
body,
params
} : {
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
path: string;
body?: { [key:string]: any }
params?: { [key:string]: any }
}) => {
try {
let url = `${BASE_URL}${path}`
const fetchOptions: RequestInit = {
method,
headers: {
'Content-Type' : 'application/json',
'Access-Control-Allow-Origin' : BASE_URL
}
}
if(params) {
const searchParams = new URLSearchParams(params)
url += '?' + searchParams.toString()
}
if(body) fetchOptions.body = JSON.stringify(body)
const res = await fetch(url, fetchOptions)
const json = await res.json()
return json
} catch (err) {
console.error(err)
}
}
export const graphqlFetcher = (query: RequestDocument, variables={}) => request(BASE_URL, query, variables)
export const QueryKeys = {
PRODUCTS: 'PRODUCTS',
}graphql/product.ts
import { gql } from 'graphql-tag'
export type Product = {
id: string
imageUrl: string
price: number
title: string
description: string
createdAt: string
}
export type Products = {
products: Product[]
}
const GET_PRODUCTS = gql`
query GET_PRODUCTS {
id
imageUrl
price
title
description
createdAt
}
`
export const GET_PRODUCT = gql`
query GET_PRODUCT($id: string) {
id
imageUrl
price
title
description
createdAt
}
`
export default GET_PRODUCTS/pages/products/index.tsx
import { useQuery } from "react-query"
import ProductItem from "../../components/product/item"
import { graphqlFetcher, QueryKeys } from "../../queryClient"
import GET_PRODUCTS, { Products } from "../../graphql/products";
const ProductList = () => {
const { data } = useQuery<Products>(QueryKeys.PRODUCTS, ()=> graphqlFetcher(GET_PRODUCTS))
console.log(data);
return (
<div>
<h2>상품목록</h2>
<ul className="products">
{data?.products?.map(product => (
<ProductItem {...product} key={product.id} />
))}
</ul>
</div>
)
}
export default ProductList
답변 1
0
mocks 폴더에 handler가 제대로 등록되어 있나요?
그밖에 의심가는 부분이 많아 즉각 답을 드리긴 어려울것 같은데,
깃헙에 코드 푸시하고 링크를 남겨주시면 저도 한 번 살펴볼게요.
0
음.. 제 컴퓨터에서는 너무 잘 실행되고 있네요;
원격으로 함께 보는편이 나을것 같아요. 아래 링크로 연락 주세요!
장바구니 담기 버튼 누르면 404에러가 뜹니다
0
256
1
[useRoutes, React-Query 오류 해결방법] No QueryClient set, use QueryClientProvider to set one 에러 나시는 분 보세요 !!!
4
1212
2
깃허브에서 선생님께서 올리신 파일 받아,, 실행시켜보려했으나 안됩니다
0
344
1
상품목록페이지만들기에서 ... 막힙니다..
0
407
1
강의 시점과 지금시점이 꽤 달라진게 있는거 같아요
0
501
1
상품목록 불러오기, 장바구니 삭제 에러 질문 드립니다
0
413
1
graphqlFetcher 관련 에러와 , data 객체 정의 되지 않는 오류 질문 드립니다
0
574
2
productdetail 데이터 안불러와지고 있습니다.
1
446
1
query 에러 발생했습니다.
1
539
1
react typescript vite 설치시 오류 질문드립니다.
0
1794
2
섹션1의 1강 routes.tsx에서 에러가 발생합니다
0
813
2
routes.tsx에서 질문이 있습니다!
1
576
1
vite-plugin-next-react-router
0
1477
3
grahpqlFetcher 설명가능할까요
1
503
1
msw mocking enabled
1
790
2
[기술 질문아님]
0
552
2
products 라우팅은 되는데 cart 라우팅은 안되네요 ㅠ
0
587
2
caught Error: No QueryClient set, use ueryClientProvider 에러
13
1971
3
Heroku build관련 오류가 발생해 글 남깁니다 ㅠㅠ
0
710
1
firebase filterling 관련 질문
0
570
3
body가 json 형태가 아닌 ReadableStream 형태로 찍힙니다.
0
785
1
graphqlFetcher 관련 에러가 해결이 안됩니다. ㅠㅠ
1
674
3
MSW graphqlFetcher 에러 관련
0
478
1
graphql-tag, graphql-request 패키지를 사용하는 이유가 궁금합니다.
0
792
1





