2017-11-11 137 views
0

第二週,我嘗試鏈接apollo-server-express/MongoDB/Mongoose/GraphQL堆棧中的兩個集合,但我不明白。我發現了一個與REST API相似的課程,我需要的是所謂的關係。我需要這個,但在GraphQL關係GraphQL

watch video

如何車添加到用戶? 我收集的測試服務器,該代碼是在這裏:https://github.com/gHashTag/test-graphql-server 幫助

回答

0

我克隆了你的項目,並實現了一些代碼,這裏我改變了關係的作品。請注意,我只是做了一個基本的代碼,沒有驗證或提前dataloader只是爲了確保非複雜性。希望它可以幫助。

的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

+0

非常感謝您,表瞭解一對多通信,但如何使連接多對多? –

0

嘗試做這樣的事情在你的車解析器

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

您需要在用戶類型添加一個解析器爲cars場。

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