首页 > 解决方案 > 为什么孩子的resolve函数不包含父母的resolve函数的结果?

问题描述

我试图隔离这个例子,我希望它没问题。我知道,这不是很好的代码,但我希望你能理解。

解析器暂时返回一个静态结果对象。

这是我的问题:

公司解析函数的结果应该传递给用户的解析函数。但这并没有发生,我想知道我错过了什么。

const GraphQL = require('graphql');

const UserType = new GraphQL.GraphQLObjectType({
  name: 'User',
  fields: {
    givenName: { type: GraphQL.GraphQLString },
    familyName: { type: GraphQL.GraphQLString },
    city: { type: GraphQL.GraphQLString },
  },
});

const CompanyType = new GraphQL.GraphQLObjectType({
  name: 'Company',
  fields: {
    legalName: { type: GraphQL.GraphQLString },
    city: { type: GraphQL.GraphQLString },
    employees: { type: new GraphQL.GraphQLList(UserType) },
  },
});

const queryDef = new GraphQL.GraphQLObjectType({
  name: 'Query',
  fields: {
    user: {
      type: UserType,
      args: {
        id: { type: GraphQL.GraphQLID },
        givenName: { type: GraphQL.GraphQLString },
        familyName: { type: GraphQL.GraphQLString },
        city: { type: GraphQL.GraphQLString },
      },
      resolve: (parent, args, context, info) => {
        console.log('parent should provide company object', parent);
        // currentyl parent is undefined
        return {
          id: 10,
          givenName: 'test',
        };
      },
    },
    company: {
      type: CompanyType,
      args: {
        id: { type: GraphQL.GraphQLID },
      },
      resolve: (parent, args, context, info) => {
        return {
          id: 3,
          legalName: 'legal test name',
          city: 'company location',
        };
      },
    },
  },
});

const schema = new GraphQL.GraphQLSchema({ query: queryDef });

const companyQuery = `
  {
    company(id: 1) { 
      city 
      employees { 
        familyName 
      }  
    } 
  }`;

GraphQL.graphql(schema, companyQuery).then( (companyResult) => {
  console.log(companyResult);
} ).catch( (err) => {
  console.error(err);
});

标签: node.jsgraphql

解决方案


推荐阅读