GraphQL模式拼接-无法读取未定义的属性'args'

时间:2019-03-11 19:07:03

标签: graphql apollo-server

这让我发疯了,仅仅是因为我无法从错误中获取足够的信息来追溯它。 Google也没有太大帮助。

简而言之,我一直在尝试使用this example来学习Schema Stitching,并且现成的效果很好。然后,我尝试将硬编码数据换成我自己的“真实”数据(存储在mLab中的NoSQL集合),并且可以通过Gateway API很好地但单独地查询每个API。当我尝试缝合它们时,我得到Cannot read property 'args' of undefined。最小堆栈跟踪显示了片段解析器中info.mergeInfo.delegateToSchema处的错误,但我什至无法执行console.log,但无济于事。

我正在为模型/模式使用猫鼬(当我不尝试将它们组合在一起时效果很好,因此我认为它不那么有用)。

我的Post.user片段:

export default schema => ({
  user: {
    fragment: `fragment PostFragment on Post { authorId }`,
    resolve(parent, args, context, info) {
      const id = parseInt(parent.user);
      return info.mergeInfo.delegateToSchema({
        schema,
        operation: 'query',
        fieldName: 'authorId',
        args: {
          id
        },
        context,
        info
      });
    }
  }
});

我的主要post.js解析器:

// ...
const resolvers = {
  Query: {
    allPosts: async () => {
      const posts = await Post.find(); // works by itself

      return posts;
    },
    post: async (parent, { id }) => {
      const post = await Post.findById(id); // works by itself
      return post;
    }
  }
};

export default resolvers;

我的关系图:

const relationsSchema = `
  extend type User {
    posts: [Post]
  }

  extend type Post {
    author: User
  }
`;

export default relationsSchema;

与架构合并:

import { mergeSchemas } from 'graphql-tools';

import makeResolvers from '../resolver';

import makePostSchema from '../remoteSchema/post';
import makeUserSchema from '../remoteSchema/user';
import relationsSchema from './relations';

// use schema stitching technique to merge schems together
export default async () => {
  const postSchema = await makePostSchema();
  const userSchema = await makeUserSchema();

  const resolvers = makeResolvers({ postSchema, userSchema });

  return mergeSchemas({
    schemas: [postSchema, userSchema, relationsSchema],
    resolvers
  });
};

如果您需要,我很乐意分享更多代码。在此先感谢!

1 个答案:

答案 0 :(得分:0)

我也有同样的经历。 对我来说,

  extend type User {
Customer(id: string): [Orders]

}   `;

这向我显示了args.id,就像我从graphiql传递的id一样。