forked from du5rte/graphql-mongodb-projection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.js
89 lines (75 loc) · 1.8 KB
/
schema.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import {
GraphQLObjectType,
GraphQLSchema,
GraphQLID,
GraphQLString,
GraphQLList,
GraphQLEnumType,
GraphQLNonNull,
GraphQLUnionType,
} from 'graphql'
import db from './MongoDBMockup'
import graphqlMongodbProjection from '../src'
const UserType = new GraphQLObjectType({
name: 'User',
description: 'User Object',
fields: () => ({
_id: {type: new GraphQLNonNull(GraphQLID)},
email: {type: new GraphQLNonNull(GraphQLString)},
firstname: {type: new GraphQLNonNull(GraphQLString)},
lastname: {type: new GraphQLNonNull(GraphQLString)},
friends: {type: new GraphQLList(PersonType)},
avatar: {type: GraphQLString}
})
})
const UnnamedUserType = new GraphQLObjectType({
name: 'UnnamedUser',
fields: {
_id: {type: new GraphQLNonNull(GraphQLID)},
email: {type: new GraphQLNonNull(GraphQLString)}
}
})
const PersonType = new GraphQLUnionType({
name: 'Person',
types: [ UserType, UnnamedUserType ],
resolveType(value) {
if (value.firstname) {
return UserType;
}
return UnnamedUserType;
}
})
const user = {
type: UserType,
description: 'Get user by ID',
args: {
_id: {type: new GraphQLNonNull(GraphQLString)},
},
resolve(root, { _id }, ctx, info) {
const projection = graphqlMongodbProjection(info, {
'avatar': 'profile.avatar'
})
console.log(projection)
return db.findOne({ _id }, projection)
}
}
const people = {
type: new GraphQLList(PersonType),
description: 'Get people',
args: {},
resolve(root, args, ctx, info) {
const projection = graphqlMongodbProjection(info)
console.log(projection)
return db.find({}, projection)
}
}
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Queries',
fields: () => ({
user,
people
})
})
})
export default schema