我是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是查询的一部分,但是我可以提出一个方案,其中我需要的参数仅在可用数据中在后端。

更新:

根据Daniel的要求(谢谢),这是整个测试文件,该文件将创建包含缝合的方案:

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,
    );
  }
},

10-06 12:27
查看更多