-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from intuit/optimize-svc-metadata
optimize service metadata implementation
- Loading branch information
Showing
12 changed files
with
300 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
src/main/java/com/intuit/graphql/orchestrator/schema/ServiceMetadataImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package com.intuit.graphql.orchestrator.schema; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.intuit.graphql.orchestrator.ServiceProvider; | ||
import com.intuit.graphql.orchestrator.federation.metadata.FederationMetadata; | ||
import com.intuit.graphql.orchestrator.schema.transform.FieldResolverContext; | ||
import graphql.schema.FieldCoordinates; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ServiceMetadataImpl implements ServiceMetadata { | ||
|
||
private final Map<String, TypeMetadata> typeMetadataMap; | ||
private final ServiceProvider serviceProvider; | ||
private final FederationMetadata federationMetadata; | ||
private final boolean hasInterfaceOrUnion; | ||
private final boolean hasFieldResolverDefinition; | ||
|
||
private ServiceMetadataImpl(Builder builder) { | ||
typeMetadataMap = builder.typeMetadataMap; | ||
serviceProvider = requireNonNull(builder.serviceProvider); | ||
federationMetadata = builder.federationMetadata; | ||
hasInterfaceOrUnion = builder.hasInterfaceOrUnion; | ||
hasFieldResolverDefinition = builder.hasFieldResolverDefinition; | ||
} | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
public static Builder newBuilder(ServiceMetadataImpl copy) { | ||
Builder builder = new Builder(); | ||
builder.typeMetadataMap = copy.getTypeMetadataMap(); | ||
builder.serviceProvider = copy.getServiceProvider(); | ||
builder.federationMetadata = copy.getFederationMetadata(); | ||
builder.hasInterfaceOrUnion = copy.isHasInterfaceOrUnion(); | ||
builder.hasFieldResolverDefinition = copy.isHasFieldResolverDefinition(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean hasType(String typeName) { | ||
return this.typeMetadataMap.containsKey(typeName); | ||
} | ||
|
||
@Override | ||
public boolean requiresTypenameInjection() { | ||
return this.hasInterfaceOrUnion; | ||
} | ||
|
||
@Override | ||
public boolean hasFieldResolverDirective() { | ||
return this.hasFieldResolverDefinition; | ||
} | ||
|
||
@Override | ||
public boolean isFederationService() { | ||
return this.serviceProvider.isFederationProvider(); | ||
} | ||
|
||
@Override | ||
public boolean shouldRemoveExternalFields() { | ||
return this.hasFieldResolverDirective() || this.isFederationService(); | ||
} | ||
|
||
|
||
@Override | ||
public ServiceProvider getServiceProvider() { | ||
return this.serviceProvider; | ||
} | ||
|
||
@Override | ||
public FieldResolverContext getFieldResolverContext(FieldCoordinates fieldCoordinates) { | ||
return Optional.ofNullable(this.typeMetadataMap.get(fieldCoordinates.getTypeName())) | ||
.map(typeMetadata -> typeMetadata.getFieldResolverContext(fieldCoordinates.getFieldName())) | ||
.orElse(null); | ||
} | ||
|
||
@Override | ||
public boolean isOwnedByEntityExtension(FieldCoordinates fieldCoordinates) { | ||
return this.serviceProvider.isFederationProvider() && federationMetadata.isFieldExternal(fieldCoordinates); | ||
} | ||
|
||
|
||
@Override | ||
public boolean isEntity(String typename) { | ||
return this.isFederationService() && this.federationMetadata.isEntity(typename); | ||
} | ||
|
||
@Override | ||
public FederationMetadata getFederationServiceMetadata() { | ||
return federationMetadata; | ||
} | ||
|
||
|
||
public static final class Builder { | ||
|
||
private Map<String, TypeMetadata> typeMetadataMap = new HashMap<>(); | ||
private ServiceProvider serviceProvider; | ||
private FederationMetadata federationMetadata; | ||
private boolean hasInterfaceOrUnion; | ||
private boolean hasFieldResolverDefinition; | ||
|
||
private Builder() { | ||
} | ||
|
||
public Builder typeMetadataMap(Map<String, TypeMetadata> val) { | ||
typeMetadataMap = val; | ||
return this; | ||
} | ||
|
||
public Builder serviceProvider(ServiceProvider val) { | ||
serviceProvider = val; | ||
return this; | ||
} | ||
|
||
public Builder federationMetadata(FederationMetadata val) { | ||
federationMetadata = val; | ||
return this; | ||
} | ||
|
||
public Builder hasInterfaceOrUnion(boolean val) { | ||
hasInterfaceOrUnion = val; | ||
return this; | ||
} | ||
|
||
public Builder hasFieldResolverDefinition(boolean val) { | ||
hasFieldResolverDefinition = val; | ||
return this; | ||
} | ||
|
||
public ServiceMetadataImpl build() { | ||
return new ServiceMetadataImpl(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.