Skip to content
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

* add possibility to configure custom scalar types externally #11

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
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@

package com.oembedler.moon.graphql.engine;

import com.oembedler.moon.graphql.engine.dfs.GraphQLTypeResolver;
import com.oembedler.moon.graphql.engine.type.resolver.DateTypeResolver;
import com.oembedler.moon.graphql.engine.type.resolver.LocalDateTimeResolver;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
* @author <a href="mailto:[email protected]">oEmbedler Inc.</a>
*/
Expand All @@ -37,6 +43,12 @@ public class GraphQLSchemaConfig {
private String schemaMutationObjectName = "Mutation";
private boolean dateAsTimestamp = true;
private String dateFormat = "yyyy-MM-dd'T'HH:mm'Z'";
private List<GraphQLTypeResolver> graphQLTypeResolvers = new ArrayList<>();

{
graphQLTypeResolvers.add(new DateTypeResolver());
graphQLTypeResolvers.add(new LocalDateTimeResolver());
}

// ---

Expand Down Expand Up @@ -117,6 +129,15 @@ public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}

public void addGraphQLTypeResolver(GraphQLTypeResolver graphQLTypeResolver) {
Assert.notNull(graphQLTypeResolver, "GraphQl resolver cannot be null!");
this.graphQLTypeResolvers.add(graphQLTypeResolver);
}

public List<GraphQLTypeResolver> getGraphQLTypeResolvers() {
return graphQLTypeResolvers;
}

