Skip to content

Commit

Permalink
RFC: Return type overlap validation
Browse files Browse the repository at this point in the history
This alters the "Overlapping Fields Can Be Merged" validation rule to better express return type validation issues.

The existing rule has presented some false-positives in some schema where interfaces with co-variant types are commonly used. The "same return type" check doesn't remove any ambiguity.

Instead what that "same return type" check is attempting to prevent is spreading two fragments (or inline fragments) which have fields with return types where ambiguity would be introduced in the response.

In order to curb false-positives, we later changed this rule such that if two fields were known to never apply simultaneously, then we would skip the remainder of the rule.

```
{
  ... on Person {
    foo: fullName
  }
  ... on Pet {
    foo: petName
  }
}
```

However this can introduce false-negatives!

```
{
  ... on Person {
    foo: birthday {
      bar: year
    }
  }
  ... on Business {
    foo: location {
      bar: street
    }
  }
}
```

In the above example, `data.foo.bar` could be of type `Int` or type `String`, it's ambiguous!

This differing return type breaks some client model implementations (Fragment models) in addition to just being confusing.

For example this invalid query:

```
fragment A on Type {
  field: someIntField
}

fragment B on Type {
  ...A
  field: someStringField
}
```

Might produce the following illegal Java artifacts:

```
interface A {
  int getField()
}

interface B implements A {
  string getField()
}
```
  • Loading branch information
leebyron committed Apr 7, 2016
1 parent de9074f commit d481d17
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions spec/Section 5 -- Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,37 @@ FieldsInSetCanMerge(set) :
* Let {fieldsForName} be the set of selections with a given response name in
{set} including visiting fragments and inline fragments.
* Given each pair of members {fieldA} and {fieldB} in {fieldsForName}:
* {SameResponseShape(fieldA, fieldB)} must be true.
* If the parent types of {fieldA} and {fieldB} are equal or if either is not
an Object Type:
* {fieldA} and {fieldB} must have identical field names.
* {fieldA} and {fieldB} must have identical return type.
* {fieldA} and {fieldB} must have identical sets of arguments.
* Let {mergedSet} be the result of adding the selection set of {fieldA}
and the selection set of {fieldB}.
* {FieldsInSetCanMerge(mergedSet)} must be true.

SameResponseShape(fieldA, fieldB) :
* Let {typeA} be the return type of {fieldA}.
* Let {typeB} be the return type of {fieldB}.
* If {typeA} or {typeB} is Non-Null.
* {typeA} and {typeB} must both be Non-Null.
* Let {typeA} be the nullable type of {typeA}
* Let {typeB} be the nullable type of {typeB}
* If {typeA} or {typeB} is List.
* {typeA} and {typeB} must both be List.
* Let {typeA} be the item type of {typeA}
* Let {typeB} be the item type of {typeB}
* Repeat from step 3.
* If {typeA} or {typeB} is Scalar or Enum.
* {typeA} and {typeB} must be the same type.
* Assert: {typeA} and {typeB} are both composite types.
* Let {mergedSet} be the result of adding the selection set of {fieldA} and
the selection set of {fieldB}.
* Let {fieldsForName} be the set of selections with a given response name in
{mergedSet} including visiting fragments and inline fragments.
* Given each pair of members {subfieldA} and {subfieldB} in {fieldsForName}:
* {SameResponseShape(subfieldA, subfieldB)} must be true.

** Explanatory Text **

If multiple fields selections with the same response names are encountered
Expand Down Expand Up @@ -369,16 +391,16 @@ fragment differingArgs on Dog {
}
```

The following would not merge together, however both cannot be encountered
against the same object:
The following fields would not merge together, however both cannot be
encountered against the same object, so they are safe:

```graphql
fragment safeDifferingFields on Pet {
... on Dog {
name: nickname
volume: barkVolume
}
... on Cat {
name
volume: meowVolume
}
}

Expand All @@ -392,6 +414,21 @@ fragment safeDifferingArgs on Pet {
}
```

However, the field responses must be shapes which can be merged. For example,
scalar values must not differ. In this example, `someValue` might be a `String`
or an `Int`:

```!graphql
fragment conflictingDifferingResponses on Pet {
... on Dog {
someValue: nickname
}
... on Cat {
someValue: meowVolume
}
}
```


### Leaf Field Selections

Expand Down

1 comment on commit d481d17

@leebyron
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the unit tests in graphql/graphql-js#346 for more valid and invalid examples.

Please sign in to comment.