0

在 GraphiQL 中http://localhost:8080/graphiql,我正在使用以下查询:

{
  instant_message(fromID: "1"){
    fromID
    toID
    msgText
  }
}

我收到以下回复:

{
  "data": {
    "instant_message": {
      "fromID": null,
      "toID": null,
      "msgText": null
    }
  },
  "errors": [
    {
      "message": "Resolve function for \"instant_message.fromID\" returned undefined",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ]
    },
    {
      "message": "Resolve function for \"instant_message.toID\" returned undefined",
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ]
    },
    {
      "message": "Resolve function for \"instant_message.msgText\" returned undefined",
      "locations": [
        {
          "line": 5,
          "column": 5
        }
      ]
    }
  ]
}

我尝试根据此处找到的示例设置我的系统:

https://medium.com/apollo-stack/tutorial-building-a-graphql-server-cddaa023c035#.s7vjgjkb7

看那篇文章,似乎没有必要为字符串字段设置单独的解析器,但我一定遗漏了一些东西。

更新解析器以从字符串字段返回结果的正确方法是什么?示例代码将不胜感激!

非常感谢大家的任何想法或信息。

连接器

import Sequelize from 'sequelize';

//SQL CONNECTORS
const db = new Sequelize(Meteor.settings.postgres.current_dev_system.dbname, Meteor.settings.postgres.current_dev_system.dbuser, Meteor.settings.postgres.current_dev_system.dbpsd, {
  host: 'localhost',
  dialect: 'postgres',

});

db
    .authenticate()
    .then(function(err) {
        console.log('Connection to Sequelize has been established successfully.');
    })
    .catch(function (err) {
        console.log('Unable to connect to the Sequelize database:', err);
    });

const IMModel = db.define('IM', {
    id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
    fromID: {type: Sequelize.STRING},
    toID: {type: Sequelize.STRING},
    msgText: {type: Sequelize.STRING}
});

IMModel.sync({force: true}).then(function () {
    // Table created
    return IMModel.create({
        fromID: '1',
        toID: '2',
        msgText: 'msg set up via IMModel.create'
    });
});

const IM = db.models.IM;
export {db, IM };

架构

const typeDefinitions = [`

type instant_message {
  id: Int
  fromID: String
  toID: String
  msgText: String
}
type Query {
  instant_message(fromID: String, toID: String, msgText: String): instant_message
}
type RootMutation {
  createInstant_message(
    fromID: String!
    toID: String!
    msgText: String!
  ): instant_message
}
schema {
  query: Query,
  mutation: RootMutation
}
`];

export default typeDefinitions;

旋转变压器

import * as connectors from './db-connectors';
import { Kind } from 'graphql/language';
const b = 100;

const resolvers = {
    Query: {
        instant_message(_, args) {
            const a = 100;
            return connectors.IM.find({ where: args });
        }
    },
    RootMutation: {
        createInstant_message: (__, args) => { return connectors.IM.create(args); },
  },

};

export default resolvers;
4

2 回答 2

0

当您定义您GraphQLObjectType的 s 时,您需要为它们的每个字段提供一个解析器。

您定义instant_message了多个字段,但没有为每个字段提供解析器。此外,您使用常规打字稿字段定义了这些字段的类型,而您需要使用GraphQL类型(GraphQLInt, GraphQLString, GrapQLFloat等)定义它

所以定义你的类型应该是这样的:

let instant_message = new GraphQLObjectType({
  id: { 
    type: GraphQLInt,
    resolve: (instantMsg)=> {return instantMsg.id}
  }
  fromID: { 
    type: GraphQLString,
    resolve: (instantMsg)=> {return instantMsg.fromID}
  }
  toID: {
    type: GraphQLString,
    resolve: (instantMsg)=> {return instantMsg.toID}
  }
  msgText: { 
    type: GraphQLString,
    resolve: (instantMsg)=> {return instantMsg.msgText}
  }
})

此外,您需要按如下方式定义查询:

let Query = new GraphQLObjectType({
    name: "query",
    description: "...",

    fields: () => ({
        instant_messages: {
            type: new GraphQLList(instant_message),
            args: {
                id: {type: GraphQLInt}
            },
            resolve: (root, args) => {
                connectors.IM.find({ where: args })
            }
        }
    })
})
于 2016-09-03T20:00:40.770 回答
0

问题是查询不需要数组,请修复它: type Query { instant_message(fromID: String, toID: String, msgText: String): [instant_message] }

然后你应该确保解析器返回对象数组,如果它不起作用,那么解析器不会返回一个数组。

于 2016-09-04T19:15:06.470 回答