【发布时间】:2021-04-16 02:41:44
【问题描述】:
我希望在这里听到专家的一些意见。 我目前正在研究 NextJS 项目,我的 graphql 正在运行在另一个 repo 中设置的模拟数据。 现在后端是由其他开发人员构建的,正在慢慢地从模拟数据转向真实数据。 他们给了我一个后端的端点,我应该在其中查询数据。 因此,目标是至少在我们完全删除模拟数据之前,让模拟的 graphql 数据和后端的真实数据并行工作。 到目前为止,我看到了 2 种方法,但我一直在寻找一种方法,我仍然可以使用像 useQuery 和 useMutation 这样的钩子
方式#1
require('isomorphic-fetch');
fetch('https://graphql.api....', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: `
query {
popularBrands ( storefront:"bax-shop.nl", limit:10, page:1){
totalCount
items{id
logo
name
image
}
}
}`
}),
})
.then(res => res.json())
.then(res => console.log(res.data));
方式#2
const client = new ApolloClient({
uri: 'https://api.spacex.land/graphql/',
cache: new InMemoryCache()
});
async function test () {
const { data: Data } = await client.query({
query: gql`
query GetLaunches {
launchesPast(limit: 10) {
id
mission_name
launch_date_local
launch_site {
site_name_long
}
links {
article_link
video_link
mission_patch
}
rocket {
rocket_name
}
}
}
`
});
console.log(Data)
}
【问题讨论】:
标签: graphql next.js react-apollo