11import mergeHeaders from './lib/mergeHeaders.js'
2+ import mergeParameters from './lib/mergeParameters.js'
23
34/**
45 * A query implementation that prepares URLs and headers for SPARQL queries and returns the raw fetch response.
@@ -17,56 +18,83 @@ class RawQuery {
1718 *
1819 * @param {string } query ASK query
1920 * @param {Object } [options]
21+ * @param {string[] } [options.defaultGraph] default graph URI parameter
2022 * @param {Headers } [options.headers] additional request headers
23+ * @param {string[] } [options.namedGraph] named graph URI parameter
2124 * @param {'get'|'postUrlencoded'|'postDirect' } [options.operation='get'] SPARQL Protocol operation
25+ * @param {Object } [options.parameters] additional request parameters
2226 * @return {Promise<Response> }
2327 */
24- async ask ( query , { headers, operation = 'get' } = { } ) {
28+ async ask ( query , { defaultGraph , headers, namedGraph , operation = 'get' , parameters } = { } ) {
2529 headers = mergeHeaders ( headers )
2630
2731 if ( ! headers . has ( 'accept' ) ) {
2832 headers . set ( 'accept' , 'application/sparql-results+json' )
2933 }
3034
31- return this . client [ operation ] ( query , { headers } )
35+ parameters = mergeParameters (
36+ parameters ,
37+ { 'default-graph-uri' : defaultGraph } ,
38+ { 'named-graph-uri' : namedGraph }
39+ )
40+
41+ return this . client [ operation ] ( query , { headers, parameters } )
3242 }
3343
3444 /**
3545 * Sends a request for a CONSTRUCT or DESCRIBE query
3646 *
3747 * @param {string } query CONSTRUCT or DESCRIBE query
3848 * @param {Object } [options]
49+ * @param {string[] } [options.defaultGraph] default graph URI parameter
3950 * @param {Headers } [options.headers] additional request headers
51+ * @param {string[] } [options.namedGraph] named graph URI parameter
4052 * @param {'get'|'postUrlencoded'|'postDirect' } [options.operation='get'] SPARQL Protocol operation
53+ * @param {Object } [options.parameters] additional request parameters
4154 * @return {Promise<Response> }
4255 */
43- async construct ( query , { headers, operation = 'get' } = { } ) {
56+ async construct ( query , { defaultGraph , headers, namedGraph , operation = 'get' , parameters = { } } = { } ) {
4457 headers = mergeHeaders ( headers )
4558
4659 if ( ! headers . has ( 'accept' ) ) {
47- headers . set ( 'accept' , 'application/n-triples' )
60+ headers . set ( 'accept' , 'application/n-triples, text/turtle ' )
4861 }
4962
50- return this . client [ operation ] ( query , { headers } )
63+ parameters = mergeParameters (
64+ parameters ,
65+ { 'default-graph-uri' : defaultGraph } ,
66+ { 'named-graph-uri' : namedGraph }
67+ )
68+
69+ return this . client [ operation ] ( query , { headers, operation, parameters } )
5170 }
5271
5372 /**
5473 * Sends a request for a SELECT query
5574 *
5675 * @param {string } query SELECT query
5776 * @param {Object } [options]
77+ * @param {string[] } [options.defaultGraph] default graph URI parameter
5878 * @param {Headers } [options.headers] additional request headers
79+ * @param {string[] } [options.namedGraph] named graph URI parameter
5980 * @param {'get'|'postUrlencoded'|'postDirect' } [options.operation='get'] SPARQL Protocol operation
81+ * @param {Object } [options.parameters] additional request parameters
6082 * @return {Promise<Response> }
6183 */
62- async select ( query , { headers, operation = 'get' } = { } ) {
84+ async select ( query , { defaultGraph , headers, namedGraph , operation = 'get' , parameters = { } } = { } ) {
6385 headers = mergeHeaders ( headers )
6486
6587 if ( ! headers . has ( 'accept' ) ) {
6688 headers . set ( 'accept' , 'application/sparql-results+json' )
6789 }
6890
69- return this . client [ operation ] ( query , { headers } )
91+ parameters = mergeParameters (
92+ parameters ,
93+ { 'default-graph-uri' : defaultGraph } ,
94+ { 'named-graph-uri' : namedGraph }
95+ )
96+
97+ return this . client [ operation ] ( query , { headers, parameters } )
7098 }
7199
72100 /**
@@ -76,16 +104,25 @@ class RawQuery {
76104 * @param {Object } [options]
77105 * @param {Headers } [options.headers] additional request headers
78106 * @param {'get'|'postUrlencoded'|'postDirect' } [options.operation='postUrlencoded'] SPARQL Protocol operation
107+ * @param {Object } [options.parameters] additional request parameters
108+ * @param {string[] } [options.usingGraph] using graph URI parameter
109+ * @param {string[] } [options.usingNamedGraph] using named graph URI parameter
79110 * @return {Promise<Response> }
80111 */
81- async update ( query , { headers, operation = 'postUrlencoded' } = { } ) {
112+ async update ( query , { headers, operation = 'postUrlencoded' , parameters , usingGraph , usingNamedGraph } = { } ) {
82113 headers = mergeHeaders ( headers )
83114
84115 if ( ! headers . has ( 'accept' ) ) {
85116 headers . set ( 'accept' , '*/*' )
86117 }
87118
88- return this . client [ operation ] ( query , { headers, update : true } )
119+ parameters = mergeParameters (
120+ parameters ,
121+ { 'using-graph-uri' : usingGraph } ,
122+ { 'using-named-graph-uri' : usingNamedGraph }
123+ )
124+
125+ return this . client [ operation ] ( query , { headers, parameters, update : true } )
89126 }
90127}
91128
0 commit comments