-
Notifications
You must be signed in to change notification settings - Fork 9
/
graphql-ld-handler.js
261 lines (241 loc) · 7.79 KB
/
graphql-ld-handler.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
require('chai').should();
const expect = require('chai').expect;
const GraphQLLDHandler = require('../../lib/handlers/graphql-ld-handler');
describe('GraphQLLDHandler', function () {
it('integer variable in query', () => {
const handler = new GraphQLLDHandler();
const query = '{ id @single name @single review @single { rating(value: $value) }}';
const variables = {
"value": "1"
};
const definedParameters = {
"value": {
"required": true,
"type": "integer",
"description": "Rating of the tv shows.",
"in": "path"
}
};
const expectedNewQuery = '{ id @single name @single review @single { rating(value: "1") }}';
const actualNewQuery = handler._substituteVariables(query, variables, definedParameters, '$', 'GRAPHQL');
actualNewQuery.should.eql(expectedNewQuery);
});
it('should be able to throw an error when an integer can\'t be parsed', () => {
const handler = new GraphQLLDHandler();
const query = '{ id @single name @single review @single { rating(value: $value) }}';
const variables = {
"value": "b"
};
const definedParameters = {
"value": {
"required": true,
"type": "integer",
"description": "Rating of the tv shows.",
"in": "path"
}
};
expect(() => handler._substituteVariables(query, variables, definedParameters)).to.throw();
});
it('string variable in query', () => {
const handler = new GraphQLLDHandler();
const query = '{ id @single name @single review @single { rating(value: $value) }}';
const variables = {
"value": "1"
};
const definedParameters = {
"value": {
"required": true,
"type": "string",
"description": "Rating of the tv shows.",
"in": "path"
}
};
const expectedNewQuery = '{ id @single name @single review @single { rating(value: "1") }}';
const actualNewQuery = handler._substituteVariables(query, variables, definedParameters, '$', 'GRAPHQL');
actualNewQuery.should.eql(expectedNewQuery);
});
it('Query data source with expired certificate', async () => {
const handler = new GraphQLLDHandler(undefined, undefined, () => {});
const graphQLLDInfo = {
"queries": {
"projects": {
"query": "{ id(_:KNOWS) hasProject { id @single name @single description @single image @single url @single @optional }}"
},
},
"context": {
"@context": {
"schema": "http://schema.org/",
"image": "schema:image",
"hasProject": "schema:memberOf",
"name": "schema:name",
"description": "schema:description",
"url": "schema:url",
"abstract": "schema:abstract",
"articleBody": "schema:articleBody",
"created": {
"@reverse": "schema:creator"
},
"KNOWS": "https://data.knows.idlab.ugent.be/person/office/#"
}
},
"comunicaConfig": {
"sources": [
"https://expired.badssl.com/",
]
},
"cache": true,
"parameters": {}
};
let error;
try {
await handler.handle(graphQLLDInfo, {}, {});
} catch (e) {
error = e;
}
expect(error).to.not.equal(undefined);
expect(error.message).to.include('Comunica: fetch failed');
expect(error.message).to.include('data source: https://expired.badssl.com/');
});
it('Query data source with self-signed certificate', async () => {
const handler = new GraphQLLDHandler(undefined, undefined, () => {});
const graphQLLDInfo = {
"queries": {
"projects": {
"query": "{ id(_:KNOWS) hasProject { id @single name @single description @single image @single url @single @optional }}"
},
},
"context": {
"@context": {
"schema": "http://schema.org/",
"image": "schema:image",
"hasProject": "schema:memberOf",
"name": "schema:name",
"description": "schema:description",
"url": "schema:url",
"abstract": "schema:abstract",
"articleBody": "schema:articleBody",
"created": {
"@reverse": "schema:creator"
},
"KNOWS": "https://data.knows.idlab.ugent.be/person/office/#"
}
},
"comunicaConfig": {
"sources": [
"https://self-signed.badssl.com/",
]
},
"cache": true,
"parameters": {}
};
let error;
try {
await handler.handle(graphQLLDInfo, {}, {});
} catch (e) {
error = e;
}
expect(error).to.not.equal(undefined);
expect(error.message).to.include('Comunica: fetch failed');
expect(error.message).to.include('data source: https://self-signed.badssl.com/');
});
it.skip('Query data source with revoked certificate', async () => {
const handler = new GraphQLLDHandler(undefined, undefined, () => {});
const graphQLLDInfo = {
"queries": {
"projects": {
"query": "{ id(_:KNOWS) hasProject { id @single name @single description @single image @single url @single @optional }}"
},
},
"context": {
"@context": {
"schema": "http://schema.org/",
"image": "schema:image",
"hasProject": "schema:memberOf",
"name": "schema:name",
"description": "schema:description",
"url": "schema:url",
"abstract": "schema:abstract",
"articleBody": "schema:articleBody",
"created": {
"@reverse": "schema:creator"
},
"KNOWS": "https://data.knows.idlab.ugent.be/person/office/#"
}
},
"comunicaConfig": {
"sources": [
"https://revoked.badssl.com/",
]
},
"cache": true,
"parameters": {}
};
let error;
try {
await handler.handle(graphQLLDInfo, {}, {});
} catch (e) {
error = e;
}
expect(error).to.not.equal(undefined);
expect(error.message).to.include('Code: CERT_HAS_EXPIRED');
expect(error.message).to.include('data source: https://revoked.badssl.com/');
});
/**
* TODO: This test succeeds but it keeps running afterwards.
*/
it.skip('Query data sources that includes query', async () => {
this.timeout(60000)
const handler = new GraphQLLDHandler(null, './test/resources/pipe-modules');
const graphQLLDInfo = {
"queries": {
"employees": {
"query": `
{
id @single
employer(_:team)
name @single
}`
},
},
"context": {
"@context": {
"schema": "http://schema.org/",
"foaf": "http://xmlns.com/foaf/0.1/",
"name": "foaf:name",
"employer": {"@reverse": "schema:employee"},
"knows": "https://data.knows.idlab.ugent.be/person/office/#",
"team": "knows:team"
}
},
"comunicaConfig": {
"sources": [
"https://data.knows.idlab.ugent.be/person/office/employees.ttl",
{
'graphql-query': ` {
id @single
employer(_:team)
}`,
'json-ld-context': ` {
"schema": "http://schema.org/",
"employer": {"@reverse": "schema:employee"},
"knows": "https://data.knows.idlab.ugent.be/person/office/#",
"team": "knows:team"
}`,
datasources: {
sources: ['https://data.knows.idlab.ugent.be/person/office/employees.ttl']
},
postprocessing: {
getIds: {
source: 'get-ids.js'
}
}
}
]
},
"cache": false,
"parameters": {}
};
const result = await handler.handle(graphQLLDInfo, {}, {});
result.employees.length.should.be.greaterThan(10);
});
});