Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional query string usage in resolvers #453

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/openapi-to-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"jsonpointer": "^5.0.0",
"oas-validator": "^5.0.2",
"pluralize": "^8.0.0",
"query-string": "^7.1.1",
"swagger2openapi": "^7.0.2",
"tslib": "^2.3.0",
"url-join": "4.0.1",
Expand All @@ -105,7 +106,7 @@
"devDependencies": {
"@types/deep-equal": "^1.0.1",
"@types/graphql": "^14.0.3",
"@types/graphql-upload": "^8.0.7",
"@types/graphql-upload": "8.0.7",
"@types/jest": "^26.0.14",
"@types/node": "^16.3.3",
"@types/url-join": "^4.0.1",
Expand Down
13 changes: 12 additions & 1 deletion packages/openapi-to-graphql/src/resolver_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import formurlencoded from 'form-urlencoded'
import { PubSub } from 'graphql-subscriptions'
import urljoin from 'url-join'
import FormData from 'form-data'
import * as querystring from 'query-string';

const pubsub = new PubSub()

Expand Down Expand Up @@ -722,7 +723,11 @@ export function getResolver<TSource, TContext, TArgs>({

resolveData.usedRequestOptions = options
resolveData.usedStatusCode = operation.statusCode
setSearchParamsFromObj(url, qs, [])
if (requestOptions.useQueryString) {
setSearchFromObj(url, qs)
} else {
setSearchParamsFromObj(url, qs, [])
}
resolveData.url = url.toString().replace(url.search, '')

// Make the call
Expand Down Expand Up @@ -1411,6 +1416,12 @@ export function extractRequestDataFromArgs<TSource, TContext, TArgs>(
return { path, qs, headers }
}

// This can be extended in the future to take an optional object which controls
// the stringify options as listed here https://github.com/sindresorhus/query-string#stringifyobject-options
const setSearchFromObj = (url: URL, obj: any) => {
url.search = querystring.stringify(obj);
}

const setSearchParamsFromObj = (url: URL, obj: any, path: string[]) => {
for (const key in obj) {
const val = obj[key]
Expand Down
3 changes: 3 additions & 0 deletions packages/openapi-to-graphql/src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ export type RequestHeadersFunction<TSource, TContext, TArgs> = (
* function.
*
* Based on: https://github.com/request/request#requestoptions-callback
*
* useQueryString is an optional parameter to use a the query-string library
*/
export type RequestOptions<TSource, TContext, TArgs> = Omit<
RequestInit,
'headers'
> & {
headers?: HeadersInit | RequestHeadersFunction<TSource, TContext, TArgs>
qs?: Record<string, string>
useQueryString?: boolean
}

/**
Expand Down
38 changes: 38 additions & 0 deletions packages/openapi-to-graphql/test/example_api6.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,41 @@ test('Handle no response schema', () => {
})
})
})

/**
* GET /arrayInQueryParameters with and without the useQueryString requestOption
*
* The default behaviour is to add indexed parameters, i.e. a[0]=x&a[1]=y.
* The querystring behaviour (which can be made configurable in the future) is a=x&a=y
*/
test('Optionally use queryString', () => {
const query = `{
arrayInQueryParameters(ids: ["a", "b"])
}`

// Use default settings
const promise = graphql(createdSchema, query).then((result) => {
expect(result.data).toEqual({
arrayInQueryParameters: encodeURI('ids[0]=a&ids[1]=b')
})
})

// Set useQueryString to true
const options: Options<any, any, any> = {
requestOptions: {
useQueryString: true
}
}

const promise2 = openAPIToGraphQL
.createGraphQLSchema(oas, options)
.then(({ schema, report }) => {
return graphql(schema, query).then((result) => {
expect(result.data).toEqual({
arrayInQueryParameters: encodeURI('ids=a&ids=b')
})
})
})

return Promise.all([promise, promise2])
})
7 changes: 7 additions & 0 deletions packages/openapi-to-graphql/test/example_api6_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ function startServer(PORT) {
}
)

app.get(
'/api/arrayInQueryParameters',
(req, res) => {
res.send(req.originalUrl.split('?')[1])
}
)

function stringifyRussianDolls(russianDoll) {
if (!typeof russianDoll.name === 'string') {
return ''
Expand Down
30 changes: 30 additions & 0 deletions packages/openapi-to-graphql/test/fixtures/example_oas6.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,36 @@
}
}
}
},
"/arrayInQueryParameters": {
"get": {
"description": "Takes an array as GET query parameter",
"responses": {
"200": {
"description": "Success",
"content": {
"text/html": {
"schema": {
"type": "string"
}
}
}
}
},
"parameters": [
{
"name": "ids",
"in": "query",
"required": false,
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}
]
}
}
},
"components": {
Expand Down
Loading