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

feat: add default values handling to buildOperationNodeForField #5269

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/utils/src/build-operation-for-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ function resolveVariable(arg: GraphQLArgument, name?: string): VariableDefinitio
},
},
type: resolveVariableType(arg.type),
defaultValue: (arg.astNode || {}).defaultValue,
};
}

Expand Down
68 changes: 68 additions & 0 deletions packages/utils/tests/build-operation-node-for-field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,71 @@ test('should handle array field types used for alias field names', async () => {
const source = parseGraphQLSDL(`${virtualFileName}.graphql`, rawSDL);
expect(source).toBeDefined();
});

test('should work with query with default value arguments', async () => {
const document = buildOperationNodeForField({
schema: buildSchema(/* GraphQL */ `
input PageInfoInput {
offset: Int!
limit: Int!
}

type User {
id: ID
name: String
}

type Query {
# users(pageInfo: PageInfoInput!): [User]
usersSearch(query: String, offset: Int! = 0, limit: Int! = 10): [User]
}
`),
kind: 'query' as OperationTypeNode,
field: 'usersSearch',
models,
ignore: [],
})!;

expect(clean(document)).toEqual(
clean(/* GraphQL */ `
query usersSearch_query($query: String, $offset: Int! = 0, $limit: Int! = 10) {
usersSearch(query: $query, offset: $offset, limit: $limit) {
id
name
}
}
`)
);
});

test('should work with mutation with default value arguments', async () => {
const document = buildOperationNodeForField({
schema: buildSchema(/* GraphQL */ `
type User {
id: ID
name: String
active: Boolean!
}

type Mutation {
addUser(name: String!, active: Boolean = true): [User]
}
`),
kind: 'mutation' as OperationTypeNode,
field: 'addUser',
models,
ignore: [],
})!;

expect(clean(document)).toEqual(
clean(/* GraphQL */ `
mutation addUser_mutation($name: String!, $active: Boolean = true) {
addUser(name: $name, active: $active) {
id
name
active
}
}
`)
);
});