Skip to content

Commit b685cef

Browse files
uroslatesardatan
authored andcommitted
feat: add default values handling to buildOperationNodeForField
1 parent fd105f4 commit b685cef

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ function resolveVariable(arg: GraphQLArgument, name?: string): VariableDefinitio
395395
},
396396
},
397397
type: resolveVariableType(arg.type),
398+
defaultValue: (arg.astNode || {}).defaultValue,
398399
};
399400
}
400401

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)