-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.js
More file actions
179 lines (171 loc) · 4.86 KB
/
schema.js
File metadata and controls
179 lines (171 loc) · 4.86 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import {
GraphQLString,
GraphQLObjectType,
GraphQLList,
GraphQLNonNull,
GraphQLSchema
} from 'graphql'; // Import specific Types from GraphQL
import SequelizeDatabase from './db';
// Custom Types
let BlobType = new GraphQLObjectType({
name: 'Blob',
fields: () => ({
field1: { type: GraphQLString },
field2: { type: GraphQLString },
field3: { type: GraphQLString }
})
});
// Individual Query
const Container = new GraphQLObjectType({
name: 'Container',
description: 'Represents a Container',
// Function that overlays Sequelize fields and returns object of field types
fields: () => {
return {
id: {
type: GraphQLString,
/**
* Resolve invokes retrieval of data from specific source (i.e. Sequelize).
* Resolve has Container object passed in as parameter that is available
* in response to query that returns Sequelized Container object from db.
*/
resolve(container) {
return container.id
}
},
data: {
type: BlobType,
resolve(container) {
return container.data
}
},
/**
* HAS MANY relationship allows add a (PLURAL) Associated query that may also be called
* as an Independent Query as a field of this query to allow Combined Queries
*/
sections: {
type: new GraphQLList(Section),
resolve(container) {
/**
* Sequelize automatically provides Dynamic Function
* getSections() function that we may call since we declared Associations
* between the two tables (hasMany and belongsTo)
*/
return container.getSections();
}
}
}
}
});
// Individual Query
const Section = new GraphQLObjectType({
name: 'Section',
description: 'Represents a Section',
fields: () => {
return {
id: {
type: GraphQLString,
resolve(section) {
return section.id
}
},
/**
* BELONGS TO relationship allows add a (SINGULAR) Associated query that may also be called
* as an Independent Query as a field of this query to allow Combined Queries
* IMPORTANT NOTE: Differs from HAS MANY as follows:
* - container NOT containers
* - getContainer NOT getContainers
* - Container NOT new GraphQLList(Container) (i.e. one container, not array of containers)
*/
container: {
type: Container,
resolve(section) {
return section.getContainer();
}
}
}
}
});
// Root Query (i.e. HTTP GET)
const RootQuery = new GraphQLObjectType({
name: 'RootQuery',
description: 'Represents the Root Query',
// Function that returns object of all Public API Methods to query against
fields: () => {
return {
containers: {
// Returns multiple records so use GraphQLList type
type: new GraphQLList(Container),
/**
* Return Promise from SequelizeDatabase instance.
* Arguments `args` are passed along with GraphQL Query
* (i.e. used to search/filter against the db columns).
*
* Security enforcement by restricting access to args using
* Input Data Sanitization (IDS) to prevent untrusted input
* and access to private database columns
*/
args: {
id: {
type: GraphQLString
}
},
resolve(root, args) {
return SequelizeDatabase.models.container.findAll({where: args});
}
},
sections: {
type: new GraphQLList(Section),
args: {
id: {
type: GraphQLString
}
},
/**
* Source is root/parent.
* Arguments is args
* Abstract Syntax Tree bits of request is ast
*/
resolve(source, args, ast) {
console.log(`
Root: ${source}\n\n
Args: ${args}\n\n
AST: ${ast}\n\n
`);
return SequelizeDatabase.models.section.findAll({where: args});
}
}
}
}
});
// Mutations (i.e. HTTP POST)
const Mutation = new GraphQLObjectType({
name: 'Mutation',
description: 'Functions to create database entries',
// Function that returns object of all Public API Methods to query/mutate against
fields() {
return {
addContainer: {
type: Container,
args: {
id: {
type: new GraphQLNonNull(GraphQLString)
}
},
// Prevent unauthorised client queries/mutations in `resolve` method by throwing error
resolve(_, args) {
if (args.id)
return SequelizeDatabase.models.container.create({
id: args.id
})
}
}
}
}
});
// Schema
const GraphQLQuerySchema = new GraphQLSchema({
query: RootQuery,
mutation: Mutation
});
export default GraphQLQuerySchema;