Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/antlr/GraphqlSDL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ argumentsDefinition : '(' inputValueDefinition+ ')';

inputValueDefinition : description? name ':' type defaultValue? directives?;

interfaceTypeDefinition : description? INTERFACE name directives? fieldsDefinition?;
interfaceTypeDefinition : description? INTERFACE name implementsInterfaces? directives? fieldsDefinition?;

interfaceTypeExtensionDefinition :
EXTEND INTERFACE name directives? extensionFieldsDefinition |
EXTEND INTERFACE name directives emptyParentheses?
EXTEND INTERFACE name implementsInterfaces? directives? extensionFieldsDefinition |
EXTEND INTERFACE name implementsInterfaces? directives emptyParentheses? |
EXTEND INTERFACE name implementsInterfaces
;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import graphql.language.OperationTypeDefinition;
import graphql.language.ScalarTypeDefinition;
import graphql.language.SchemaDefinition;
import graphql.language.SourceLocation;
import graphql.language.StringValue;
import graphql.language.Type;
import graphql.language.TypeDefinition;
import graphql.language.TypeName;
import graphql.language.UnionTypeDefinition;
import graphql.language.Value;
import graphql.schema.idl.ScalarInfo;
import graphql.util.FpKit;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -185,6 +185,14 @@ InterfaceTypeDefinition createInterface(Map<String, Object> input) {

InterfaceTypeDefinition.Builder interfaceTypeDefinition = InterfaceTypeDefinition.newInterfaceTypeDefinition().name((String) input.get("name"));
interfaceTypeDefinition.description(toDescription(input));
if (input.containsKey("interfaces") && input.get("interfaces") != null) {
interfaceTypeDefinition.implementz(
FpKit.map(
(List<Map<String, Object>>) input.get("interfaces"),
this::createTypeIndirection
)
);
}
List<Map<String, Object>> fields = (List<Map<String, Object>>) input.get("fields");
interfaceTypeDefinition.definitions(createFields(fields));

Expand Down
1 change: 1 addition & 0 deletions src/main/java/graphql/language/AstSorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public TraversalControl visitObjectTypeDefinition(ObjectTypeDefinition node, Tra
public TraversalControl visitInterfaceTypeDefinition(InterfaceTypeDefinition node, TraverserContext<Node> context) {
InterfaceTypeDefinition changedNode = node.transform(builder -> {
builder.directives(sort(node.getDirectives(), comparing(Directive::getName)));
builder.implementz(sort(node.getImplements(), comparingTypes()));
builder.definitions(sort(node.getFieldDefinitions(), comparing(FieldDefinition::getName)));
});
return changeNode(context, changedNode);
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/graphql/language/ImplementingTypeDefinition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package graphql.language;


import graphql.PublicApi;

import java.util.List;

/**
* A {@link TypeDefinition} that might implement interfaces
*
* @param <T>
*/
@PublicApi
public interface ImplementingTypeDefinition<T extends TypeDefinition> extends TypeDefinition<T> {

List<Type> getImplements();

List<FieldDefinition> getFieldDefinitions();
}
32 changes: 30 additions & 2 deletions src/main/java/graphql/language/InterfaceTypeDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@
import static java.util.Collections.emptyMap;

@PublicApi
public class InterfaceTypeDefinition extends AbstractDescribedNode<InterfaceTypeDefinition> implements TypeDefinition<InterfaceTypeDefinition>, DirectivesContainer<InterfaceTypeDefinition>, NamedNode<InterfaceTypeDefinition> {
public class InterfaceTypeDefinition extends AbstractDescribedNode<InterfaceTypeDefinition> implements ImplementingTypeDefinition<InterfaceTypeDefinition>, DirectivesContainer<InterfaceTypeDefinition>, NamedNode<InterfaceTypeDefinition> {

private final String name;
private final List<Type> implementz;
private final List<FieldDefinition> definitions;
private final List<Directive> directives;

public static final String CHILD_IMPLEMENTZ = "implementz";
public static final String CHILD_DEFINITIONS = "definitions";
public static final String CHILD_DIRECTIVES = "directives";

@Internal
protected InterfaceTypeDefinition(String name,
List<Type> implementz,
List<FieldDefinition> definitions,
List<Directive> directives,
Description description,
Expand All @@ -37,6 +40,7 @@ protected InterfaceTypeDefinition(String name,
Map<String, String> additionalData) {
super(sourceLocation, comments, ignoredChars, additionalData, description);
this.name = name;
this.implementz = implementz;
this.definitions = definitions;
this.directives = directives;
}
Expand All @@ -47,9 +51,15 @@ protected InterfaceTypeDefinition(String name,
* @param name of the interface
*/
public InterfaceTypeDefinition(String name) {
this(name, new ArrayList<>(), new ArrayList<>(), null, null, new ArrayList<>(), IgnoredChars.EMPTY, emptyMap());
this(name, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), null, null, new ArrayList<>(), IgnoredChars.EMPTY, emptyMap());
}

@Override
public List<Type> getImplements() {
return new ArrayList<>(implementz);
}

@Override
public List<FieldDefinition> getFieldDefinitions() {
return new ArrayList<>(definitions);
}
Expand All @@ -67,6 +77,7 @@ public String getName() {
@Override
public List<Node> getChildren() {
List<Node> result = new ArrayList<>();
result.addAll(implementz);
result.addAll(definitions);
result.addAll(directives);
return result;
Expand All @@ -75,6 +86,7 @@ public List<Node> getChildren() {
@Override
public NodeChildrenContainer getNamedChildren() {
return newNodeChildrenContainer()
.children(CHILD_IMPLEMENTZ, implementz)
.children(CHILD_DEFINITIONS, definitions)
.children(CHILD_DIRECTIVES, directives)
.build();
Expand All @@ -83,6 +95,7 @@ public NodeChildrenContainer getNamedChildren() {
@Override
public InterfaceTypeDefinition withNewChildren(NodeChildrenContainer newChildren) {
return transform(builder -> builder
.implementz(newChildren.getChildren(CHILD_IMPLEMENTZ))
.definitions(newChildren.getChildren(CHILD_DEFINITIONS))
.directives(newChildren.getChildren(CHILD_DIRECTIVES))
);
Expand All @@ -105,6 +118,7 @@ public boolean isEqualTo(Node o) {
@Override
public InterfaceTypeDefinition deepCopy() {
return new InterfaceTypeDefinition(name,
deepCopy(implementz),
deepCopy(definitions),
deepCopy(directives),
description,
Expand All @@ -118,6 +132,7 @@ public InterfaceTypeDefinition deepCopy() {
public String toString() {
return "InterfaceTypeDefinition{" +
"name='" + name + '\'' +
", implements=" + implementz +
", fieldDefinitions=" + definitions +
", directives=" + directives +
'}';
Expand All @@ -144,6 +159,7 @@ public static final class Builder implements NodeBuilder {
private List<Comment> comments = new ArrayList<>();
private String name;
private Description description;
private List<Type> implementz = new ArrayList<>();
private List<FieldDefinition> definitions = new ArrayList<>();
private List<Directive> directives = new ArrayList<>();
private IgnoredChars ignoredChars = IgnoredChars.EMPTY;
Expand Down Expand Up @@ -185,6 +201,17 @@ public Builder description(Description description) {
return this;
}

public Builder implementz(List<Type> implementz) {
this.implementz = implementz;
return this;
}

public Builder implementz(Type implement) {
this.implementz.add(implement);
return this;
}


public Builder definitions(List<FieldDefinition> definitions) {
this.definitions = definitions;
return this;
Expand Down Expand Up @@ -223,6 +250,7 @@ public Builder additionalData(String key, String value) {

public InterfaceTypeDefinition build() {
return new InterfaceTypeDefinition(name,
implementz,
definitions,
directives,
description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ public class InterfaceTypeExtensionDefinition extends InterfaceTypeDefinition {

@Internal
protected InterfaceTypeExtensionDefinition(String name,
List<Type> implementz,
List<FieldDefinition> definitions,
List<Directive> directives,
Description description,
SourceLocation sourceLocation,
List<Comment> comments,
IgnoredChars ignoredChars,
Map<String, String> additionalData) {
super(name, definitions, directives, description, sourceLocation, comments, ignoredChars, additionalData);
super(name, implementz, definitions, directives, description, sourceLocation, comments, ignoredChars, additionalData);
}

@Override
public InterfaceTypeExtensionDefinition deepCopy() {
return new InterfaceTypeExtensionDefinition(getName(),
getImplements(),
deepCopy(getFieldDefinitions()),
deepCopy(getDirectives()),
getDescription(),
Expand Down Expand Up @@ -63,6 +65,7 @@ public static final class Builder implements NodeBuilder {
private List<Comment> comments = new ArrayList<>();
private String name;
private Description description;
private List<Type> implementz = new ArrayList<>();
private List<FieldDefinition> definitions = new ArrayList<>();
private List<Directive> directives = new ArrayList<>();
private IgnoredChars ignoredChars = IgnoredChars.EMPTY;
Expand All @@ -77,6 +80,7 @@ private Builder(InterfaceTypeExtensionDefinition existing) {
this.name = existing.getName();
this.description = existing.getDescription();
this.directives = existing.getDirectives();
this.implementz = existing.getImplements();
this.definitions = existing.getFieldDefinitions();
this.ignoredChars = existing.getIgnoredChars();
this.additionalData = new LinkedHashMap<>(existing.getAdditionalData());
Expand All @@ -102,6 +106,16 @@ public Builder description(Description description) {
return this;
}

public Builder implementz(List<Type> implementz) {
this.implementz = implementz;
return this;
}

public Builder implementz(Type implementz) {
this.implementz.add(implementz);
return this;
}

public Builder definitions(List<FieldDefinition> definitions) {
this.definitions = definitions;
return this;
Expand Down Expand Up @@ -130,6 +144,7 @@ public Builder additionalData(String key, String value) {

public InterfaceTypeExtensionDefinition build() {
return new InterfaceTypeExtensionDefinition(name,
implementz,
definitions,
directives,
description,
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/graphql/language/ObjectTypeDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import static java.util.Collections.emptyMap;

@PublicApi
public class ObjectTypeDefinition extends AbstractDescribedNode<ObjectTypeDefinition> implements TypeDefinition<ObjectTypeDefinition>, DirectivesContainer<ObjectTypeDefinition>, NamedNode<ObjectTypeDefinition> {
public class ObjectTypeDefinition extends AbstractDescribedNode<ObjectTypeDefinition> implements ImplementingTypeDefinition<ObjectTypeDefinition>, DirectivesContainer<ObjectTypeDefinition>, NamedNode<ObjectTypeDefinition> {
private final String name;
private final List<Type> implementz;
private final List<Directive> directives;
Expand Down Expand Up @@ -53,6 +53,7 @@ public ObjectTypeDefinition(String name) {
this(name, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), null, null, new ArrayList<>(), IgnoredChars.EMPTY, emptyMap());
}

@Override
public List<Type> getImplements() {
return new ArrayList<>(implementz);
}
Expand All @@ -62,6 +63,7 @@ public List<Directive> getDirectives() {
return new ArrayList<>(directives);
}

@Override
public List<FieldDefinition> getFieldDefinitions() {
return new ArrayList<>(fieldDefinitions);
}
Expand Down
32 changes: 20 additions & 12 deletions src/main/java/graphql/parser/GraphqlAntlrToLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import graphql.language.VariableReference;
import graphql.parser.antlr.GraphqlLexer;
import graphql.parser.antlr.GraphqlParser;
import graphql.util.FpKit;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
Expand Down Expand Up @@ -433,12 +434,7 @@ protected ObjectTypeDefinition createObjectTypeDefinition(GraphqlParser.ObjectTy
def.description(newDescription(ctx.description()));
def.directives(createDirectives(ctx.directives()));
GraphqlParser.ImplementsInterfacesContext implementsInterfacesContext = ctx.implementsInterfaces();
List<Type> implementz = new ArrayList<>();
while (implementsInterfacesContext != null) {
List<TypeName> typeNames = implementsInterfacesContext.typeName().stream().map(this::createTypeName).collect(toList());
implementz.addAll(0, typeNames);
implementsInterfacesContext = implementsInterfacesContext.implementsInterfaces();
}
List<Type> implementz = getImplementz(implementsInterfacesContext);
def.implementz(implementz);
if (ctx.fieldsDefinition() != null) {
def.fieldDefinitions(createFieldDefinitions(ctx.fieldsDefinition()));
Expand All @@ -452,12 +448,7 @@ protected ObjectTypeExtensionDefinition createObjectTypeExtensionDefinition(Grap
addCommonData(def, ctx);
def.directives(createDirectives(ctx.directives()));
GraphqlParser.ImplementsInterfacesContext implementsInterfacesContext = ctx.implementsInterfaces();
List<Type> implementz = new ArrayList<>();
while (implementsInterfacesContext != null) {
List<TypeName> typeNames = implementsInterfacesContext.typeName().stream().map(this::createTypeName).collect(toList());
implementz.addAll(0, typeNames);
implementsInterfacesContext = implementsInterfacesContext.implementsInterfaces();
}
List<Type> implementz = getImplementz(implementsInterfacesContext);
def.implementz(implementz);
if (ctx.extensionFieldsDefinition() != null) {
def.fieldDefinitions(createFieldDefinitions(ctx.extensionFieldsDefinition()));
Expand Down Expand Up @@ -517,6 +508,9 @@ protected InterfaceTypeDefinition createInterfaceTypeDefinition(GraphqlParser.In
addCommonData(def, ctx);
def.description(newDescription(ctx.description()));
def.directives(createDirectives(ctx.directives()));
GraphqlParser.ImplementsInterfacesContext implementsInterfacesContext = ctx.implementsInterfaces();
List<Type> implementz = getImplementz(implementsInterfacesContext);
def.implementz(implementz);
def.definitions(createFieldDefinitions(ctx.fieldsDefinition()));
return def.build();
}
Expand All @@ -526,6 +520,9 @@ protected InterfaceTypeExtensionDefinition createInterfaceTypeExtensionDefinitio
def.name(ctx.name().getText());
addCommonData(def, ctx);
def.directives(createDirectives(ctx.directives()));
GraphqlParser.ImplementsInterfacesContext implementsInterfacesContext = ctx.implementsInterfaces();
List<Type> implementz = getImplementz(implementsInterfacesContext);
def.implementz(implementz);
def.definitions(createFieldDefinitions(ctx.extensionFieldsDefinition()));
return def.build();
}
Expand Down Expand Up @@ -882,4 +879,15 @@ protected List<Comment> getCommentOnChannel(List<Token> refChannel) {
return comments;
}


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

implementz.addAll(0, typeNames);
implementsInterfacesContext = implementsInterfacesContext.implementsInterfaces();
}
return implementz;
}
}
Loading