2017-05-10 31 views
3

私は何か不足しているかもしれませんが、新しいエントリを作成するときに多対多リレーションシップを設定する方法について、Apollo docsに関する情報は見つかりません。graphqlの突然変異で多対多の関係を設定するには?

リレーションが1対多である場合、多くの側のオブジェクトでリレーションシップの一方のIDを設定するのと同じくらい簡単です。

私は本と作家で働いているようですが、1人(または多くの人)の著者のためにブックを作成するgraphqlクエリを書くにはどうすればよいですか?

答えて

4

これは、おそらくGraphQLサーバー(つまりスキーマ)のAPIレイヤで発生するはずです。多対多リレーションシップの場合は、BookAuthor多対多リレーションシップを表す「結合」型を作成し、その結合型にエントリを追加する必要があります。

実質的にはBookと呼ばれるタイプ、もう1つはAuthorと呼ばれるタイプ、もう1つはBookAuthorと呼ばれるタイプです。そして、その関係を管理できるようにいくつかの変異を加えることができます。多分...

  • addToBookAuthorConnection
  • updateBookAuthorConnection
  • removeFromBookAuthorConnection

これは、リレーの仕様に準拠しAPIを使用して、従来のセットアップです。あなたはread more about how to structure your API for many-to-many relationships hereです。

次に、フロントエンドでその多対多接続を追加できるようにする代わりに、addToBookAuthorConnectionの突然変異をApolloから呼び出すだけで済みます。

希望すると便利です。

+0

説明をありがとう!!私はgraph.coolをバックエンドとして使用していると述べていたはずです。この時点ではドキュメントを掘り下げていませんでした。ドキュメントはこのトピックでは不足しているようですが、ヘルプデスクに連絡しました。私はここで更新します。 – vgrafe

+0

graphiQLの自動補完の助けを借りて関係の名前を見つけました。それらの名前が私のスキーマにはどこにも現れないので、非常に便利です。 – vgrafe

+2

Gotcha! GraphiQLは、APIを掘り下げて何が存在するかを見るのに最適なツールです。うまくいけば、関係は今より明確です。時間がある場合は、[Scaphold](https://scaphold.io)もチェックしてください。 – vince

1

ウルは、多くの関係への1、その後connectors.js、resolvers.jsとアポログラフサーバを使用して、与えられたフォーマットとしてファイルをschema.js場合

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 }; 
+0

フィールドを明示的に定義してこのようにするのはどういう違いがありますか? AuthorModel.hasMany(PostModel、{Through: 'AuthorPosts'}); – CopyJosh