首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从作为库运行的postgraphile中获取查询结果

如何从作为库运行的postgraphile中获取查询结果
EN

Stack Overflow用户
提问于 2022-04-29 18:15:47
回答 1查看 230关注 0票数 1

我已经将postgraphile作为快速中间件运行。例如:

代码语言:javascript
复制
const pgMiddleware = postgraphile(pool, SCHEMA, postgraphileConfig);

app.use(pgMiddleware);

如何在没有单独的客户端的情况下获取或拦截查询或突变的结果数据?

例如,当我发送以下查询时

代码语言:javascript
复制
query {
 personById(id: 1){
  firstname
  }
}

I want to be able to get the data sent back inside the same express app.
EN

回答 1

Stack Overflow用户

发布于 2022-05-06 21:01:30

我相信您所要求的是能够对来自Express中其他路由/中间件的PostGraphile模式执行PostGraphile操作,而不需要提出额外的http请求。这称为模式仅使用,您将特别希望使用withPostGraphileContext来执行请求和处理结果:

代码语言:javascript
复制
import type { Express } from "express";
import type { Pool } from "pg";
import {
  gql,
  makeProcessSchemaPlugin,
  postgraphile,
  withPostGraphileContext,
} from "postgraphile";
import PgSimplifyInflectorPlugin from "@graphile-contrib/pg-simplify-inflector";
import type { GraphQLSchema } from "graphql";
import { graphql } from "graphql";

// Register your middlewares with express
const schemaOnlyUsageApp = (app: Express, pool: Pool) => {
  let schema: GraphQLSchema;

  // This plugin will execute a callback each time the PostGraphile
  // GraphQl schema is rebuit.
  const schemaProcessorPlugin = makeProcessSchemaPlugin((newSchema) => {
    schema = newSchema;
    return schema;
  });

  // Register the PostGraphile middleware as normal for requests on /graphql (and /graphiql)
  app.use(
    postgraphile(pool, "my_schema", {
      simpleCollections: "omit",
      dynamicJson: true,
      legacyRelations: "omit",
      setofFunctionsContainNulls: false,
      appendPlugins: [PgSimplifyInflectorPlugin, schemaProcessorPlugin],
      watchPg: true,
      graphiql: true,
      enhanceGraphiql: true,
      showErrorStack: true,
      allowExplain: true,
    })
  );

  // custom route that will execute a predefined gql query directly against the schema
  app.get("/posts", async (req, res) => {
    // arbitrary gql query
    const query = gql`
      query posts {
        posts {
          edges {
            node {
              id
              title
              body
              likeCount
              createdAt
            }
          }
        }
      }
    `;

    const result = await withPostGraphileContext(
      {
        // Reuse your pool to avoid creating additional connections
        pgPool: pool,
      },
      async (context) => {
        // execute your query directly and get results without making
        // an additional http request!
        const queryResult = await graphql({
          schema,
          source: query.loc?.source || "",
          contextValue: { ...context },
        });
        return queryResult;
      }
    );

    res.send(result);
  });
};

export default schemaOnlyUsageApp;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72062178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档