【发布时间】:2019-06-30 18:40:41
【问题描述】:
我的前端是localhost:3000,我的GraphQL 服务器是localhost:3333。
我已经使用 react-apollo 在 JSX 领域进行查询/变异,但还没有从 Express 进行查询/变异。
我想在我的server.js 中进行查询/突变。
server.get('/auth/github/callback', (req, res) => {
// send GraphQL mutation to add new user
});
下面似乎是正确的方向,但我得到了TypeError: ApolloClient is not a constructor:
const express = require('express');
const next = require('next');
const ApolloClient = require('apollo-boost');
const gql = require('graphql-tag');
// setup
const client = new ApolloClient({
uri: 'http://localhost:3333/graphql'
});
const app = next({dev});
const handle = app.getRequestHandler();
app
.prepare()
.then(() => {
const server = express();
server.get('/auth/github/callback', (req, res) => {
// GraphQL mutation
client.query({
query: gql`
mutation ADD_GITHUB_USER {
signInUpGithub(
email: "email@address.com"
githubAccount: "githubusername"
githubToken: "89qwrui234nf0"
) {
id
email
githubToken
githubAccount
}
}
`,
})
.then(data => console.log(data))
.catch(error => console.error(error));
});
server.listen(3333, err => {
if (err) throw err;
console.log(`Ready on http://localhost:3333`);
});
})
.catch(ex => {
console.error(ex.stack);
process.exit(1);
});
This post mentions Apollo as the solution,但没有举例。
如何将 GraphQL 突变从 Express 服务器 :3000 调用到 GraphQL :3333?
【问题讨论】:
-
您好 Chance,您能详细解释一下您的问题吗?我不明白...您说您使用过
react-apollo(React 方面...)但是您不知道如何从 React 查询?我不明白。 -
嘿,@JVLobo - 我更新了我的问题。
-
酷,现在更清楚了:) 我已经发布了答案,希望对您有所帮助
-
我不会使用功能齐全的客户端来执行服务器端请求。你可以使用像graphql-request 这样非常简单的东西来代替。
标签: express graphql apollo apollo-client