-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql.xqy
254 lines (231 loc) · 7.24 KB
/
graphql.xqy
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
(:
: GraphQL endpoint and associated services for MarkLogic Server
:
: Turn MarkLogic into a GraphQL Server. GraphQL Queries are translated
: into SPARQL Queries and then SPARQL Results are converted into
: GraphQL JSON Result format.
:
: Version: 1.0.0
: Author: Charles Foster
:
: Copyright 2019 XML London Limited. All rights reserved.
:
: Licensed under the Apache License, Version 2.0 (the "License");
: you may not use this file except in compliance with the License.
: You may obtain a copy of the License at
:
: http://www.apache.org/licenses/LICENSE-2.0
:
: Unless required by applicable law or agreed to in writing, software
: distributed under the License is distributed on an "AS IS" BASIS,
: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
: See the License for the specific language governing permissions and
: limitations under the License.
:)
xquery version "1.0-ml";
module namespace graphql = "http://xmllondon.com/xquery/graphql";
declare namespace rest = "http://exquery.org/ns/restxq";
(: Where GraphQL Config files are stored in MarkLogic :)
declare variable $config-path := "/graphql/config/";
(: --------------------- regular /graphql endpoint --------------------- :)
declare
%rest:POST("{$query}")
%rest:path("/graphql")
%rest:query-param("config", "{$config}")
%rest:consumes("application/graphql")
function post-graphql($query as xs:string, $config as xs:string?) {
config($config) => execute($query)
};
declare
%rest:POST("{$body}")
%rest:path("/graphql")
%rest:query-param("config", "{$config}")
%rest:consumes("application/json")
function post-json($body as document-node(), $config as xs:string?) {
config($config) => execute($body/query)
};
declare
%rest:GET
%rest:path("/graphql")
%rest:query-param-1("query", "{$query}")
%rest:query-param-2("config", "{$config}")
function get($query as xs:string, $config as xs:string?) {
config($config) => execute($query)
};
(: --------------------------------------------------------------------- :)
(: --------------------- /{config}/graphql endpoint -------------------- :)
declare
%rest:POST("{$query}")
%rest:path("/{$config}/graphql")
%rest:consumes("application/graphql")
function config-post-graphql($query as xs:string, $config as xs:string) {
config($config) => execute($query)
};
declare
%rest:POST("{$body}")
%rest:path("/{$config}/graphql")
%rest:consumes("application/json")
function config-post-json($body as document-node(), $config as xs:string) {
config($config) => execute($body/query)
};
declare
%rest:GET
%rest:path("/{$config}/graphql")
%rest:query-param("query", "{$query}")
function config-get($query as xs:string, $config as xs:string) {
config($config) => execute($query)
};
(: --------------------------------------------------------------------- :)
(:~
: Converts a GraphQL Query into SPARQL
:
: @param $query the user's GraphQL Query
: @param $context information helping one convert GraphQL to SPARQL
: @return a SPARQL Query to inspect and analyse
:)
declare
%rest:path("/graphql-to-sparql")
%rest:query-param-1("query", "{$query}")
%rest:query-param-2("config", "{$config}")
function graphql-to-sparql($query as xs:string, $config as xs:string) {
(config($config) => execute($query, "show-sparql-query"))/data
};
declare function execute(
$config as document-node()?,
$query as xs:string)
{
execute($config, $query, "run")
};
declare function execute(
$config as document-node()?,
$query as xs:string,
$action as xs:string)
{
if(fn:empty($config)) then (
fn:error("graphql:GRQL0001', 'MarkLogic GraphQL config not set.")
)
else (
invoke-graphql-marklogic-module(
map:entry("query", $query) =>
map:with("config", $config) =>
map:with("action", $action)
)
)
};
(:~
: Invokes the GraphQL MarkLogic Server Side JavaScript XQuery Module
:
: Takes a map as a parameter, which may contain the following keys,
: "query" (string) a GraphQL Query
: "config" (document-node) Contains a JSON-LD Context an a GraphQL Schema
: "materializeRdfJsTerms" (boolean) If terms should be converted to their raw
: value instead of being represented as RDFJS terms
: "action" (string) set this to "show-sparql-query" to show the generated
: SPARQL Query, or "sparql-results" to show the SPARQL JSON Results,
: default behaviour being showing a GraphQL JSON Result Tree
:)
declare
%private
function invoke-graphql-marklogic-module($map as map:map) {
try
{
if(fn:empty(map:get($map, "config"))) then (
fn:error("graphql:GRQL0001', 'MarkLogic GraphQL config not set.")
) else (
object-node {
"data" :
xdmp:invoke(
"graphql-marklogic.sjs", ( xs:QName("pObject"), xdmp:to-json($map)),
map:entry("isolation", "same-statement")
),
"error" : object-node {}
}
)
} catch($ex) {
object-node {
"data" : object-node { },
"error" : $ex//*:message/string()
}
}
};
(: --------------------------------------------------------------------- :)
(: -------------------------- config endpoints ------------------------- :)
(:~
: Inserts a new GraphQL config JSON file in MarkLogic Server
:)
declare
%rest:path("/graphql/config/{$uri}")
%rest:POST("{$body}")
%rest:PUT("{$body}")
%rest:consumes("application/json", "text/json")
%xdmp:update
function insert-config($body as document-node(), $uri as xs:string) {
xdmp:document-insert($config-path || $uri, $body)
};
(:~
: Gets a GraphQL config JSON file in MarkLogic Server
:)
declare
%rest:path("/graphql/config/{$uri}")
function get-config($uri as xs:string) {
fn:doc($config-path || $uri)
};
(:~
: Delete a GraphQL config JSON file in MarkLogic Server
:)
declare
%rest:path("/graphql/config/{$uri}")
%rest:DELETE
%xdmp:update
function delete-config($uri as xs:string) {
xdmp:document-delete($config-path || $uri)
};
(:~
: Lists GraphQL config JSON files in MarkLgic Server
:)
declare
%rest:path("/graphql/config")
function list-config() {
array-node {
cts:uris((), (), cts:directory-query($config-path)) !
fn:replace(., '^.*/', '')[normalize-space()]
}
};
declare function config() as document-node() {
config('default.json')
};
declare function config($uri as xs:string) as document-node() {
fn:doc($config-path || $uri)
};
(: --------------------------------------------------------------------- :)
declare
%rest:path("/test-run")
%rest:GET
function test-function-2() {
let $config :=
document {
object-node {
"context" : object-node {
"me": "http://example.org/me",
"name": "http://example.org/name",
"creator" : "http://purl.org/dc/elements/1.1/creator",
"pname" : "http://xmlns.com/foaf/0.1/name",
"hero" : "http://example.org/hero",
"friends" : "http://example.org/friends"
},
"schema" : object-node {
"singularizeVariables" : object-node {
"" : fn:true(),
"books": fn:false(),
"books_name": fn:true()
}
},
"materializeRdfJsTerms" : fn:true()
}
}
return
invoke-graphql-marklogic-module(
map:entry("query", "{ creator { pname } }") =>
map:with("config", $config)
)
};