关系GraphQL

时间:2017-11-11 18:12:17

标签: mongodb mongoose graphql mongoose-schema apollo-server

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

watch video

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

3 个答案:

答案 0 :(得分:4)

我克隆了你的项目并实现了一些代码,在这里我改变了以使关系有效。注意,我只是做了一个没有验证的基本代码或高级数据加载器,只是为了确保非复杂性。希望它可以提供帮助。

<强>的src / graphql /解析器/轿厢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.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 /解析器/ 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
      }
    }))
  )
}

enter image description here

enter image description here

enter image description here

enter image description here

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

您需要为用户类型的cars字段添加解析程序。

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