File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,39 @@ export const GadgetConnectionSharedSuite = (queryExtra = "") => {
35
35
expect ( ( connection as any ) . requestPolicy ) . toEqual ( "network-only" ) ;
36
36
} ) ;
37
37
38
+ it ( "should allow connecting to an endpoint with existing query params" , async ( ) => {
39
+ nock ( "https://someapp.gadget.app" )
40
+ . post ( "/api/graphql?foo=bar&operation=meta" , { query : `{\n meta {\n appName\n${ queryExtra } }\n}` , variables : { } } )
41
+ . reply ( 200 , {
42
+ data : {
43
+ meta : {
44
+ appName : "some app" ,
45
+ } ,
46
+ } ,
47
+ } ) ;
48
+
49
+ const connection = new GadgetConnection ( {
50
+ endpoint : "https://someapp.gadget.app/api/graphql?foo=bar" ,
51
+ authenticationMode : { anonymous : true } ,
52
+ } ) ;
53
+
54
+ const result = await connection . currentClient
55
+ . query (
56
+ gql `
57
+ {
58
+ meta {
59
+ appName
60
+ }
61
+ }
62
+ ` ,
63
+ { }
64
+ )
65
+ . toPromise ( ) ;
66
+
67
+ expect ( result . error ) . toBeUndefined ( ) ;
68
+ expect ( result . data ) . toEqual ( { meta : { appName : "some app" } } ) ;
69
+ } ) ;
70
+
38
71
describe ( "authorization" , ( ) => {
39
72
it ( "should allow connecting with anonymous authentication" , async ( ) => {
40
73
nock ( "https://someapp.gadget.app" )
Original file line number Diff line number Diff line change @@ -2,8 +2,15 @@ import { mapExchange } from "@urql/core";
2
2
3
3
export const urlParamExchange = mapExchange ( {
4
4
onOperation : ( operation ) => {
5
- if ( operation . context . url && operation . context . operationName && ! operation . context . url . includes ( "?" ) ) {
6
- operation . context . url += `?operation=${ operation . context . operationName } ` ;
5
+ if ( operation . context . url && operation . context . operationName ) {
6
+ try {
7
+ const [ start , params ] = operation . context . url . split ( "?" ) ;
8
+ const paramsObj = new URLSearchParams ( params ) ;
9
+ paramsObj . set ( "operation" , operation . context . operationName ) ;
10
+ operation . context . url = `${ start } ?${ paramsObj . toString ( ) } ` ;
11
+ } catch ( error ) {
12
+ // not able to parse URL params, just don't add this optional param and let the rest of the system react to the invalid URL
13
+ }
7
14
}
8
15
} ,
9
16
} ) ;
You can’t perform that action at this time.
0 commit comments