Skip to content

graphql-language-service: Support single-item coercion for lists #4090

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

Open
wants to merge 1 commit into
base: main
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
5 changes: 5 additions & 0 deletions .changeset/metal-mice-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-language-service': minor
---

getJSONSchemaFromGraphQLType: Support single-item coercion for lists
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,25 @@ describe('getVariablesJSONSchema', () => {
description: 'nesting a whole object!\n\nChildInputType!',
},
exampleList: {
type: ['array', 'null'],
items: {
description: 'ChildInputType',
oneOf: [
{ $ref: '#/definitions/ChildInputType' },
{ type: 'null' },
],
},
anyOf: [
{
description: 'ChildInputType',
oneOf: [
{ $ref: '#/definitions/ChildInputType' },
{ type: 'null' },
],
},
{
type: ['array', 'null'],
items: {
description: 'ChildInputType',
oneOf: [
{ $ref: '#/definitions/ChildInputType' },
{ type: 'null' },
],
},
},
],
description: 'list type with default\n\n[ChildInputType]',
default: [
{
Expand All @@ -318,12 +329,20 @@ describe('getVariablesJSONSchema', () => {
],
},
exampleScalarList: {
type: 'array',
description: '[String]!',
items: {
type: ['string', 'null'],
description: 'String',
},
anyOf: [
{
type: ['string', 'null'],
description: 'String',
},
{
type: 'array',
items: {
type: ['string', 'null'],
description: 'String',
},
},
],
default: ['something'],
},
},
Expand Down Expand Up @@ -414,15 +433,27 @@ describe('getVariablesJSONSchema', () => {
$ref: '#/definitions/ChildInputType',
},
exampleList: {
type: ['array', 'null'],
items: {
description: 'ChildInputType',
markdownDescription: '```graphql\nChildInputType\n```',
oneOf: [
{ $ref: '#/definitions/ChildInputType' },
{ type: 'null' },
],
},
anyOf: [
{
description: 'ChildInputType',
markdownDescription: '```graphql\nChildInputType\n```',
oneOf: [
{ $ref: '#/definitions/ChildInputType' },
{ type: 'null' },
],
},
{
type: ['array', 'null'],
items: {
description: 'ChildInputType',
markdownDescription: '```graphql\nChildInputType\n```',
oneOf: [
{ $ref: '#/definitions/ChildInputType' },
{ type: 'null' },
],
},
},
],
description: 'list type with default\n\n[ChildInputType]',
markdownDescription: `list type with default\n\n${mdTicks(
'[ChildInputType]',
Expand All @@ -435,14 +466,23 @@ describe('getVariablesJSONSchema', () => {
],
},
exampleScalarList: {
type: 'array',
description: '[String]!',
markdownDescription: mdTicks('[String]!'),
items: {
type: ['string', 'null'],
description: 'String',
markdownDescription: mdTicks('String'),
},
anyOf: [
{
type: ['string', 'null'],
description: 'String',
markdownDescription: mdTicks('String'),
},
{
type: 'array',
items: {
type: ['string', 'null'],
description: 'String',
markdownDescription: mdTicks('String'),
},
},
],
default: ['something'],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,16 @@ function getJSONSchemaFromGraphQLType(
definition.enum.push(null);
}
} else if (isListType(baseType)) {
if (required) {
definition.type = 'array';
} else {
definition.type = ['array', 'null'];
}

const { definition: def, definitions: defs } = getJSONSchemaFromGraphQLType(
baseType.ofType,
options,
);

definition.items = def;
// The GraphQL spec allows for passing a single list item as a list
definition.anyOf = [
def,
{ type: required ? 'array' : ['array', 'null'], items: def },
];

if (defs) {
for (const defName of Object.keys(defs)) {
Expand Down