Skip to content

Add URI scalar #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/main/java/graphql/scalars/ExtendedScalars.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import graphql.scalars.object.JsonScalar;
import graphql.scalars.object.ObjectScalar;
import graphql.scalars.regex.RegexScalar;
import graphql.scalars.uri.UriScalar;
import graphql.scalars.url.UrlScalar;
import graphql.schema.GraphQLScalarType;

Expand Down Expand Up @@ -209,6 +210,11 @@ public class ExtendedScalars {
*/
public static final GraphQLScalarType Json = JsonScalar.INSTANCE;

/**
* A URI scalar that accepts URI strings and produces {@link java.net.URI} objects at runtime
*/
public static final GraphQLScalarType Uri = UriScalar.INSTANCE;

/**
* A URL scalar that accepts URL strings and produces {@link java.net.URL} objects at runtime
*/
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/graphql/scalars/uri/UriScalar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package graphql.scalars.uri;

import graphql.GraphQLContext;
import graphql.Internal;
import graphql.execution.CoercedVariables;
import graphql.language.StringValue;
import graphql.language.Value;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseLiteralException;
import graphql.schema.CoercingParseValueException;
import graphql.schema.CoercingSerializeException;
import graphql.schema.GraphQLScalarType;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Function;

import static graphql.scalars.util.Kit.typeName;

@Internal
public final class UriScalar {

private UriScalar() {
}

public static final GraphQLScalarType INSTANCE;

static {
Coercing<URI, URI> coercing = new Coercing<>() {
@Override
public URI serialize(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException {
Optional<URI> uri;
if (input instanceof String) {
uri = Optional.of(parseURI(input.toString(), CoercingSerializeException::new));
} else {
uri = toURI(input);
}
if (uri.isPresent()) {
return uri.get();
}
throw new CoercingSerializeException(
"Expected a 'URI' like object but was '" + typeName(input) + "'."
);
}

@Override
public URI parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException {
String uriStr;
if (input instanceof String) {
uriStr = String.valueOf(input);
} else {
Optional<URI> uri = toURI(input);
if (uri.isEmpty()) {
throw new CoercingParseValueException(
"Expected a 'URI' like object but was '" + typeName(input) + "'."
);
}
return uri.get();
}
return parseURI(uriStr, CoercingParseValueException::new);
}

@Override
public URI parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException {
if (!(input instanceof StringValue)) {
throw new CoercingParseLiteralException(
"Expected AST type 'StringValue' but was '" + typeName(input) + "'."
);
}
return parseURI(((StringValue) input).getValue(), CoercingParseLiteralException::new);
}

@Override
public Value<?> valueToLiteral(Object input, GraphQLContext graphQLContext, Locale locale) {
URI uri = serialize(input, graphQLContext, locale);
return StringValue.newStringValue(uri.toString()).build();
}


private URI parseURI(String input, Function<String, RuntimeException> exceptionMaker) {
try {
return new URI(input);
} catch (URISyntaxException e) {
throw exceptionMaker.apply("Invalid URI value : '" + input + "'.");
}
}
};

INSTANCE = GraphQLScalarType.newScalar()
.name("Uri")
.description("A Uri scalar")
.coercing(coercing)
.build();
}

private static Optional<URI> toURI(Object input) {
if (input instanceof URI) {
return Optional.of((URI) input);
} else if (input instanceof URL) {
try {
return Optional.of(((URL) input).toURI());
} catch (URISyntaxException ignored) {
}
} else if (input instanceof File) {
return Optional.of(((File) input).toURI());
}
return Optional.empty();
}

}
111 changes: 111 additions & 0 deletions src/test/groovy/graphql/scalars/uri/UriScalarTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package graphql.scalars.uri


import graphql.language.BooleanValue
import graphql.language.StringValue
import graphql.scalars.ExtendedScalars
import graphql.scalars.util.AbstractScalarTest
import graphql.schema.CoercingParseLiteralException
import graphql.schema.CoercingParseValueException
import graphql.schema.CoercingSerializeException
import spock.lang.Unroll

import static graphql.scalars.util.TestKit.mkStringValue

class UriScalarTest extends AbstractScalarTest {

def coercing = ExtendedScalars.Uri.getCoercing()

@Unroll
def "test serialize"() {

when:
def result = coercing.serialize(input, graphQLContext, locale)
then:
result == expectedResult
where:
input | expectedResult
new URL("http://www.graphql-java.com/") | new URI("http://www.graphql-java.com/")
new URI("http://www.graphql-java.com/") | new URI("http://www.graphql-java.com/")
new File("/this/that") | new URI("file:/this/that")
"http://www.graphql-java.com/" | new URI("http://www.graphql-java.com/")
}

@Unroll
def "test valueToLiteral"() {

when:
def result = coercing.valueToLiteral(input, graphQLContext, locale)
then:
result.isEqualTo(expectedResult)
where:
input | expectedResult
new URL("http://www.graphql-java.com/") | mkStringValue("http://www.graphql-java.com/")
new URI("http://www.graphql-java.com/") | mkStringValue("http://www.graphql-java.com/")
new File("/this/that") | mkStringValue("file:/this/that")
"http://www.graphql-java.com/" | mkStringValue("http://www.graphql-java.com/")
}

@Unroll
def "test serialize bad inputs"() {
when:
coercing.serialize(input, graphQLContext, locale)
then:
thrown(exceptionClas)
where:
input || exceptionClas
666 || CoercingSerializeException
"1:not/a/uri" || CoercingSerializeException
}

@Unroll
def "test parseValue"() {
when:
def result = coercing.parseValue(input, graphQLContext, locale)
then:
result == expectedResult
where:
input | expectedResult
new URL("http://www.graphql-java.com/") | new URI("http://www.graphql-java.com/")
new URI("http://www.graphql-java.com/") | new URI("http://www.graphql-java.com/")
new File("/this/that") | new URI("file:/this/that")
"http://www.graphql-java.com/" | new URI("http://www.graphql-java.com/")
}

@Unroll
def "test parseValue bad inputs"() {
when:
coercing.parseValue(input, graphQLContext, locale)
then:
thrown(exceptionClas)
where:
input || exceptionClas
666 || CoercingParseValueException
"1:not/a/url" || CoercingParseValueException
}

@Unroll
def "test parseLiteral"() {
when:
def result = coercing.parseLiteral(input, variables, graphQLContext, locale)
then:
result == expectedResult
where:
input | expectedResult
new StringValue("http://www.graphql-java.com/") | new URI("http://www.graphql-java.com/")
}

@Unroll
def "test parseLiteral bad inputs"() {
when:
coercing.parseLiteral(input, variables, graphQLContext, locale)
then:
thrown(exceptionClas)
where:
input | exceptionClas
new BooleanValue(true) | CoercingParseLiteralException
new StringValue("1:not/a/url") | CoercingParseLiteralException
}


}