Skip to content

Commit

Permalink
get scalars working correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ashley-taylor committed Mar 6, 2023
1 parent c5e47b6 commit beea4ea
Show file tree
Hide file tree
Showing 61 changed files with 645 additions and 822 deletions.
17 changes: 9 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,19 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.fleetpin</groupId>
<artifactId>graphql-builder</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>

<name>GraphQL Builder</name>
<description>Builds a graphql schema from a model using reflection</description>
<url>https://github.com/fleetpin/graphql-builder</url>

<organization>
<name>test</name>
</organization>
<inceptionYear>2017</inceptionYear>


<properties>
<junit.jupiter.version>5.6.0</junit.jupiter.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jackson.version>2.14.2</jackson.version>
<pitest.version>1.9.0</pitest.version>
<pitest-junit5-plugin.version>1.0.0</pitest-junit5-plugin.version>
<graphql.version>20.0</graphql.version>
</properties>

<distributionManagement>
Expand Down Expand Up @@ -146,7 +141,13 @@
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>20.0</version>
<version>${graphql.version}</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-extended-scalars</artifactId>
<version>${graphql.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/fleetpin/graphql/builder/Authorizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.fleetpin.graphql.builder;

public interface Authorizer {}
12 changes: 0 additions & 12 deletions src/main/java/com/fleetpin/graphql/builder/AuthorizerSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.fleetpin.graphql.builder;

import graphql.GraphqlErrorException;
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/fleetpin/graphql/builder/DirectiveCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.fleetpin.graphql.builder;

import graphql.schema.DataFetcher;
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/fleetpin/graphql/builder/DirectiveOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.fleetpin.graphql.builder;

import java.lang.annotation.Annotation;
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/fleetpin/graphql/builder/DirectivesSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.fleetpin.graphql.builder;

import com.fleetpin.graphql.builder.annotations.Directive;
Expand Down
56 changes: 38 additions & 18 deletions src/main/java/com/fleetpin/graphql/builder/EntityHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,14 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.fleetpin.graphql.builder;

import com.fleetpin.graphql.builder.TypeMeta.Flag;
import com.fleetpin.graphql.builder.annotations.Id;
import com.fleetpin.graphql.builder.annotations.Union;
import com.fleetpin.graphql.builder.mapper.InputTypeBuilder;
import graphql.Scalars;
import graphql.com.google.common.collect.Sets;
import graphql.schema.GraphQLInputType;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNamedInputType;
Expand All @@ -38,12 +27,17 @@
import graphql.schema.GraphQLTypeReference;
import graphql.schema.GraphQLUnionType;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

public abstract class EntityHolder {
Expand Down Expand Up @@ -172,14 +166,19 @@ private static InputTypeBuilder process(Iterator<Class<?>> iterator, InputTypeBu
var type = iterator.next();

if (List.class.isAssignableFrom(type)) {
return processList(iterator, resolver);
return processCollection(ArrayList::new, iterator, resolver);
}

if (Set.class.isAssignableFrom(type)) {
return processCollection(size -> new LinkedHashSet<>((int) (size / 0.75 + 1)), iterator, resolver);
}

if (Optional.class.isAssignableFrom(type)) {
return processOptional(iterator, resolver);
}
// if(type.isArray()) {
// return processArray(iterator);
// }
if (type.isArray()) {
return processArray(type, iterator, resolver);
}

if (iterator.hasNext()) {
throw new RuntimeException("Unsupported type " + type);
Expand Down Expand Up @@ -225,13 +224,34 @@ private static InputTypeBuilder processOptional(Iterator<Class<?>> iterator, Inp
};
}

private static InputTypeBuilder processList(Iterator<Class<?>> iterator, InputTypeBuilder resolver) {
private static InputTypeBuilder processArray(Class<?> type, Iterator<Class<?>> iterator, InputTypeBuilder resolver) {
var component = type.getComponentType();
if (component.isPrimitive()) {
throw new RuntimeException("Do not support primitive array");
}

var mapper = process(iterator, resolver);
return (obj, context, locale) -> {
if (obj instanceof Collection) {
var collection = (Collection) obj;
Object[] toReturn = (Object[]) Array.newInstance(component, collection.size());
int i = 0;
for (var c : collection) {
toReturn[i] = mapper.convert(c, context, locale);
}
return toReturn;
} else {
throw new RuntimeException("Expected a Collection got " + obj.getClass());
}
};
}

var toReturn = new ArrayList<>(collection.size());
private static InputTypeBuilder processCollection(Function<Integer, Collection> create, Iterator<Class<?>> iterator, InputTypeBuilder resolver) {
var mapper = process(iterator, resolver);
return (obj, context, locale) -> {
if (obj instanceof Collection) {
var collection = (Collection) obj;
var toReturn = create.apply(collection.size());
for (var c : collection) {
toReturn.add(mapper.convert(c, context, locale));
}
Expand Down
48 changes: 32 additions & 16 deletions src/main/java/com/fleetpin/graphql/builder/EntityProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.fleetpin.graphql.builder;

import com.fleetpin.graphql.builder.annotations.Scalar;
Expand All @@ -31,11 +19,13 @@
import graphql.schema.GraphQLCodeRegistry;
import graphql.schema.GraphQLInputType;
import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLType;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
Expand All @@ -48,9 +38,10 @@ public class EntityProcessor {

private final Map<String, EntityHolder> entities;

EntityProcessor(GraphQLCodeRegistry.Builder codeRegistry, DirectivesSchema diretives) {
EntityProcessor(List<GraphQLScalarType> scalars, GraphQLCodeRegistry.Builder codeRegistry, DirectivesSchema diretives) {
this.entities = new HashMap<>();
addDefaults();
addScalars(scalars);

this.codeRegistry = codeRegistry;
this.directives = diretives;
Expand All @@ -69,6 +60,34 @@ private void addDefaults() {
put(String.class, new ScalarEntity(Scalars.GraphQLString));
}

private void addScalars(List<GraphQLScalarType> scalars) {
for (var scalar : scalars) {
var coercing = scalar.getCoercing();
var type = coercing.getClass();
for (var method : type.getMethods()) {
if (method.isSynthetic()) {
continue;
}
if ("parseValue".equals(method.getName())) {
var returnType = method.getReturnType();
if (returnType.equals(Long.class)) {
put(Long.TYPE, new ScalarEntity(scalar));
} else if (returnType.equals(Byte.class)) {
put(Byte.TYPE, new ScalarEntity(scalar));
} else if (returnType.equals(Character.class)) {
put(Character.TYPE, new ScalarEntity(scalar));
} else if (returnType.equals(Float.class)) {
put(Float.TYPE, new ScalarEntity(scalar));
} else if (returnType.equals(Short.class)) {
put(Short.TYPE, new ScalarEntity(scalar));
}
put(returnType, new ScalarEntity(scalar));
break;
}
}
}
}

private void put(Class<?> type, ScalarEntity entity) {
var name = EntityUtil.getName(new TypeMeta(null, type, type));
entities.put(name, entity);
Expand All @@ -88,9 +107,6 @@ EntityHolder getEntity(TypeMeta meta) {
name,
__ -> {
Class<?> type = meta.getType();
if (type.equals(Object.class)) {
return new ReferenceEntity("Object");
}
Type genericType = meta.getGenericType();
if (genericType == null) {
genericType = type;
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/fleetpin/graphql/builder/EntityUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.fleetpin.graphql.builder;

import com.fleetpin.graphql.builder.annotations.GraphQLIgnore;
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/fleetpin/graphql/builder/EnumEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.fleetpin.graphql.builder;

import com.fleetpin.graphql.builder.annotations.GraphQLDescription;
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/fleetpin/graphql/builder/ErrorType.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.fleetpin.graphql.builder;

import graphql.ErrorClassification;
Expand Down
Loading

0 comments on commit beea4ea

Please sign in to comment.