Apollo / GraphQL:为字符串字段设置解析器?

时间:2016-09-03 18:02:06

标签: meteor graphql apollo-server

http://localhost:8080/graphiql的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 };

SCHEMA

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;

2 个答案:

答案 0 :(得分:0)

定义GraphQLObjectType时,您需要为每个字段提供解析器。

您使用多个字段定义了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 })
            }
        }
    })
})

答案 1 :(得分:0)

问题是查询不期望数组, 请修理一下: type Query { instant_message(fromID: String, toID: String, msgText: String): [instant_message] }

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