在测试中启动Postgraphile+Apollo

Postgraphile在node.js上运行。
据说可以从Postgres轻松创建GraphQL。
我会在这里或者其他地方试试。
环境是WindowsServer2012。

安装Postgraphile

由于Postgres已在本地运行,因此可以省略。

使用npm安装postgraphile模块。

由于这次很麻烦,所以我选择用-g安装。(应该可以用npx在本地运行?)
安装了4.12.8版本。

启动命令如下:
postgraphile -c postgres://postgres:password@localhost:5432/postgres –watch –enhance-graphiql –dynamic-json

我能用這個來啟動了。

  ‣ GraphQL API:         http://localhost:5000/graphql
  ‣ GraphiQL GUI/IDE:    http://localhost:5000/graphiql
  ‣ Postgres connection: postgres://postgres:[SECRET]@localhost/postgres (watching)

访问此URL以进行确认,但发现其为公共模式(public schema)。可以通过指定–schema test来选择模式。
可以在以前创建的test表中执行查询操作。

query MyQuery {
  allTests(first: 10) {
    edges {
      node {
        id
      }
    }
  }
}
{
  "data": {
    "allTests": {
      "edges": [
        {
          "node": {
            "id": 1
          }
        },
        {
          "node": {
            "id": 2
          }
        },
        {
          "node": {
            "id": 3
          }
        }
      ]
    }
  }
}

很容易做到。 .)

从HTTP服务器启动

也可以通过node.js服务器使用的方式。官方文档中也有说明。

const express = require("express");
const { postgraphile } = require("postgraphile");

const app = express();

app.use(
  postgraphile(
    process.env.DATABASE_URL || "postgres://user:pass@host:5432/dbname",
    "public",
    {
      watchPg: true,
      graphiql: true,
      enhanceGraphiql: true,
    }
  )
);

app.listen(process.env.PORT || 5000);

只要这样运行,就会出现一个名为“graphql”的错误,所以可能是安装的某个东西有问题。

"resolutions": {
 "graphql": "15.5.0"
}

在网络上有人写道,以此方式书写并解决,但由于似乎有很多麻烦,所以我在这里放弃了…
在”@apollo”中写着:”graphql”: “^14.0.0 || ^15.0.0 || ^16.0.0″。

此外,在生产环境中,建议的选项是什么?

const postgraphileOptions = {
  subscriptions: true,
  retryOnInitFail: true,
  dynamicJson: true,
  setofFunctionsContainNulls: false,
  ignoreRBAC: false,
  extendedErrors: ["errcode"],
  appendPlugins: [require("@graphile-contrib/pg-simplify-inflector")],
  graphiql: false,
  enableQueryBatching: true,
  disableQueryLog: true, // our default logging has performance issues, but do make sure you have a logging system in place!
  legacyRelations: "omit",
  pgSettings(req) {
    /* TODO */
  },
};

我写了这个,但是没有仔细看。

其他服务器

关于安全性问题,在官方文档中已经写了一些。
关于 DoS,可以参考https://www.apollographql.com/blog/graphql/security/securing-your-graphql-api-from-malicious-queries/。
一般来说,安装https://github.com/graphile/persisted-operations插件是个不错的选择。

中国有一个名为bodySizeLimit的选项。
如果想使用类似graphqlDepthLimit或graphqlCostLimit的限制,似乎存在Pro版本。

当无法正常运行时,可以参考这里吗?

如果觉得GraphQL比Rest API更好的话,可以阅读有关如何构建PostgREST的文章。

阿波罗客户端

作为连接到GraphQL的一方,这次我们顺便试试Apollo的客户端部分。可以参考这里和这里。

阿波罗客户端

使用 node.js 单独返回而不是使用 React 进行测试。
虽然信息不太充分,但这可能会有一些参考价值。

首先,安装所需的软件包:
npm install @apollo/client graphql apollo-boost node-fetch react

服务器使用postgraphile命令启动。
我尝试使用express框架在node.js端设置,使得当访问/api时从GraphQL获取数据。
由于一直在不断尝试中,所以可能存在一些不必要的处理。

import apolloClient, { gql } from "apollo-boost";
import express from "express";
import apolloInMemoryCache from 'apollo-cache-inmemory';
import apolloHttpLink from 'apollo-link-http';
import nodeFetch from 'node-fetch';
global.fetch = nodeFetch;
const { HttpLink } = apolloHttpLink;
const { InMemoryCache } = apolloInMemoryCache;
const { ApolloClient } = apolloClient;
const app = express();
var server = app.listen(3000, function(){
    console.log("Node.js is listening to PORT:" + server.address().port);
});

const client = new ApolloClient({
  uri: 'http://localhost:5000/graphql', // このパターンだとこれは必要ないはず
  cache : new InMemoryCache(),
  link:  new HttpLink( {uri:'http://localhost:5000/graphql'})
});

async function getTestProps() {
    const {data} = await client.query({
      query: gql`
      query MyQuery { allTests(first: 10) { edges { node { id name } } } }
      `,
    });
    return data
}

// 外部から取得する際の処理
app.get("/api", async function(req, res, next){
    let data =  await getTestProps();
    res.json(data);
});

数据已经回来了。

{"allTests":{"edges":[{"node":{"id":1,"name":"one","__typename":"Test"},"__typename":"TestsEdge"}
,{"node":{"id":2,"name":"two","__typename":"Test"},"__typename":"TestsEdge"}
,{"node":{"id":3,"name":"therr","__typename":"Test"},"__typename":"TestsEdge"}],"__typename":"TestsConnection"}}
bannerAds