Skip to content

Commit

Permalink
Remove redundant (commented out) code + imports
Browse files Browse the repository at this point in the history
Prefer NIO streams as far as possible
Enable use of random ports for, e.g. testing
  • Loading branch information
pc3356 committed Mar 23, 2018
1 parent 875d1cb commit 407e624
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package org.hypergraphql.config.schema;

import org.hypergraphql.datafetching.services.Service;

public class FieldConfig {

private String id;
// private Service service;

public FieldConfig(String id, Service service) {
public FieldConfig(String id) {

this.id = id;
// this.service = service;

}

public String getId() { return this.id; }
// public Service getService() { return this.service; }

}
2 changes: 0 additions & 2 deletions src/main/java/org/hypergraphql/config/schema/TypeConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.hypergraphql.config.schema;

import org.hypergraphql.datafetching.services.Service;

import java.util.Map;

public class TypeConfig {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/hypergraphql/config/system/FetchParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ private String extractTargetURI(final DataFetchingEnvironment environment, final
throw new HGQLConfigurationException("'name' is a required field for HGQL types");
}

if (!parentTypeName.equals("Query")) {
if (parentTypeName.equals("Query")) {

return null;

} else {

final TypeConfig typeConfig = schema.getTypes().get(parentTypeName);

Expand All @@ -95,8 +99,6 @@ private String extractTargetURI(final DataFetchingEnvironment environment, final
throw new HGQLConfigurationException("schema must have a value for '" + targetName + "'");
}
}

return null;
}

}
16 changes: 15 additions & 1 deletion src/main/java/org/hypergraphql/config/system/GraphqlConfig.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.hypergraphql.config.system;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.concurrent.ThreadLocalRandom;

public class GraphqlConfig {

private Integer port;
Expand All @@ -14,7 +17,11 @@ public GraphqlConfig(@JsonProperty("port") Integer port,
@JsonProperty("graphql") String graphqlPath,
@JsonProperty("graphiql") String graphiqlPath
) {
this.port = port;
if(port == null) {
this.port = generateRandomPort();
} else {
this.port = port;
}
this.graphqlPath = graphqlPath;
this.graphiqlPath = graphiqlPath;
}
Expand All @@ -36,4 +43,11 @@ public String graphiqlPath() {
public String graphiQLPath() {
return graphiqlPath;
}

@JsonIgnore
private int generateRandomPort() {
int min = 1024;
int max = 65536;
return ThreadLocalRandom.current().nextInt(min, max);
}
}
65 changes: 50 additions & 15 deletions src/main/java/org/hypergraphql/config/system/HGQLConfig.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package org.hypergraphql.config.system;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import org.apache.http.client.utils.URIBuilder;
import org.apache.log4j.Logger;
import org.hypergraphql.datamodel.HGQLSchema;
import org.hypergraphql.datamodel.HGQLSchemaWiring;
import org.hypergraphql.exception.HGQLConfigurationException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
Expand Down Expand Up @@ -56,7 +64,7 @@ public static void setLogger(Logger logger) {
private static Logger logger = Logger.getLogger(HGQLConfig.class);

@JsonCreator
private HGQLConfig(
private HGQLConfig(
@JsonProperty("name") String name,
@JsonProperty("schema") String schemaFile,
@JsonProperty("server") GraphqlConfig graphqlConfig,
Expand All @@ -68,29 +76,46 @@ private HGQLConfig(
this.serviceConfigs = services;
}

public HGQLConfig(String propertyFilepath) {
public static HGQLConfig fromClasspathConfig(final String path) {

final InputStream in = HGQLConfig.class.getClassLoader().getResourceAsStream(path);
return new HGQLConfig(in);
}

public static HGQLConfig fromFileSystemPath(final String path) {

try {
return new HGQLConfig(new FileInputStream(path));
} catch (FileNotFoundException e) {
throw new HGQLConfigurationException("Unable to find config file", e);
}
}

public static HGQLConfig from(final InputStream inputStream) {
return new HGQLConfig(inputStream);
}

private HGQLConfig(final InputStream inputStream) {

ObjectMapper mapper = new ObjectMapper();

try {
HGQLConfig config = mapper.readValue(new File(propertyFilepath), HGQLConfig.class);
HGQLConfig config = mapper.readValue(inputStream, HGQLConfig.class);

SchemaParser schemaParser = new SchemaParser();
this.registry = schemaParser.parse(new File(config.schemaFile));

this.name = config.name;
this.schemaFile = config.schemaFile;
this.serviceConfigs = config.serviceConfigs;
this.graphqlConfig = config.graphqlConfig;
checkServicePorts(config.serviceConfigs);
this.serviceConfigs = config.serviceConfigs;
HGQLSchemaWiring wiring = new HGQLSchemaWiring(this.registry, this.name, this.serviceConfigs);
this.schema = wiring.getSchema();
this.hgqlSchema = wiring.getHgqlSchema();

} catch (IOException e) {
logger.error(e);
throw new HGQLConfigurationException("Error reading from configuration file", e);
}


}

public GraphqlConfig getGraphqlConfig() {
Expand All @@ -101,13 +126,23 @@ public String getName() {
return name;
}


public TypeDefinitionRegistry getRegistry() {
return registry;
}

public String getSchemaFile() {
return schemaFile;
@JsonIgnore
private void checkServicePorts(final List<ServiceConfig> serviceConfigs) {

serviceConfigs.forEach(serviceConfig -> {
try {
if(serviceConfig.getUrl() != null) {
final URI serviceUri = new URI(serviceConfig.getUrl());
if (serviceUri.getHost().equals("localhost") && serviceUri.getPort() <= 0) {
final URIBuilder uriBuilder = new URIBuilder(serviceUri);
uriBuilder.setPort(graphqlConfig.port());
serviceConfig.setUrl(uriBuilder.build().toString());
}
}
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
});
}
}

Expand Down

0 comments on commit 407e624

Please sign in to comment.