【问题标题】:Why won't GraphQL (on Node.js) call my "resolve" method?为什么 GraphQL(在 Node.js 上)不会调用我的“resolve”方法?
【发布时间】:2016-11-03 06:58:10
【问题描述】:

我正在尝试在 Node.js 中实现一个非常基本的 GraphQL 接口,但无论我做什么,我似乎都无法触发我的 foo 类型的 resolve 方法。当我在单元测试中运行以下代码时,它运行成功,但是我可以从(缺少)控制台输出中看到 resolve 没有被调用,因此当我调用 @987654324 时我得到一个空对象@。

任何对 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); // == {}

【问题讨论】:

    标签: graphql graphql-js


    【解决方案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

    【讨论】:

    • 感谢您的建议:我一直在复制/粘贴《星球大战》示例,但我一定没有足够注意我要复制的内容的类型。但是,将fooType 更改为新的GraphQLObjectType 并不能解决我的问题:我的resolve 仍然没有被调用,graphql 调用仍然返回一个空对象:(
    • return getFoo(id) 到底是做什么的?
    • 什么都没有(或者更准确地说,它是无关紧要的),因为它甚至不会被调用。这是根本问题:如果我可以让graphql 调用它(通过resolve),我会准备好的。
    • 我编辑了答案以显示在我使用此查询时适用于本地的代码:` { foo(id: "123") { id title } } `
    • 嗯。所以我直接复制/粘贴了你的代码,然后我运行了我的测试,它只是graphql(FooSchema, 'foo(id: "123") { id title })' ...我仍然得到一个空对象。当我修改您的 resolve 方法以执行 console.log 时,我看不到任何日志输出。我完全相信您的代码可以在您的机器上运行......可能有任何环境因素吗?
    猜你喜欢
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 2013-07-17
    • 2014-12-14
    • 1970-01-01
    相关资源
    最近更新 更多