关系 GraphQL

Posted

技术标签:

【中文标题】关系 GraphQL【英文标题】:Relationships GraphQL 【发布时间】:2018-04-24 18:31:21 【问题描述】:

第二周我尝试链接 apollo-server-express / MongoDB / Mongoose / GraphQL 堆栈中的两个集合,但我不明白如何。我发现了一个与 REST API 类似的课程,我需要的是关系。我需要这个,但在 GraphQL 中

watch video

如何向用户添加汽车? 我收集了测试服务器,代码在这里:https://github.com/gHashTag/test-graphql-server 帮助

【问题讨论】:

【参考方案1】:

我已经克隆了您的项目并实现了一些代码,在这里我进行了更改以使关系有效。请注意,我只是做了一个没有验证或高级数据加载器的基本代码,只是为了确保不复杂。希望对您有所帮助。

src/graphql/resolvers/car-resolvers.js

import Car from '../../models/Car'
import User from '../../models/User'

export default 
  getCar: (_,  _id ) => Car.findById(_id),
  getCars: () => Car.find(),
  getCarsByUser: (user, ) =>  Car.find(seller: user._id ), // for relationship
  createCar: async (_, args) => 
    // Create new car
    return await Car.create(args) 
  

src/graphql/resolvers/user-resolvers.js

import User from '../../models/User'

export default  
  getUser: (_,  _id ) => User.findById(_id),
  getUsers: () => User.find(),
  getUserByCar: (car, args) => User.findById(car.seller), // for relationship
  createUser: (_, args) => 
    return User.create(args) 
  

src/graphql/resolvers/index.js

import UserResolvers from './user-resolvers'
import CarResolvers from './car-resolvers'

export default 
  User:
    cars: CarResolvers.getCarsByUser  // tricky part to link query relation ship between User and Car
  ,
  Car:
    seller: UserResolvers.getUserByCar  // tricky part to link query relation ship between User and Car
  ,
  Query: 
    getUser: UserResolvers.getUser,
    getUsers: UserResolvers.getUsers,
    getCar: CarResolvers.getCar,
    getCars: CarResolvers.getCars
  ,

  Mutation: 
    createUser: UserResolvers.createUser,
    createCar: CarResolvers.createCar,
  

src/graphql/schema.js

export default`
  type Status 
    message: String!
  

  type User 
    _id: ID!
    firstName: String
    lastName: String
    email: String
    cars: [Car]
  

  type Car 
    _id: ID 
    make: String
    model: String
    year: String
    seller: User
  

  type Query 
    getUser(_id: ID!): User 
    getUsers: [User] 
    getCar(_id: ID!): Car 
    getCars: [Car] 
  

  type Mutation 
    createUser(firstName: String, lastName: String, email: String): User 
    // change from _id to seller, due to base on logic _id conflict with CarId 
    createCar(seller: ID!, make: String, model: String, year: String): Car 
  

  schema 
    query: Query
    mutation: Mutation
  
`

src/middlewares.js

import bodyParser from 'body-parser'
import  graphqlExpress, graphiqlExpress  from 'apollo-server-express'
import  makeExecutableSchema  from 'graphql-tools'

import typeDefs from '../graphql/schema'
import resolvers from '../graphql/resolvers'
import constants from './constants'

export const schema = makeExecutableSchema(
  typeDefs,
  resolvers
)

export default app => 

  app.use('/graphiql', graphiqlExpress(
    endpointURL: constants.GRAPHQL_PATH
  ))

  app.use(
    constants.GRAPHQL_PATH, 
    bodyParser.json(),
    graphqlExpress(req => (
      schema,
      context: 
        event: req.event
      
    ))
  )

【讨论】:

非常感谢,表理解是一对多的通信,但是多对多怎么连接呢?【参考方案2】:

尝试在您的汽车解析器中制作类似的东西

export default 
  getCar: ( _id: ownId ,  _id ) =>
    Car.findById(ownId || _id);
// here is the rest of your code

【讨论】:

【参考方案3】:

您需要为 User 类型的 cars 字段添加解析器。

const resolvers = 
  Query: 
    getUsers: ...
    getCars: ...
    ...
  ,
  Mutation: 
    ...
  ,
  User: 
    cars: ...
  

【讨论】:

以上是关于关系 GraphQL的主要内容,如果未能解决你的问题,请参考以下文章

GraphQL集成Spring和MongoDB

GraphQL-前端开发的利剑与桥梁

分分钟将 REST 转换为 GraphQL

IT人在南宁系列之架构篇 | GraphQL与微服务实战二三事

对象关系之聚合关系

集合论:关系