Skip to content

Commit fded91e

Browse files
uroslatesardatan
andauthored
feat: add default values handling to buildOperationNodeForField (#5269)
* feat: add default values handling to buildOperationNodeForField * Fix * Changeset * Fix v15 --------- Co-authored-by: Arda TANRIKULU <[email protected]>
1 parent fd105f4 commit fded91e

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

.changeset/gold-eggs-stick.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
'@graphql-tools/utils': minor
3+
---
4+
5+
Add default values to the arguments
6+
7+
When the schema is like following;
8+
9+
```graphql
10+
type Query {
11+
getAllPages(currentPage: Int = 0, pageSize: Int = 10, pageType: getAllPages_pageType = ContentPage, sort: String = "asc"): PagesList
12+
}
13+
14+
enum getAllPages_pageType {
15+
ContentPage
16+
CategoryPage
17+
CatalogPage
18+
}
19+
20+
type PagesList {
21+
...
22+
}
23+
```
24+
25+
The generated operation will be like following;
26+
27+
```graphql
28+
query getAllPages_query($currentPage: Int = 0, $pageSize: Int = 10, $pageType: getAllPages_pageType = ContentPage, $sort: String = "asc") {
29+
getAllPages(currentPage: $currentPage, pageSize: $pageSize, pageType: $pageType, sort: $sort) {
30+
...
31+
}
32+
}
33+
```

packages/utils/src/build-operation-for-field.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
ArgumentNode,
3+
astFromValue,
34
getNamedType,
45
GraphQLArgument,
56
GraphQLField,
@@ -27,6 +28,7 @@ import {
2728
TypeNode,
2829
VariableDefinitionNode,
2930
} from 'graphql';
31+
import { astFromValueUntyped } from './astFromValueUntyped.js';
3032
import { getDefinedRootType, getRootTypeNames } from './rootTypes.js';
3133

3234
let operationVariables: VariableDefinitionNode[] = [];
@@ -385,6 +387,22 @@ function resolveVariable(arg: GraphQLArgument, name?: string): VariableDefinitio
385387
};
386388
}
387389

390+
let defaultValue: any | undefined;
391+
try {
392+
const returnVal = astFromValue(arg.defaultValue, arg.type) as any;
393+
if (returnVal == null) {
394+
defaultValue = undefined;
395+
} else {
396+
defaultValue = returnVal;
397+
}
398+
} catch (e) {
399+
const returnVal = astFromValueUntyped(arg.defaultValue) as any;
400+
if (returnVal == null) {
401+
defaultValue = undefined;
402+
} else {
403+
defaultValue = returnVal;
404+
}
405+
}
388406
return {
389407
kind: Kind.VARIABLE_DEFINITION,
390408
variable: {
@@ -395,6 +413,7 @@ function resolveVariable(arg: GraphQLArgument, name?: string): VariableDefinitio
395413
},
396414
},
397415
type: resolveVariableType(arg.type),
416+
defaultValue,
398417
};
399418
}
400419

packages/utils/tests/build-operation-node-for-field.spec.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,3 +664,71 @@ test('should handle array field types used for alias field names', async () => {
664664
const source = parseGraphQLSDL(`${virtualFileName}.graphql`, rawSDL);
665665
expect(source).toBeDefined();
666666
});
667+
668+
test('should work with query with default value arguments', async () => {
669+
const document = buildOperationNodeForField({
670+
schema: buildSchema(/* GraphQL */ `
671+
input PageInfoInput {
672+
offset: Int!
673+
limit: Int!
674+
}
675+
676+
type User {
677+
id: ID
678+
name: String
679+
}
680+
681+
type Query {
682+
# users(pageInfo: PageInfoInput!): [User]
683+
usersSearch(query: String, offset: Int! = 0, limit: Int! = 10): [User]
684+
}
685+
`),
686+
kind: 'query' as OperationTypeNode,
687+
field: 'usersSearch',
688+
models,
689+
ignore: [],
690+
})!;
691+
692+
expect(clean(document)).toEqual(
693+
clean(/* GraphQL */ `
694+
query usersSearch_query($query: String, $offset: Int! = 0, $limit: Int! = 10) {
695+
usersSearch(query: $query, offset: $offset, limit: $limit) {
696+
id
697+
name
698+
}
699+
}
700+
`),
701+
);
702+
});
703+
704+
test('should work with mutation with default value arguments', async () => {
705+
const document = buildOperationNodeForField({
706+
schema: buildSchema(/* GraphQL */ `
707+
type User {
708+
id: ID
709+
name: String
710+
active: Boolean!
711+
}
712+
713+
type Mutation {
714+
addUser(name: String!, active: Boolean = true): [User]
715+
}
716+
`),
717+
kind: 'mutation' as OperationTypeNode,
718+
field: 'addUser',
719+
models,
720+
ignore: [],
721+
})!;
722+
723+
expect(clean(document)).toEqual(
724+
clean(/* GraphQL */ `
725+
mutation addUser_mutation($name: String!, $active: Boolean = true) {
726+
addUser(name: $name, active: $active) {
727+
id
728+
name
729+
active
730+
}
731+
}
732+
`),
733+
);
734+
});

0 commit comments

Comments
 (0)