node.js - GraphQL 嵌套解析器

标签 node.js graphql graphql-js

我是 GraphQL 新手,我正在尝试设置一个演示应用程序,以便向我的同事进行一些介绍性演讲。

我正在使用 NodeJS。

我提出了以下架构:

type Query {
  author(id:Int!): Author
  song(id:Int!): Song
}
type Author {
  id:Int!
  name:String!
  songs(max:Int):[Song]!
}
type Song {
  id:Int!
  name:String!
  author:Author!
}

这是与 Song.author 关联相关的解析器部分:

[...]
Song: {
  author: ({ id }, args, context, info) => {
    return mergeInfo.delegate(
      'query',
      'author',
      { id },
      context,
      info,
    );
  }
}
[...]

因此,这种方法的问题是我需要将 Song.id 添加到包含查询中,以便能够在其中包含 Song.author:

{
  song(id: 1) {
    id
    author {
      name
    }
  }
}

以下内容不起作用:

{
  song(id: 1) {
    author {
      name
    }
  }
}

根据实现的不同,它会给我一个错误或null(这会更糟)。

这迫使编写查询的人知道后端的实现细节,这显然是不好的。 :P

有人有办法解决这个问题吗?有什么我忽略的吗?

我尝试过使用 info 对象,但这可以解决问题,因为我需要的 id 是查询的一部分,但我可以想出一个场景,其中我需要的参数位于仅在后端可用的数据中。

更新:

根据丹尼尔的要求(谢谢),这是创建模式(包括拼接)的整个测试文件:

const { makeExecutableSchema, mergeSchemas } = require('graphql-tools');

const DATA = {
  authors: {
    1: { id: 1, name: 'John' },
    2: { id: 2, name: 'Paul' },
  },
  songs: {
    1: { id: 1, name: 'Love me do', authorId: 1 },
    2: { id: 2, name: 'I wanna be your man', authorId: 1 },
    3: { id: 3, name: 'I\'ll be back', authorId: 2 },
  }
};

const authorsTypes = `
  type Query {
    author(id:Int!): Author
  }
  type Author {
    id:Int!
    name:String!
  }
`;
const authorSchema = makeExecutableSchema({
  typeDefs: authorsTypes,
  resolvers: {
    Query: {
      author: (_, { id }) => DATA.authors[id],
    },
  },
});
const authorsLinksTypes = `
  extend type Author {
    songs(max:Int):[Song]!
  }
`;
const authorsLinksResolvers = mergeInfo => ({
  Author: {
    songs: ({ id }, args, context, info) => {
      return Object.values(DATA.songs).filter(it => it.authorId === id)
    }
  },
});

const songsTypes = `
  type Query {
    song(id:Int!): Song
  }
  type Song {
    id:Int!
    name:String!
  }
`;
const songsSchema = makeExecutableSchema({
  typeDefs: songsTypes,
  resolvers: {
    Query: {
      song: (_, { id }) => DATA.songs[id],
    },
  },
});
const songsLinksTypes = `
  extend type Song {
    author:Author!
  }
`;
const songsLinksResolvers = mergeInfo => ({
  Song: {
    author: ({ id }, args, context, info) => {
      return mergeInfo.delegate(
        'query',
        'author',
        { id },
        context,
        info,
      );
    }
  },
});

module.exports = mergeSchemas({
  schemas: [authorSchema, songsSchema, songsLinksTypes, authorsLinksTypes],
  resolvers: mergeInfo => ({
    ...songsLinksResolvers(mergeInfo),
    ...authorsLinksResolvers(mergeInfo),
  }),
});

最佳答案

处理这个问题的最简单方法是利用上下文来传递歌曲 ID。这意味着您需要修改 song 查询的解析器,如下所示:

Query: {
  song: (_, { id }, context) => {
    context.songId = id
    return DATA.songs[id]
  },
},

然后您可以从作者解析器内的上下文中获取 id

Song: {
  author: (song, args, context, info) => {
    const id = song.id || context.songId
    return mergeInfo.delegate(
      'query',
      'author',
      { id },
      context,
      info,
    );
  }
},

关于node.js - GraphQL 嵌套解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49317788/

相关文章:

node.js - 无服务器异步函数错误

graphql - Github v4 GraphQL API。按日期获取提交

node.js - Sequelize 和响应请求 GraphQL

javascript - GraphQL 字段作为函数

reactjs - Graphql:如果参数类型为字符串,则跳过该参数

graphql-js 中的 graphql 错误和状态消息

node.js - 如何在 Mongoose 文档中选择具有匹配内部值的内部对象?

node.js - jenkins 通过代理配置 npm install

node.js - 重定向后Nodejs req.sessionID偶尔会发生变化

javascript - Meal.nutrition 的类型必须是 Output Type 但得到 : undefined