为什么不赢得GraphQL(在Node.js上)调用我的"解决"方法?

时间:2016-11-02 21:20:38

标签: graphql graphql-js

我试图在Node.js中实现一个非常基本的GraphQL接口,但无论我做什么,我似乎无法获得resolve foo方法要触发的类型。当我在单元测试中运行以下代码时它运行成功,但是我可以从(缺少)控制台输出中看到resolve没有被调用,结果我得到一个空对象。我打电话给graphql(FooSchema, query)

任何对GraphQL更有经验的人都会建议我可能做错了吗?如果GraphQL无法找到并调用应该返回结果的方法,我完全不知道整个操作如何成功完成...

const fooType = new GraphQLInterfaceType({
    name: `Foo`,
    description: `A foo`,
    fields: () => ({
        id: {
            description: `The foo's id`,
            type: new GraphQLNonNull(GraphQLInt),
        },
        title: {
            description: `The foo's title`,
            type: new GraphQLNonNull(GraphQLString),
        }
    })
});
const queryType = new GraphQLObjectType({
    fields: {
        foo: {
            args: {
                id: {
                    description: 'ID of the foo',
                    type: new GraphQLNonNull(GraphQLString)
                }
            },
            resolve: (root, { id }) => {
                console.log(12345); 
                return getFoo(id)
            },
            type: fooType,
        }
    },
    name: 'Query',
});

export default new GraphQLSchema({
    query: queryType,
    types: [fooType],
});

// In test:
const query = `
    foo {
        title
    }
`;
const result = graphql(FooSchema, query); // == {}

1 个答案:

答案 0 :(得分:1)

const fooType = new GraphQLInterfaceType({
    name: `Foo`,
    description: `A foo`,
    fields: () => ({
        id: {
            description: `The foo's id`,
            type: new GraphQLNonNull(GraphQLInt),
        },
        title: {
            description: `The foo's title`,
            type: new GraphQLNonNull(GraphQLString),
        }
    })
});

这是一种界面类型,但您的消费者queryType永远不会实施它。一个快速的解决方案应该是将其改为:

const fooType = new GraphQLObjectType({
    name: `Foo`,
    description: `A foo`,
    fields: () => ({
        id: {
            description: `The foo's id`,
            type: new GraphQLNonNull(GraphQLInt),
        },
        title: {
            description: `The foo's title`,
            type: new GraphQLNonNull(GraphQLString),
        }
    })
});

这是一个适合我的例子:

const {
  GraphQLNonNull,
  GraphQLInt,
  GraphQLString,
  GraphQLObjectType,
  GraphQLSchema,
  graphql,
} = require('graphql');

const fooType = new GraphQLObjectType({
  name: `Foo`,
  description: `A foo`,
  fields: () => ({
    id: {
      description: `The foo's id`,
      type: new GraphQLNonNull(GraphQLInt),
    },
    title: {
      description: `The foo's title`,
      type: new GraphQLNonNull(GraphQLString),
    },
  }),
});

const queryType = new GraphQLObjectType({
  fields: {
    foo: {
      args: {
        id: {
          description: 'ID of the foo',
          type: new GraphQLNonNull(GraphQLString),
        },
      },
      resolve: (root, { id }) => {
        return { id, title: 'some-title' };
      },
      type: fooType,
    },
  },
  name: 'Query',
});

const schema = new GraphQLSchema({
  query: queryType,
  types: [fooType],
});

graphql(schema, `{ foo (id:"123") { id, title } }`).then(console.log.bind(console));

这应该打印:

$ node test.js
{ data: { foo: { id: 123, title: 'some-title' } } }

以下是InterfaceType上的文档:http://graphql.org/learn/schema/#interfaces

相关问题