Skip to content

Add 'diffSchema' utility function #3224

Closed
@IvanGoncharov

Description

@IvanGoncharov

Extracted from #1127 see #1127 (comment)

We shoud add diffSchemas function. That returns something like this:

type SchemaDifference = {|
  added: {|
    queryType?: GraphQLObjectType,
    // ...
    types: Array<GraphQLTypes>,
    directives: Array<GraphQLDirectives>
  |},
  changed: {|
    queryType?: ValueDifference<GraphQLType>,
    // ...
    types: Array<TypeDifference>,
    directives: Array<DirectivesDifference>
  |}
  // ...
|};

type ValueDifference<T> = { old: T, new: T };

type TypeDifference = ScalarTypeDifference | ObjectTypeDifference; /* ... */

type ObjectTypeDifference = {|
  name: string,
  added: {|
    description: string,
    fields: Array<GraphQLField>
    // ...
  |},
  changed: {|
    description: ValueDifference<string>,
    fields: Array<FieldDifference>
    // ...
  |}
  // ...
|};

It will allow to implement your particular check as:

const diff = diffSchema(oldSchema, newSchema);

for (const newType of diff.added.types) {
  if (newType.description) {
    console.log("Description of a new type ...");
  }
  newType.getFields().forEach(reportNewField);
}

for (const typeDiff of diff.changed.types) {
  if (typeDiff.added.description) {
    console.log("Added type description ...");
  }
  typeDiff.added.fields.forEach(reportNewField);
  if (typeDiff.changed.description) {
    console.log("Changed type description ...");
  }
  for (const fieldDiff of typeDiff.changed.field) {
    if (fieldDiff.added.description) {
      console.log("Added field description ...");
      // ...
    }
  }
}

function reportNewField(field) {
  if (field.description) {
    // ...
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions