GraphQL 如何做一个 JOIN 请求而不是许多顺序请求? [英] GraphQL how to do a JOIN request instead of many sequential request?

查看:30
本文介绍了GraphQL 如何做一个 JOIN 请求而不是许多顺序请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种 GraphQL 类型:

type 作者 {id:字符串!名称:字符串!}类型书{id:字符串!作者:作者!名称:字符串!}

在我的数据库中,是通过books表内的外键实现的:

表作者(伪代码)

`id` INTEGER UNSIGNED`名称`字符串

桌书(伪代码)

`id` INTEGER UNSIGNED`author_id` INTEGER UNSIGNED REFERENCE `authors.id``名称`字符串

所以当我解析一个 GraphQL 查询时:

查询所有书籍{ID姓名作者 {ID姓名}}

我只想做一个 SELECT SQL 查询,例如:

SELECT books.id、books.name、authors.id、authors.name来自书籍左加入作者 onauthors.id = books.author_id

代替当前:

SELECT books.id、books.name、books.author_id来自书籍选择authors.id,authors.name来自作者WHERE author.id = [第一个SELECT的books.author_id]选择authors.id,authors.name来自作者WHERE author.id = [第一个SELECT的books.author_id][...]

有没有办法知道在 GraphQL 解析器中查询了哪些子字段"?

预先感谢您的回答!

解决方案

我刚刚发现在解析器给出的第四个参数中,有一个查询字段的数组:info.fieldNodes[0].selectionSet.selections.

我没有找到任何关于此的文档,我想知道 fieldNodes 数组代表什么......(并且不喜欢在不知情的情况下以这种方式访问​​第一个元素......).

selections 对象包含一个这样的数组:

<预><代码>[{种类:'字段',别名:未定义,名称:{种类:'名称',值:'id',位置:[对象]},参数:[],指令:[],选择集:未定义,位置:{开始:36,结束:38}},{[...]},...]

这里,value: 'id' 匹配相关作者的查询字段的名称.

如果我再深入一层,selectionSet: undefined 就会变成一个对象,并且模式会递归地重复自己......

I have two GraphQL type:

type Author {
  id: String!
  name: String!
}

type Book {
  id: String!
  author: Author!
  name: String!
}

In my database, it is implemented by a foreign key inside the books table:

table authors (pseudo code)

`id` INTEGER UNSIGNED
`name` STRING

table books (pseudo code)

`id` INTEGER UNSIGNED
`author_id` INTEGER UNSIGNED REFERENCE `authors.id`
`name` STRING

So when I resolve a GraphQL query like:

query allTheBooks {
  id
  name
  author {
    id
    name
  }
}

I would like to do only one SELECT SQL query like:

SELECT books.id, books.name, authors.id, authors.name
FROM books
LEFT JOIN authors ON authors.id = books.author_id

Instead of the current:

SELECT books.id, books.name, books.author_id
FROM books

SELECT authors.id, authors.name
FROM authors
WHERE author.id = [one the the books.author_id of the first SELECT]

SELECT authors.id, authors.name
FROM authors
WHERE author.id = [one the the books.author_id of the first SELECT]

[...]

Is there a way to know which "child fields" are queried in a GraphQL resolver ?

Thank you in advance for your answer !

解决方案

I just discovered that in the fourth parameter gived at the resolver, there where an array of the queried fields: info.fieldNodes[0].selectionSet.selections.

I didn't found any documentation about this, and I wonder what represent the fieldNodes array... (and dont like to access the first element that way without knowing it...).

The selections object contains an array like this:

[
  {
    kind: 'Field',
    alias: undefined,
    name: { kind: 'Name', value: 'id', loc: [Object] },
    arguments: [],
    directives: [],
    selectionSet: undefined,
    loc: { start: 36, end: 38 }
  },
  {
    [...]
  },
  ...
]

Here, the value: 'id' match the name of the queried field of the related author.

If I go a level deeped, the selectionSet: undefined becomes an object and the pattern repeat itself recursively...

这篇关于GraphQL 如何做一个 JOIN 请求而不是许多顺序请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