grantila / typeconv

Convert between JSON Schema, TypeScript, GraphQL, Open API and SureType
MIT License
424 stars 8 forks source link

JsonSchema => GraphQL: only "AllOf" schema is used #34

Closed romgere closed 1 year ago

romgere commented 1 year ago

Hi there,

I'm trying to convert some Json schema to GraphQL & got some issue when using allOf in schema.

Here is a minimal reproduction :

const baseSchema = {
  $id: 'base',
  type: 'object',
  required: ['id'],
  properties: {
    id: {
      description: 'Content uniq ID.',
      type: 'string',
    },
  },
};

const schema = {
  allOf: [baseSchema],
  $id: 'something',
  type: 'object',
  title: 'CMS Product Documentation',
  required: ['title', 'excerpt'],
  properties: {
    title: {
      type: 'string',
      description: 'Content title',
      maxLength: 30,
    },
    excerpt: {
      type: 'string',
      description: 'Short description of post.',
      minLength: 15,
      maxLength: 300,
    }
  },
};

const contentSchema = {
  definitions: {
    'Something': schema,
  },
};

import { getGraphQLWriter, getJsonSchemaReader, makeConverter } from 'typeconv';

async function main() {
  const reader = getJsonSchemaReader();
  const gqlWriter = getGraphQLWriter();
  const { convert: gQlConvert } = makeConverter(reader, gqlWriter);

  let { data } = await gQlConvert({ data: JSON.stringify(contentSchema) });
  console.log(data)
}

main()

This ouput :

type Something {
  "Content uniq ID."
  id: String!
}

title& excerpt properties of the schema are ignored/override by the base schema.

Am I wrong to think output should include those 2 properties ?

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 2.3.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

grantila commented 1 year ago

No you're right, it's just a bit unusual to have a type itself which includes an allOf also. But it's valid JSON Schema.

This is fixed in 2.3.1

In your code, you might want to add a third option to makeConverter: { mergeObjects: true } to get the result you most likely want.