public boolean isDateAsTimestamp() {
return dateAsTimestamp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@
package com.oembedler.moon.graphql.engine.dfs;

import com.oembedler.moon.graphql.engine.GraphQLSchemaConfig;
import com.oembedler.moon.graphql.engine.type.GraphQLDateType;
import com.oembedler.moon.graphql.engine.type.GraphQLLocalDateTimeType;
import graphql.schema.GraphQLScalarType;
import org.springframework.util.Assert;

import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.util.Date;

/**
* @author <a href="mailto:[email protected]">oEmbedler Inc.</a>
Expand All @@ -43,26 +39,17 @@ public GraphQLMappingContext(GraphQLSchemaConfig graphQLSchemaConfig) {

public GraphQLScalarType getScalarGraphQLType(final Type cls) {
GraphQLScalarType graphQLScalarType = MappingConstants.getScalarGraphQLType(cls);
if (Date.class.isAssignableFrom((Class<?>) cls)) {
if (graphQLSchemaConfig.isDateAsTimestamp())
graphQLScalarType = MappingConstants.graphQLTimestamp;
else
graphQLScalarType = getGraphQLDateType();
} else if (LocalDateTime.class.isAssignableFrom((Class<?>) cls)) {
if (graphQLSchemaConfig.isDateAsTimestamp())
graphQLScalarType = MappingConstants.graphQLTimestamp;
else
graphQLScalarType = getGraphQLDateType();
}
return graphQLScalarType;
}

private GraphQLScalarType getGraphQLDateType() {
return new GraphQLDateType("Date", "Date formatted according to defined format string", graphQLSchemaConfig.getDateFormat());
}
if (graphQLScalarType == null) {
graphQLScalarType = graphQLSchemaConfig.getGraphQLTypeResolvers()
.stream()
.filter(resolver -> resolver.getType().isAssignableFrom((Class<?>) cls))
.findFirst()
.map(resolver -> resolver.resolve(graphQLSchemaConfig))
.orElse(null);
}

private GraphQLScalarType getGraphQLLocalDateTimeType() {
return new GraphQLLocalDateTimeType("LocalDateTime", "LocalDateTime formatted according to defined format string", graphQLSchemaConfig.getDateFormat());
return graphQLScalarType;
}

// ---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public GraphQLFieldDefinition getFieldDefinition(DfsContext dfsContext, Class<?>
.deprecate(resolvableTypeAccessor.getGraphQLDeprecationReason())
.description(resolvableTypeAccessor.getDescription());

boolean isConstant = Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers());
boolean isConstant = isPublicConstant(field);
if (isConstant) {
graphQLFieldDefinitionBuilder.staticValue(org.springframework.util.ReflectionUtils.getField(field, null));
}
Expand All @@ -299,6 +299,13 @@ public GraphQLFieldDefinition getFieldDefinition(DfsContext dfsContext, Class<?>
return graphQLFieldDefinition;
}

private boolean isPublicConstant(Field field) {
int modifiers = field.getModifiers();
return Modifier.isFinal(modifiers)
&& Modifier.isStatic(modifiers)
&& !Modifier.isPrivate(modifiers);
}

public void addToFieldDefinitionResolverMap(DfsContext dfsContext, GraphQLFieldDefinition graphQLFieldDefinition, String complexitySpelExpression) {
fieldDefinitionResolverMap.put(graphQLFieldDefinition,
new GraphQLFieldDefinitionWrapper(graphQLFieldDefinition, complexitySpelExpression));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.oembedler.moon.graphql.engine.dfs;

import com.oembedler.moon.graphql.engine.GraphQLSchemaConfig;
import graphql.schema.GraphQLScalarType;

/**
* @author Sergey Kuptsov
* @since 23/03/2017
*/
public interface GraphQLTypeResolver {
Class getType();

GraphQLScalarType resolve(GraphQLSchemaConfig graphQLSchemaConfig);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.oembedler.moon.graphql.engine.type.resolver;

import com.oembedler.moon.graphql.engine.GraphQLSchemaConfig;
import com.oembedler.moon.graphql.engine.dfs.GraphQLTypeResolver;
import com.oembedler.moon.graphql.engine.dfs.MappingConstants;
import com.oembedler.moon.graphql.engine.type.GraphQLDateType;
import graphql.schema.GraphQLScalarType;

import java.util.Date;

/**
* @author Sergey Kuptsov
* @since 23/03/2017
*/
public class DateTypeResolver implements GraphQLTypeResolver {

@Override
public Class getType() {
return Date.class;
}

@Override
public GraphQLScalarType resolve(GraphQLSchemaConfig graphQLSchemaConfig) {
if (graphQLSchemaConfig.isDateAsTimestamp())
return MappingConstants.graphQLTimestamp;
else
return new GraphQLDateType("Date", "Date formatted according to defined format string",
graphQLSchemaConfig.getDateFormat());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.oembedler.moon.graphql.engine.type.resolver;

import com.oembedler.moon.graphql.engine.GraphQLSchemaConfig;
import com.oembedler.moon.graphql.engine.dfs.GraphQLTypeResolver;
import com.oembedler.moon.graphql.engine.dfs.MappingConstants;
import com.oembedler.moon.graphql.engine.type.GraphQLLocalDateTimeType;
import graphql.schema.GraphQLScalarType;

import java.time.LocalDateTime;

/**
* @author Sergey Kuptsov
* @since 23/03/2017
*/
public class LocalDateTimeResolver implements GraphQLTypeResolver {
@Override
public Class getType() {
return LocalDateTime.class;
}

@Override
public GraphQLScalarType resolve(GraphQLSchemaConfig graphQLSchemaConfig) {
if (graphQLSchemaConfig.isDateAsTimestamp())
return MappingConstants.graphQLTimestamp;
else {
return new GraphQLLocalDateTimeType("LocalDateTime", "LocalDateTime formatted according to defined format string",
graphQLSchemaConfig.getDateFormat());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
package com.oembedler.moon.graphql.test.todoschema.objecttype;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.oembedler.moon.graphql.engine.stereotype.*;
import com.oembedler.moon.graphql.test.GenericTodoSchemaParserTest;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
Expand All @@ -37,6 +35,7 @@ public class UserObjectType extends BaseObjectType {

private String name;
private ROLE role;
private Date created;

@GraphQLIgnore
private UserObjectType manager;
Expand Down Expand Up @@ -70,6 +69,14 @@ public void setRole(ROLE role) {
this.role = role;
}

public Date getCreated() {
return created;
}

public void setCreated(Date created) {
this.created = created;
}

@GraphQLField("manager")
public UserObjectType getManager(UserObjectType employee,
@GraphQLIn(value = "ids", defaultSpel = "T(java.util.Collections).EMPTY_SET") Set<String> ids) {
Expand Down