Skip to content

1764 allow interfaces to implement other interfaces - #1884

Merged
andimarek merged 22 commits into
graphql-java:masterfrom
felipe-gdr:1764_allow-interfaces-to-implement-other-interfaces
May 24, 2020
Merged

andimarek merged 22 commits into
graphql-java:masterfrom
felipe-gdr:1764_allow-interfaces-to-implement-other-interfaces

Conversation

@felipe-gdr

@felipe-gdr felipe-gdr commented Apr 27, 2020 •

Copy link
Copy Markdown
Member

Allows interfaces to implement other interfaces, as described in the draft spec.

fixes: #1764

@felipe-gdr

felipe-gdr commented Apr 28, 2020 •

Copy link
Copy Markdown
Member Author
  • use error list in asserts instead of regex for more fine tuned checks

@felipe-gdr
felipe-gdr marked this pull request as draft April 28, 2020 23:05
@felipe-gdr
felipe-gdr force-pushed the 1764_allow-interfaces-to-implement-other-interfaces branch from 84a5b99 to ea740ef Compare April 28, 2020 23:07
@felipe-gdr
felipe-gdr force-pushed the 1764_allow-interfaces-to-implement-other-interfaces branch from 0a95b71 to 3dfdab1 Compare April 28, 2020 23:20
@andimarek

Copy link
Copy Markdown
Member

thanks a lot @felipe-gdr. Please have a look at the Graphql JS PR as sanity check: graphql/graphql-js#2084

@felipe-gdr

felipe-gdr commented Apr 29, 2020 •

Copy link
Copy Markdown
Member Author
  • Interfaces declared in interface extensions are not being enforced by the transitive validation

@felipe-gdr

Copy link
Copy Markdown
Member Author

thanks a lot @felipe-gdr. Please have a look at the Graphql JS PR as sanity check: graphql/graphql-js#2084

Yep, good idea.

@felipe-gdr

Copy link
Copy Markdown
Member Author

Looking at the graphq-js implementation was a good idea. I've simplified the validation code quite a bit. I didn't have to use recursion to find circular dependencies since the list of implemented interfaces is flattened at each interface type. 👍

@felipe-gdr
felipe-gdr force-pushed the 1764_allow-interfaces-to-implement-other-interfaces branch from 311ef32 to 4015f0d Compare May 17, 2020 09:52
@felipe-gdr
felipe-gdr marked this pull request as ready for review May 17, 2020 09:56
assertErrorMessage(error, "The interface extension type 'BaseInterface' [@n:n] has tried to redefine field 'fieldB' arguments defined via interface 'InterfaceType' [@n:n] from 'arg3:Int' to 'arg3:String")
}

def assertErrorMessage(SchemaProblem error, expectedMessage) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice one! We should do this in other tests.

Can you please rewrite all our tests to this pattern please! ;)

#
extend type BaseType implements Interface1 {
extraField1 : String
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why does this need to be removed? Is it no longer valid or just not longer relevant and tested in the other places

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is actually invalid now.
I've added a new validation rule which prevents extensions from implementing an interface which has already been implemented from a base type, or other extensions for the same type.
This will make the graphql-java validation more aligned with graphql-js

field21: String
}

extend type Type1 implements Interface2 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My comment below is covered by this test

@bbakerman

Copy link
Copy Markdown
Member

Wow this is impressive code - neat and conscice but readable

@andimarek andimarek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

great work. Did a first review. See my comments.

We also should have a "integration test" for the introspection result, what do you think?

interfaceTypeDefinition.description(toDescription(input));
if (input.containsKey("interfaces") && input.get("interfaces") != null) {
interfaceTypeDefinition.implementz(
((List<Map<String, Object>>) input.get("interfaces")).stream()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please use our map static function instead of streams directly

* @param <T>
*/
@PublicApi
public interface ImplementingTypeDefinition<T extends ImplementingTypeDefinition> extends TypeDefinition<T> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why the recursive type definition here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep, not needed. <T extends TypeDefinition> should be enough.

@PublicApi
public interface ImplementingTypeDefinition<T extends ImplementingTypeDefinition> extends TypeDefinition<T> {

List<Type> getImplements();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

not great name, but not in scope to change it here.

private List<Type> getImplementz(GraphqlParser.ImplementsInterfacesContext implementsInterfacesContext) {
List<Type> implementz = new ArrayList<>();
while (implementsInterfacesContext != null) {
List<TypeName> typeNames = implementsInterfacesContext.typeName().stream().map(this::createTypeName).collect(toList());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please use our map util function.

private void checkForMissingTypes(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry) {
// type extensions
List<ObjectTypeExtensionDefinition> typeExtensions = typeRegistry.objectTypeExtensions().values().stream().flatMap(Collection::stream).collect(Collectors.toList());
List<ObjectTypeExtensionDefinition> typeExtensions = typeRegistry.objectTypeExtensions().values().stream().flatMap(Collection::stream).collect(toList());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please use the util function

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

FpKit#flatList applies to List<List<>> and we have a Collection<List<>> here.
Should we create a more generic flatten method in FpKit?

*/
public List<ObjectTypeDefinition> getImplementationsOf(InterfaceTypeDefinition targetInterface) {
List<ObjectTypeDefinition> objectTypeDefinitions = getTypes(ObjectTypeDefinition.class);
public List<ImplementingTypeDefinition> getImplementationsOf(InterfaceTypeDefinition targetInterface) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is a breaking change. Should we consider adding another one and keeping this one for use object implementations?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep, just noticed that this is public API. I'll create a new method.

@andimarek andimarek added this to the 15.0 milestone May 19, 2020
- use FpKit
- fix Generic definition in ImplementingTypeDefinition
- don't break public API in TypeDefinitionRegistry
@felipe-gdr
felipe-gdr force-pushed the 1764_allow-interfaces-to-implement-other-interfaces branch from 1339a86 to 04cfbe7 Compare May 20, 2020 10:47
@andimarek

Copy link
Copy Markdown
Member

We need to add a verification for programmatically created GraphQLTypes that they declare all interfaces transitively.

Also we need to look at AstPrinter if it prints correctly and add a test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow interfaces to implement other interfaces

3 participants