本文是小编为大家收集整理的关于如何在graphql突变中设置多对多的关系?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我可能会缺少某些内容,但找不到有关Apollo文档的任何信息,以建立新的条目时设置多对多关系的方法.
当关系是一对多的时,就像在多边对象中设置关系的一侧的ID一样简单.
但是让我们假装我正在与书籍和作者合作,我将如何编写一个为一个(还是很多?)创建书籍的GraphQl查询?
推荐答案
这可能发生在GraphQl Server上的API层(即架构)上.对于多一对人际关系,您应该有一个"加入"类型来表示BookAuthor多对多的关系,然后将条目添加到该类型.
从本质上讲,您将拥有一种称为Book的类型,另一种称为Author,最后一个称为BookAuthor.您可以添加一些突变以控制这种关系.也许...
- addToBookAuthorConnection
- updateBookAuthorConnection
- removeFromBookAuthorConnection
这是使用符合继电器规格的API的常规设置.您可以 row .
然后,您只需要调用来自阿波罗的addToBookAuthorConnection突变,就可以添加到前端上的多对多连接中.
希望这会有所帮助!
其他推荐答案
如果您使用Apollo图形服务器与一到多个关系,则连接器.
schema.js
const typeDefinitions = ` type Author { authorId: Int firstName: String lastName: String posts: [Post] } type Post { postId: Int title: String text: String views: Int author: Author } input postInput{ title: String text: String views: Int } type Query { author(firstName: String, lastName: String): [Author] posts(postId: Int, title: String, text: String, views: Int): [Post] } type Mutation { createAuthor(firstName: String, lastName: String, posts:[postInput]): Author updateAuthor(authorId: Int, firstName: String, lastName: String, posts:[postInput]): String } schema { query: Query mutation:Mutation } `; export default [typeDefinitions];
Resolvers.js
import { Author } from './connectors'; import { Post } from './connectors'; const resolvers = { Query: { author(_, args) { return Author.findAll({ where: args }); }, posts(_, args) { return Post.findAll({ where: args }); } }, Mutation: { createAuthor(_, args) { console.log(args) return Author.create(args, { include: [{ model: Post, }] }); }, updateAuthor(_, args) { var updateProfile = { title: "name here" }; console.log(args.authorId) var filter = { where: { authorId: args.authorId }, include: [ { model: Post } ] }; Author.findOne(filter).then(function (product) { Author.update(args, { where: { authorId: args.authorId } }).then(function (result) { product.posts[0].updateAttributes(args.posts[0]).then(function (result) { //return result; }) }); }) return "updated"; }, }, Author: { posts(author) { return author.getPosts(); }, }, Post: { author(post) { return post.getAuthor(); }, }, }; export default resolvers;
connectors.js
import rp from 'request-promise'; var Sequelize = require('sequelize'); var db = new Sequelize('test', 'postgres', 'postgres', { host: '192.168.1.168', dialect: 'postgres', pool: { max: 5, min: 0, idle: 10000 } }); const AuthorModel = db.define('author', { authorId: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, field: "author_id" }, firstName: { type: Sequelize.STRING, field: "first_name" }, lastName: { type: Sequelize.STRING, field: "last_name" }, },{ freezeTableName: false, timestamps: false, underscored: false, tableName: "author" }); const PostModel = db.define('post', { postId: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, field: "post_id" }, text: { type: Sequelize.STRING }, title: { type: Sequelize.STRING }, views: { type: Sequelize.INTEGER }, },{ freezeTableName: false, timestamps: false, underscored: false, tableName: "post" }); AuthorModel.hasMany(PostModel, { foreignKey: 'author_id' }); PostModel.belongsTo(AuthorModel, { foreignKey: 'author_id' }); const Author = db.models.author; const Post = db.models.post; export { Author, Post };
问题描述
I may be missing something, but can not find any information on Apollo docs about the way to set a many-to-many relation when creating a new entry.
When the relation is one-to-many it is as simple as setting the ID of the one-side of the relationship in the many-side object.
But let's pretend I am working with Books and Authors, how would I write a graphql query that creates a Book for one (or many?) Authors?
推荐答案
This should probably happen at the API layer on the GraphQL server (i.e. schema). For many-to-many relationships, you should have a "join" type to denote the BookAuthor many-to-many relationship, and then add an entry to that join type.
Essentially then you'll have a type called Book, another called Author, and finally one more called BookAuthor. And you can add a few mutations to be able to manage that relationship. Perhaps...
- addToBookAuthorConnection
- updateBookAuthorConnection
- removeFromBookAuthorConnection
This is a conventional setup using a Relay-spec compliant API. You can read more about how to structure your API for many-to-many relationships here.
Then, you only need to call the addToBookAuthorConnection mutation from Apollo instead to be able to add to that many-to-many connection on your frontend.
Hope this helps!
其他推荐答案
If u r using apollo graph server with one to many relations then connectors.js, resolvers.js and schema.js files as given formats
schema.js
const typeDefinitions = ` type Author { authorId: Int firstName: String lastName: String posts: [Post] } type Post { postId: Int title: String text: String views: Int author: Author } input postInput{ title: String text: String views: Int } type Query { author(firstName: String, lastName: String): [Author] posts(postId: Int, title: String, text: String, views: Int): [Post] } type Mutation { createAuthor(firstName: String, lastName: String, posts:[postInput]): Author updateAuthor(authorId: Int, firstName: String, lastName: String, posts:[postInput]): String } schema { query: Query mutation:Mutation } `; export default [typeDefinitions];
resolvers.js
import { Author } from './connectors'; import { Post } from './connectors'; const resolvers = { Query: { author(_, args) { return Author.findAll({ where: args }); }, posts(_, args) { return Post.findAll({ where: args }); } }, Mutation: { createAuthor(_, args) { console.log(args) return Author.create(args, { include: [{ model: Post, }] }); }, updateAuthor(_, args) { var updateProfile = { title: "name here" }; console.log(args.authorId) var filter = { where: { authorId: args.authorId }, include: [ { model: Post } ] }; Author.findOne(filter).then(function (product) { Author.update(args, { where: { authorId: args.authorId } }).then(function (result) { product.posts[0].updateAttributes(args.posts[0]).then(function (result) { //return result; }) }); }) return "updated"; }, }, Author: { posts(author) { return author.getPosts(); }, }, Post: { author(post) { return post.getAuthor(); }, }, }; export default resolvers;
connectors.js
import rp from 'request-promise'; var Sequelize = require('sequelize'); var db = new Sequelize('test', 'postgres', 'postgres', { host: '192.168.1.168', dialect: 'postgres', pool: { max: 5, min: 0, idle: 10000 } }); const AuthorModel = db.define('author', { authorId: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, field: "author_id" }, firstName: { type: Sequelize.STRING, field: "first_name" }, lastName: { type: Sequelize.STRING, field: "last_name" }, },{ freezeTableName: false, timestamps: false, underscored: false, tableName: "author" }); const PostModel = db.define('post', { postId: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, field: "post_id" }, text: { type: Sequelize.STRING }, title: { type: Sequelize.STRING }, views: { type: Sequelize.INTEGER }, },{ freezeTableName: false, timestamps: false, underscored: false, tableName: "post" }); AuthorModel.hasMany(PostModel, { foreignKey: 'author_id' }); PostModel.belongsTo(AuthorModel, { foreignKey: 'author_id' }); const Author = db.models.author; const Post = db.models.post; export { Author, Post };