【问题标题】:Apollo server 2.0 not working with join-monster-graphql-tools-adapter and oracleApollo 服务器 2.0 不能与 join-monster-graphql-tools-adapter 和 oracle 一起使用
【发布时间】:2020-07-07 04:02:03
【问题描述】:

我正在 expressjs 上构建一个 graphql 服务器。下面是代码:

const express = require('express');
const app = express();
const {ApolloServer} = require('apollo-server-express');

const server = new ApolloServer({schema});
server.applyMiddleware({app, path: '/graphql'});

app.listen(4000,()=>console.log(`server started on port $4000}`));

这是我的架构:

const typeDefs = `

    input CustomersInput {
        EMAIL_ADDRESS: String
        NAME: String
        HOME_PHONE: String
        SPA_FOLIO_ID: ID
        ALL_CUSTOMER_ID: ID
    }

    type Customer {
        ALL_CUSTOMER_ID: ID
        NAME: String
        ALL_CUSTOMER_TYPE: String
        FIRST_NAME: String
    }

    type Query {
        customers(input: CustomersInput): [Customer]!
    }

    schema {
        query: Query
    }
`;

const resolvers = {
    Query: {
        customers(parent, args, ctx, resolveInfo) {
            return joinMonster.default(resolveInfo,ctx, async sql=>{
                console.log(sql)
                return knex.raw(sql); 
            });
        },
    },
}

const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
});

joinMonsterAdapt(schema, {
    Query: {
        fields: {
            customers: {
                where: (customerTable,args) => {
                    return escape(`${customerTable}.UPPER_FIRST_NAME || ' ' || ${customerTable}.UPPER_LAST_NAME || ' ' || ${customerTable}.UPPER_FIRST_NAME like %L`, `%${args.input.NAME.toUpperCase()}%`);
                },
            },
        }
    },
    Customer: {
        sqlTable: 'ALL_CUSTOMER',
        uniqueKey: 'ALL_CUSTOMER_ID',
    },
});



module.exports = schema;

当我运行应用程序时,转到http://localhost:4000/graphql,并使用查询:

{
  customers(input:{NAME: "as"}){
    FIRST_NAME
    ALL_CUSTOMER_ID

  }
}

我回来了:

{
  "data": {
    "customers": [
      {
        "FIRST_NAME": null,
        "ALL_CUSTOMER_ID": "563",

      },
    ]
  }
}

发生这种情况是因为当我查看 joinmonster 正在生成的 sql 查询时,它只请求客户 ID,如下所示:

SELECT
  "customers"."ALL_CUSTOMER_ID" AS "ALL_CUSTOMER_ID"
FROM ALL_CUSTOMER "customers"
WHERE "customers".UPPER_FIRST_NAME || ' ' || "customers".UPPER_LAST_NAME || ' ' || "customers".UPPER_FIRST_NAME like '%AS%'

当我运行完全相同的代码但使用 express-graphql 时,

const expressGraphQL = require('express-graphql');

app.use('/graphql', expressGraphQL({
        schema,
        graphiql: true
    }))

这是连接怪物正在生成的查询:

SELECT
  "customers"."ALL_CUSTOMER_ID" AS "ALL_CUSTOMER_ID",
  "customers"."FIRST_NAME" AS "FIRST_NAME"
FROM ALL_CUSTOMER "customers"
WHERE "customers".UPPER_FIRST_NAME || ' ' || "customers".UPPER_LAST_NAME || ' ' || "customers".UPPER_FIRST_NAME like '%AS%'

一切都按预期进行。我错过了什么吗?

【问题讨论】:

    标签: javascript express graphql-js apollo-server express-graphql


    【解决方案1】:

    您找到解决此问题的方法了吗?除了我的类型中的 id 之外,我看到返回空值的情况相同。我使用的是express-graphql,一切正常,就像你描述的那样。当我将其从apollo-server-express 换成ApolloServer 时,我可以看到架构似乎可以正常加载并显示在 GraphQL 操场上,但是 sql 查询没有正常执行,导致到处都出现空值。

    在最新版本的join-monster (3.0, unreleased) 中,他们已将所有join-monster 特定语法移至extensions 属性中。我认为这可能会解决问题,如果它是添加到导致它的类型的额外非标准 GraphQL 属性。

    const User = new GraphQLObjectType({
      name: 'User',
      extensions: {
        joinMonster: {
          sqlTable: 'accounts', // the SQL table for this object type is called "accounts"
          uniqueKey: 'id' // id is different for every row
        }
      },
      fields: () => ({
        /*...*/
      })
    })

    【讨论】:

      猜你喜欢
      • 2021-05-06
      • 2020-03-27
      • 2021-12-01
      • 2017-08-08
      • 2022-07-27
      • 2023-03-23
      • 2021-05-09
      • 2020-03-24
      • 2018-01-05
      相关资源
      最近更新 更多