Graphql仅在传递参数时执行where条件

时间:2019-07-23 12:24:41

标签: node.js oracle graphql oracle-sqldeveloper

我在节点js中为我的Oracle数据库使用graphql,其中我连接到远程数据库并获取一些详细信息。我对这些技术还很陌生,所以请原谅我。我有一个具有以下架构的customer表:

const Customer = new GraphQLObjectType({
  description: 'Customer data schema',
  name: 'Customer',
  fields: () => ({
    name: {
      type: GraphQLString,
      sqlColumn: 'NAME',
    },
    city: {
      type: GraphQLString,
      sqlColumn: 'CITY'
    },
    country: {
      type: GraphQLString,
      sqlColumn: 'COUNTRY'
    },
    gender: {
      type: GraphQLString,
      sqlColumn: 'GENDER'
    },
    emp_id: {
      type: GraphQLString,
      sqlColumn: 'EMP_ID'
    }
  })
});

Customer._typeConfig = {
  sqlTable: 'CUSTOMER',
  uniqueKey: ['NAME','EMP_ID']
}

使用加入怪物,我将查询根目录创建为:

const QueryRoot = new GraphQLObjectType({
  description: 'global query object',
  name: 'RootQuery',
  fields: () => ({
    customer: {
      type: new GraphQLList(Customer),
      args: {
        emp_id: {
          description: 'Emp Id',
          type: GraphQLString
        },
        name: {
          description: 'Customer Name',
          type: GraphQLString
        }
      },
      where: (customer, args, context) => {
        return `${customer}."EMP_ID" = :emp_id AND ${customer}."NAME" = :name`;
      },
      resolve: (parent, args, context, resolveInfo) => {
        return joinMonster(resolveInfo, context, sql => {
          console.log('joinMaster', sql);
          return database.simpleExecute(sql, args,{
            outFormat: database.OBJECT
        });
        });
      }
    }
  })
})

当我在emp_id和name参数的浏览器中的graphql中传递查询时,我得到了数据。但是在某些情况下,我无法传递任何参数,并且希望获取所有行。

当我不发送参数时,我得到如下错误: ORA-01008 : Not all variables bound

我希望参数是可选的,如果我不发送参数,那么它将返回所有行。

谢谢。

1 个答案:

答案 0 :(得分:1)

whereresolver函数都传递了一个args参数。这将具有参数名称和值(如果有)。您可以使用该参数来构建动态的where子句。这是一个未经测试的示例:

const QueryRoot = new GraphQLObjectType({
  description: 'global query object',
  name: 'RootQuery',
  fields: () => ({
    customer: {
      type: new GraphQLList(Customer),
      args: {
        emp_id: {
          description: 'Emp Id',
          type: GraphQLString
        },
        name: {
          description: 'Customer Name',
          type: GraphQLString
        }
      },
      where: (customer, args, context) => {
        if (Object.keys(args).length === 0) {
          return false;
        }

        let whereClause = '1 = 1';

        if (args.emp_id != undefined) {
          whereClause += `\n  AND ${customer}."EMP_ID" = :emp_id`;
        }

        if (args.name != undefined) {
          whereClause += `\n  AND ${customer}."NAME" = :name`;
        }

        return whereClause;
      },
      resolve: (parent, args, context, resolveInfo) => {
        return joinMonster(resolveInfo, context, sql => {
          console.log('joinMaster', sql);
          return database.simpleExecute(sql, args,{
            outFormat: database.OBJECT
          });
        });
      }
    }
  })
})

由于where子句将与参数数目匹配,因此您不应出现ORA-01008错误。