実現したいこと
auth-apiには Userが存在する
business-apiには
Busienss,
UserBusinessRelationが存在する。
Ruby
1class < UserBusinessRelation 2 user_id 3 business_id 4end
currentUserにbusinessesを含めたレスポンスを返したい。
発生している問題・分からないこと
ruby
1auth-api/app/graphql/types/user_type.rb 2module Types 3 class UserType < Types::BaseObject 4 field :id, ID, null: false 5 field :allow_password_change, Boolean, null: false 6 field :confirmation_sent_at, GraphQL::Types::ISO8601DateTime, null: true 7 field :confirmed_at, GraphQL::Types::ISO8601DateTime, null: true 8 field :email, String, null: false 9 field :name, String, null: true 10 field :nickname, String, null: true 11 field :provider, String, null: false 12 field :created_at, GraphQL::Types::ISO8601DateTime, null: false 13 field :updated_at, GraphQL::Types::ISO8601DateTime, null: false 14 field :role, Types::RoleType, null: false 15#businessesを追加したい 16 field :businesses, [Types::BusinessType], null: true 17 18 end 19end 20
該当のソースコード
js
1const { ApolloServer } = require('apollo-server'); 2const { ApolloGateway, IntrospectAndCompose, RemoteGraphQLDataSource } = require('@apollo/gateway'); 3 4class AuthenticatedDataSource extends RemoteGraphQLDataSource { 5 willSendRequest({ request, context }) { 6 request.http.headers.set('access-token', context["access-token"] || ''); 7 request.http.headers.set('client', context.client || ''); 8 request.http.headers.set('uid', context.uid || ''); 9 } 10} 11 12const gateway = new ApolloGateway({ 13 supergraphSdl: new IntrospectAndCompose({ 14 subgraphs: [ 15 { name: 'auth', url: 'http://localhost:3001/graphql' }, 16 { name: 'business_manage', url: 'http://localhost:3002/graphql' }, 17 ], 18 }), 19 debug: false, 20 buildService({ name, url }) { 21 return new AuthenticatedDataSource({ url }); 22 }, 23}); 24 25const server = new ApolloServer({ 26 gateway, 27 subscriptions: false, 28 context: ({ req }) => { 29 const accessToken = req.headers['access-token'] || ''; 30 const client = req.headers['client'] || ''; 31 const uid = req.headers['uid'] || ''; 32 return { "access-token": accessToken, client, uid }; 33 }, 34}); 35 36server.listen().then(({ url }) => { 37 console.log(`🚀 Gateway started at ${url}`); 38}); 39
ruby
1auth-api/app/graphql/types/user_type.rb 2module Types 3 class UserType < Types::BaseObject 4 field :id, ID, null: false 5 field :allow_password_change, Boolean, null: false 6 field :confirmation_sent_at, GraphQL::Types::ISO8601DateTime, null: true 7 field :confirmed_at, GraphQL::Types::ISO8601DateTime, null: true 8 field :email, String, null: false 9 field :name, String, null: true 10 field :nickname, String, null: true 11 field :provider, String, null: false 12 field :created_at, GraphQL::Types::ISO8601DateTime, null: false 13 field :updated_at, GraphQL::Types::ISO8601DateTime, null: false 14 field :role, Types::RoleType, null: false 15 field :businesses, [Types::BusinessType], null: true 16 end 17 18 def self.resolve_reference(reference, _context) 19 User.find(reference[:id]) 20 end 21 22 def businesses 23 #どうすれば外部のbusinessesを取得できるか 24 [] 25 end 26end 27 28 29 30 31auth-api/app/graphql/types/user_business_relation_type.rb 32module Types 33 class UserBusinessRelationType < Types::BaseObject 34 key fields: 'id' 35 extend_type 36 37 field :id, ID, null: false, external: true 38 field :business_id, Integer, null: false 39 field :user_id, Integer, null: false 40 41 def user 42 User.find(user_id) 43 end 44 45 def business 46 { __typename: Types::BusinessType, id: object[:business_id] } 47 end 48 end 49end 50 51 52auth-api/app/graphql/types/business_type.rb 53module Types 54 class BusinessType < Types::BaseObject 55 key fields: 'id' 56 extend_type 57 58 field :id, ID, null: false, external: true 59 field :address, String, null: false 60 field :building, String, null: false 61 field :city, String, null: false 62 field :name, String, null: false 63 field :phone_number, String, null: false 64 field :postal_code, String, null: false 65 field :prefecture, String, null: false 66 field :stripe_customer_id, String, null: true 67 field :created_at, GraphQL::Types::ISO8601DateTime, null: false 68 field :updated_at, GraphQL::Types::ISO8601DateTime, null: false 69 end 70end
ruby
1business-manage-api/app/graphql/types/business_type.rb 2# business-api の BusinessType 3module Types 4 class BusinessType < Types::BaseObject 5 key fields: 'id' 6 7 field :id, ID, null: false 8 field :address, String, null: false 9 field :building, String, null: false 10 field :city, String, null: false 11 field :name, String, null: false 12 field :phone_number, String, null: false 13 field :postal_code, String, null: false 14 field :prefecture, String, null: false 15 field :stripe_customer_id, String, null: true 16 field :created_at, GraphQL::Types::ISO8601DateTime, null: false 17 field :updated_at, GraphQL::Types::ISO8601DateTime, null: false 18 end 19end 20 21 22 23business-manage-api/app/graphql/types/user_business_relation_type.rb 24module Types 25 class UserBusinessRelationType < Types::BaseObject 26 key fields: 'id' 27 field :id, ID, null: false 28 field :business_id, Integer, null: false 29 field :user_id, Integer, null: false 30 31 def user 32 { __typename: Types::UserType, id: object[:user_id]} 33 end 34 35 def business 36 Business.find(business_id) 37 end 38 end 39end 40 41 42 43/Users/masatakakitano/Desktop/cloud-ver2/business-manage-api/app/graphql/types/user_type.rb 44module Types 45 class UserType < Types::BaseObject 46 key fields: 'id' 47 extend_type 48 49 field :id, ID, null: false, external: true 50 field :businesses, [Types::BusinessType], null: true 51 52 def businesses 53 UserBusinessRelation.where(user_id: object[:id]).map do |relation| 54 Business.find(relation.business_id) 55 end 56 end 57 end 58end
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
下記記事を参考にしましたが、若干パターンが違ったためうまく落とし込む事ができませんでした。https://blog.spacemarket.com/code/graphql-apollo-federation/
補足
特になし
あなたの回答
tips
プレビュー