graphql-inspector icon indicating copy to clipboard operation
graphql-inspector copied to clipboard

Crucial improvement for "similar" method

Open istominaver opened this issue 1 year ago • 6 comments

Issue workflow progress

Progress of the issue based on the Contributor Workflow

  • [ ] 1. The issue provides a reproduction available on GitHub, Stackblitz or CodeSandbox

    Make sure to fork this template and run pnpm generate in the terminal.

    Please make sure the Codegen and plugins version under package.json matches yours.

  • [ ] 2. A failing test has been provided
  • [ ] 3. A local solution has been provided
  • [ ] 4. A pull request is pending review

Describe the bug

I think "similar" method works somehow strange right now. Two totally different types with no common fields or similarity in the names but with the same number of fields are detects as similar, but the method does not detect 2 types which have some common/similar field names and similar type names but have different number of fields.

To Reproduce Steps to reproduce the behavior:

  1. Build schema for the next types:
type MyListingInfo {
title: String,
price: String,
image: String
}

type Listing {
id: ID!,
title: String,
currentPrice: String,
originalRetailPrice: String
image: String,
fieldX: Boolean!,
fieldY: String,
fieldZ: String
}

type Cat {
id: ID,
type: String,
test: String
}
  1. Call "similar" method with the built schema and "MyListingInfo" type as input parameters.

  2. As a result, "Cat" type is the best match for "MyListingInfo".

Expected behavior

"Listing" type is expected to be the best match for "MyListingInfo".

Environment:

  • OS: macOS Monterey v12.6
  • @graphql-inspector/core: v4.0.0
  • graphql: v16.6.0
  • NodeJS: v19.3.0

Additional context

I import "similar" method and call it from the NodeJS code, not from terminal.

import {similar} from '@graphql-inspector/core';

similar(builtSchema, typeName);

istominaver avatar Mar 13 '23 21:03 istominaver

Hey @istominaver, Thank you for this issue. Can you share the result after you run graphql-inspector similar schema.graphql?

TuvalSimha avatar Mar 15 '23 14:03 TuvalSimha

@TuvalSimha thanks for looking into the issue result of running graphql-inspector similar ./schema.graphql aligns with above example image

istominaver avatar Mar 16 '23 05:03 istominaver

Hey @istominaver, thank you! So let's talk about similar.

Here’s the code where the results of “rating similarities” is combined into an end result. And here’s how it rates types.

We take the full type:

type Cat {
 id: ID
 type: String
 test: String
}

It prints the type to SDL form, gets ride of the kind and the name, so you end up with:

 id: ID
 type: String
 test: String

and compares words, one by one. If the word is equal in the source type and the type we compare it with, we give it 1 point. Then we do some math and give it a score from 0 to 1 (basically a percentage where 0 is 0% and 1 is 100%).

TuvalSimha avatar Mar 21 '23 14:03 TuvalSimha

@istominaver You are more than welcome to clone/fork the repository, review any feature, and try to improve it or add some new things.

TuvalSimha avatar Mar 21 '23 14:03 TuvalSimha

@TuvalSimha First off, I think it can be helpful to get rid of types of fields, as they add noice when we compare strings

I tried this out and it doesn't detect MyListingInfo and Cat types to be similar, but still doesn't detect MyListingInfo and Listing to be similar

Here is a draft PR: https://github.com/kamilkisiela/graphql-inspector/pull/2418

istominaver avatar Mar 22 '23 18:03 istominaver

Also I would say we need to compare type names as well, not only field names.

As for comparison algorithm, were you considering to use something like fuzzy-matching. I wonder may it work better here?

istominaver avatar Mar 22 '23 21:03 istominaver