如何从Express服务器后端调用GraphQL查询器?

问题描述 投票:10回答:3

我的前端是 localhost:3000我的GraphQL服务器是 localhost:3333.

我在JSX的土地上使用react-apollo来进行查询,但还没有从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 protected]"
              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);
  });

本员额提到Apollo是解决办法。但没有给出一个例子。

如何从Express服务器调用GraphQL突变?:3000 到GraphQL :3333?

express graphql apollo apollo-client
3个回答
2
投票

这更有可能是你要找的。

const { createApolloFetch } = require('apollo-fetch');

const fetch = createApolloFetch({
    uri: 'https://1jzxrj179.lp.gql.zone/graphql',
});

fetch({
    query: '{ posts { title } }',
}).then(res => {
    console.log(res.data);
});

// You can also easily pass variables for dynamic arguments
fetch({
    query: `
        query PostsForAuthor($id: Int!) {
            author(id: $id) {
                firstName
                posts {
                    title
                    votes
                }
            }
        }
    `,
    variables: { id: 1 },
}).then(res => {
    console.log(res.data);
});

从这个帖子中摘录的,可能对其他人也有帮助。https:/blog.apollographql.com4 -简单的调用方式 -a -graphql -api -a6807bcdb355


0
投票

由于你越来越 ApolloClientrequire 而不是 import 我想你漏掉了这部分。

// es5 or Node.js
const Boost = require('apollo-boost');
const ApolloClient = Boost.DefaultClient;

或者

const ApolloBoost = require('apollo-boost');
const ApolloClient = ApolloBoost.default;

试试其中的一个,看看是否有效。


0
投票

你可以用 graphql-request它是一个简单的GraphQL客户端。

const { request } = require('graphql-request');

request('http://localhost:3333/graphql', `mutation ADD_USER($email: String!, $password: String!) {
  createUser(email: $email, password: $password) {
    id
    email
  }
}`, {email: '[email protected]', password: 'Pa$$w0rd'})
.then(data => console.info(data))
.catch(error => console.error(error));

它还支持CORS。

const { GraphQLClient } = require('graphql-request');

const endpoint = 'http://localhost:3333/graphql';
const client = new GraphQLClient(endpoint, {
  credentials: 'include',
  mode: 'cors'
});

client.request(`mutation ADD_USER($email: String!, $password: String!) {
  createUser(email: $email, password: $password) {
    id
    email
  }
}`, {email: '[email protected]', password: 'Pa$$w0rd'})
.then(data => console.info(data))
.catch(error => console.error(error));

我用它来做E2E测试。

© www.soinside.com 2019 - 2024. All rights reserved.