将 Apollo Client 或 Isomorphic Fetch 与 GraphiQL 结合使用
Use Apollo Client or Isomorphic Fetch with GraphiQL
我正在尝试将 GraphiQL IDE 集成到我的网站中,我想知道是否可以就我最好使用网站其余部分使用的 Apollo Client 还是使用 Isomorphic Fetch 获得一些建议按照文档中的建议。
如果我使用 Apollo 客户端,那么我只需要担心一个连接,而不必考虑确保身份验证正确,但代码更复杂。
我有这段代码适用于 fetcher 函数,但到目前为止它只适用于查询,据我所知,也需要大量工作才能处理突变。
graphQlFetcher = (params) => {
return this.props.client.query({
query: gql`
${params.query}
`
}).then(function(result) {
return result;
});
}
或者还有同构获取方法,但要使它起作用,我需要先获取身份验证令牌,而我的承诺不足以使它起作用。无效代码:
import { Auth } from 'aws-amplify';
getToken = async () => {
const session = await Auth.currentSession();
return session.accessToken.jwtToken;
}
graphQLFetcher = (graphQLParams) => {
return getToken().then(function(token) {
fetch(window.location.origin + '/graphql', {
method: 'post',
headers: {
'Content-Type': 'application/json',
"Authorization": token
},
body: JSON.stringify(graphQLParams),
}).then(response => response.json());
})
}
感谢有两种可能的方法来解决这个问题,它既是一个设计问题,也是一个技术问题,但希望有人能帮助我。
第一个很好,但是你的语法有些多余。这是我使用的:
params => client.query({ ...params, query: gql(params.query) });
我正在尝试将 GraphiQL IDE 集成到我的网站中,我想知道是否可以就我最好使用网站其余部分使用的 Apollo Client 还是使用 Isomorphic Fetch 获得一些建议按照文档中的建议。
如果我使用 Apollo 客户端,那么我只需要担心一个连接,而不必考虑确保身份验证正确,但代码更复杂。
我有这段代码适用于 fetcher 函数,但到目前为止它只适用于查询,据我所知,也需要大量工作才能处理突变。
graphQlFetcher = (params) => {
return this.props.client.query({
query: gql`
${params.query}
`
}).then(function(result) {
return result;
});
}
或者还有同构获取方法,但要使它起作用,我需要先获取身份验证令牌,而我的承诺不足以使它起作用。无效代码:
import { Auth } from 'aws-amplify';
getToken = async () => {
const session = await Auth.currentSession();
return session.accessToken.jwtToken;
}
graphQLFetcher = (graphQLParams) => {
return getToken().then(function(token) {
fetch(window.location.origin + '/graphql', {
method: 'post',
headers: {
'Content-Type': 'application/json',
"Authorization": token
},
body: JSON.stringify(graphQLParams),
}).then(response => response.json());
})
}
感谢有两种可能的方法来解决这个问题,它既是一个设计问题,也是一个技术问题,但希望有人能帮助我。
第一个很好,但是你的语法有些多余。这是我使用的:
params => client.query({ ...params, query: gql(params.query) });