尝试将graphql连接到postgress如何定义用户并通过?

时间:2019-04-23 14:04:39

标签: postgresql graphql

尝试连接graphql时,我无法从postgres数据库接收信息。

我收到以下答复:

{
  "errors": [
    {
      "message": "password authentication failed for user \"admin\"",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "account"
      ]
    }
  ],
  "data": {
    "account": null
  }
}

老实说,我不知道在哪里定义用户并传递。

const express = require('express');
const expressGraphQL = require('express-graphql');
const schema = require('./schema');
const app = express();

app.use('/graphql', expressGraphQL({
   schema,
   graphiql: true
}))
app.listen(4000, () => {
   console.log('Listening...')
})

这是我的架构文件

const graphql = require('graphql');
const connectionString = 'myURI';
const pgp = require('pg-promise')();
const db = {}
db.conn = pgp(connectionString);
const {
   GraphQLObjectType,
   GraphQLID,
   GraphQLString,
   GraphQLBoolean,
   GraphQLList,
   GraphQLSchema
} = graphql;
const AccountType = new GraphQLObjectType({
   name: 'account',
   fields: () => ({
      id: { type: GraphQLID },
      name: { type: GraphQLString },
      busines_name: { type: GraphQLString },
      email: {
         type: new GraphQLList(EmailType),
         resolve(parentValue, args) {
            const query = `SELECT * FROM "emails" WHERE
            account=${parentValue.id}`;
            return db.conn.many(query)
               .then(data => {
                  return data;
               })
               .catch(err => {
                  return 'The error is', err;
               });
         }
      }
   })
})
const EmailType = new GraphQLObjectType({
   name: 'Email',
   fields: {
      id: { type: GraphQLID },
      email: { type: GraphQLString },
      primary: { type: GraphQLBoolean }
   }
})
const RootQuery = new GraphQLObjectType({
   name: 'RootQueryType',
   fields: {
      account: {
      type: AccountType,
      args: { id: { type: GraphQLID } },
      resolve(parentValue, args) {
         const query = `SELECT * FROM "account" WHERE id=${args.id}`;
         return db.conn.one(query)
            .then(data => {
               return data;
            })
            .catch(err => {
                return 'The error is', err;
            });
      }
   },
   emails: {
      type: EmailType,
      args: { id: { type: GraphQLID } },
      resolve(parentValue, args) {
         const query = `SELECT * FROM "emails" WHERE id=${args.id}`;
         return db.conn.one(query)
            .then(data => {
               return data;
            })
            .catch(err => {
               return 'The error is', err;
            });
        }
      }
   }
})
module.exports = new GraphQLSchema({
   query: RootQuery
})

我想知道在哪里定义用户以及我正在做错的db密码。

1 个答案:

答案 0 :(得分:0)

const connectionString = 'myURI'; 如果您的连接字符串包含用户名和密码,就足够了。您的数据库连接字符串的格式为postgres:// username:password @ server:5432吗? 参见https://www.postgresql.org/docs/10/libpq-connect.html#LIBPQ-CONNSTRING