Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/maven/com.graphql-java-graphql-ja…
Browse files Browse the repository at this point in the history
…va-19.2
  • Loading branch information
phillip-kruger authored Sep 26, 2022
2 parents 8084b39 + 9407940 commit b03a1ca
Show file tree
Hide file tree
Showing 31 changed files with 449 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .github/project.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: SmallRye GraphQL
release:
current-version: 2.0.0.RC8
current-version: 2.0.0.RC10
next-version: 2.0.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ private String graphQlInputTypeName(TypeInfo type) {
case "LocalDateTime":
case "OffsetDateTime":
case "ZonedDateTime":
case "Instant":
return "DateTime";
default:
return type.getSimpleName() + (type.isScalar() || type.isEnum() ? "" : "Input");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ void shouldCallInstantQuery() {

Instant value = api.foo(in);

then(fixture.query()).isEqualTo("query foo($instant: Instant) { foo(instant: $instant) }");
then(fixture.query()).isEqualTo("query foo($instant: DateTime) { foo(instant: $instant) }");
then(fixture.variables()).isEqualTo("{'instant':'" + in + "'}");
then(value).isEqualTo(out);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class SchemaBuilder {
private final InputTypeCreator inputTypeCreator;
private final TypeCreator typeCreator;
private final FieldCreator fieldCreator;
private final ArgumentCreator argumentCreator;
private final InterfaceCreator interfaceCreator;
private final EnumCreator enumCreator;
private final ReferenceCreator referenceCreator;
Expand Down Expand Up @@ -93,8 +94,7 @@ private SchemaBuilder(TypeAutoNameStrategy autoNameStrategy) {
enumCreator = new EnumCreator(autoNameStrategy);
referenceCreator = new ReferenceCreator(autoNameStrategy);
fieldCreator = new FieldCreator(referenceCreator);
ArgumentCreator argumentCreator = new ArgumentCreator(referenceCreator);

argumentCreator = new ArgumentCreator(referenceCreator);
inputTypeCreator = new InputTypeCreator(fieldCreator);
operationCreator = new OperationCreator(referenceCreator, argumentCreator);
typeCreator = new TypeCreator(referenceCreator, fieldCreator, operationCreator);
Expand Down Expand Up @@ -150,9 +150,13 @@ private void addDirectiveTypes(Schema schema) {
}

private void setupDirectives(Directives directives) {
inputTypeCreator.setDirectives(directives);
typeCreator.setDirectives(directives);
interfaceCreator.setDirectives(directives);
enumCreator.setDirectives(directives);
fieldCreator.setDirectives(directives);
argumentCreator.setDirectives(directives);
operationCreator.setDirectives(directives);
}

private void addTypesToSchema(Schema schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import io.smallrye.graphql.schema.Annotations;
import io.smallrye.graphql.schema.helper.DescriptionHelper;
import io.smallrye.graphql.schema.helper.Direction;
import io.smallrye.graphql.schema.helper.Directives;
import io.smallrye.graphql.schema.helper.IgnoreHelper;
import io.smallrye.graphql.schema.helper.TypeAutoNameStrategy;
import io.smallrye.graphql.schema.helper.TypeNameHelper;
import io.smallrye.graphql.schema.model.DirectiveInstance;
import io.smallrye.graphql.schema.model.EnumType;
import io.smallrye.graphql.schema.model.EnumValue;
import io.smallrye.graphql.schema.model.Reference;
Expand All @@ -28,11 +30,16 @@ public class EnumCreator implements Creator<EnumType> {
private static final Logger LOG = Logger.getLogger(EnumCreator.class.getName());

private final TypeAutoNameStrategy autoNameStrategy;
private Directives directives;

public EnumCreator(TypeAutoNameStrategy autoNameStrategy) {
this.autoNameStrategy = autoNameStrategy;
}

public void setDirectives(Directives directives) {
this.directives = directives;
}

@Override
public EnumType create(ClassInfo classInfo, Reference reference) {
LOG.debug("Creating enum from " + classInfo.name().toString());
Expand All @@ -51,6 +58,9 @@ public EnumType create(ClassInfo classInfo, Reference reference) {

EnumType enumType = new EnumType(classInfo.name().toString(), name, maybeDescription.orElse(null));

// Directives
enumType.setDirectiveInstances(getDirectiveInstances(annotations));

// Values
List<FieldInfo> fields = classInfo.fields();
for (FieldInfo field : fields) {
Expand All @@ -59,12 +69,16 @@ public EnumType create(ClassInfo classInfo, Reference reference) {
if (!field.type().kind().equals(Type.Kind.ARRAY) && !IgnoreHelper.shouldIgnore(annotationsForField, field)) {
String description = annotationsForField.getOneOfTheseAnnotationsValue(Annotations.DESCRIPTION)
.orElse(null);
enumType.addValue(new EnumValue(description, field.name()));
enumType.addValue(new EnumValue(description, field.name(), getDirectiveInstances(annotationsForField)));
}
}
}

return enumType;
}

private List<DirectiveInstance> getDirectiveInstances(Annotations annotations) {
return directives.buildDirectiveInstances(dotName -> annotations.getOneOfTheseAnnotations(dotName).orElse(null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import io.smallrye.graphql.schema.creator.FieldCreator;
import io.smallrye.graphql.schema.helper.DescriptionHelper;
import io.smallrye.graphql.schema.helper.Direction;
import io.smallrye.graphql.schema.helper.Directives;
import io.smallrye.graphql.schema.helper.MethodHelper;
import io.smallrye.graphql.schema.helper.TypeNameHelper;
import io.smallrye.graphql.schema.model.DirectiveInstance;
import io.smallrye.graphql.schema.model.Field;
import io.smallrye.graphql.schema.model.InputType;
import io.smallrye.graphql.schema.model.Reference;
Expand All @@ -37,6 +39,7 @@ public class InputTypeCreator implements Creator<InputType> {
private static final Logger LOG = Logger.getLogger(InputTypeCreator.class.getName());

private final FieldCreator fieldCreator;
private Directives directives;

public InputTypeCreator(FieldCreator fieldCreator) {
this.fieldCreator = fieldCreator;
Expand Down Expand Up @@ -66,6 +69,9 @@ public InputType create(ClassInfo classInfo, Reference reference) {

InputType inputType = new InputType(classInfo.name().toString(), name, description);

// Directives
inputType.setDirectiveInstances(getDirectiveInstances(annotations));

// Fields
addFields(inputType, classInfo, reference);

Expand Down Expand Up @@ -127,6 +133,14 @@ public MethodInfo findCreator(ClassInfo classInfo) {
return null;
}

public void setDirectives(Directives directives) {
this.directives = directives;
}

private List<DirectiveInstance> getDirectiveInstances(Annotations annotations) {
return directives.buildDirectiveInstances(dotName -> annotations.getOneOfTheseAnnotations(dotName).orElse(null));
}

private void addFields(InputType inputType, ClassInfo classInfo, Reference reference) {
// Fields
List<MethodInfo> allMethods = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.smallrye.graphql.schema.model;

import java.util.Collection;
import java.util.List;

/**
* Represents one of an enum's values. Is part of {@link EnumType}.
*
Expand All @@ -9,13 +12,15 @@ public final class EnumValue {

private String description;
private String value;
private List<DirectiveInstance> directiveInstances;

public EnumValue() {
}

public EnumValue(String description, String value) {
public EnumValue(String description, String value, List<DirectiveInstance> directiveInstances) {
this.description = description;
this.value = value;
this.directiveInstances = directiveInstances;
}

public String getDescription() {
Expand All @@ -36,6 +41,19 @@ public void setValue(String value) {

@Override
public String toString() {
return "EnumValue{" + "description=" + description + ", value=" + value + '}';
return "EnumValue{" +
"description=" + description +
", value=" + value +
", directiveInstances=" + directiveInstances +
'}';
}

public boolean hasDirectiveInstances() {
return directiveInstances != null && !directiveInstances.isEmpty();
}

public Collection<DirectiveInstance> getDirectiveInstances() {
return directiveInstances;
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package io.smallrye.graphql.cdi.event;

import io.smallrye.graphql.api.Context;

/**
* Simple Pojo that hold error info
*
* @author Phillip Kruger ([email protected])
*/
public class ErrorInfo {
private String executionId;
private Throwable t;
private final Context context;
private final Throwable t;

public ErrorInfo(String executionId, Throwable t) {
this.executionId = executionId;
ErrorInfo(Context context, Throwable t) {
this.context = context;
this.t = t;
}

public Context getContext() {
return context;
}

public String getExecutionId() {
return executionId;
return context.getExecutionId();
}

public Throwable getT() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public void afterDataFetch(Context context) {
}

@Override
public void errorExecute(String executionId, Throwable t) {
fire(new ErrorInfo(executionId, t), ErrorExecute.LITERAL);
public void errorExecute(Context context, Throwable t) {
fire(new ErrorInfo(context, t), ErrorExecute.LITERAL);
}

@Override
public void errorDataFetch(String executionId, Throwable t) {
fire(new ErrorInfo(executionId, t), ErrorDataFetch.LITERAL);
public void errorDataFetch(Context context, Throwable t) {
fire(new ErrorInfo(context, t), ErrorDataFetch.LITERAL);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public void afterExecute(Context context) {
}

@Override
public void errorExecute(String executionId, Throwable t) {
Span span = spans.remove(executionId);
public void errorExecute(Context context, Throwable t) {
Span span = spans.remove(context.getExecutionId());
if (span != null) {
Map<String, Object> error = new HashMap<>();
error.put("event.object", t);
Expand Down Expand Up @@ -117,8 +117,8 @@ public void afterDataFetch(Context context) {
}

@Override
public void errorDataFetch(String executionId, Throwable t) {
Span span = spans.get(executionId);
public void errorDataFetch(Context context, Throwable t) {
Span span = spans.get(context.getExecutionId());
if (span != null) {
logError(span, t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import graphql.schema.GraphQLCodeRegistry;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLEnumType;
import graphql.schema.GraphQLEnumValueDefinition;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLInputObjectType;
Expand Down Expand Up @@ -339,9 +340,20 @@ private void createGraphQLEnumType(EnumType enumType) {
GraphQLEnumType.Builder enumBuilder = GraphQLEnumType.newEnum()
.name(enumType.getName())
.description(enumType.getDescription());
// Directives
if (enumType.hasDirectiveInstances()) {
enumBuilder = enumBuilder.withDirectives(createGraphQLDirectives(enumType.getDirectiveInstances()));
}
// Values
for (EnumValue value : enumType.getValues()) {
enumBuilder = enumBuilder.value(value.getValue(), value.getValue(), value.getDescription());
GraphQLEnumValueDefinition.Builder definitionBuilder = GraphQLEnumValueDefinition.newEnumValueDefinition()
.name(value.getValue())
.value(value.getValue())
.description(value.getDescription());
if (value.hasDirectiveInstances()) {
definitionBuilder = definitionBuilder.withDirectives(createGraphQLDirectives(value.getDirectiveInstances()));
}
enumBuilder = enumBuilder.value(definitionBuilder.build());
}
GraphQLEnumType graphQLEnumType = enumBuilder.build();
enumMap.put(enumType.getClassName(), graphQLEnumType);
Expand All @@ -367,6 +379,13 @@ private void createGraphQLInterfaceType(Type interfaceType) {
interfaceType.getFields().values()));
}

// Directives
if (interfaceType.hasDirectiveInstances()) {
for (DirectiveInstance directiveInstance : interfaceType.getDirectiveInstances()) {
interfaceTypeBuilder.withDirective(createGraphQLDirectiveFrom(directiveInstance));
}
}

// Interfaces
if (interfaceType.hasInterfaces()) {
Set<Reference> interfaces = interfaceType.getInterfaces();
Expand Down Expand Up @@ -450,6 +469,12 @@ private GraphQLInputObjectType createGraphQLInputObjectType(InputType inputType)
.name(inputType.getName())
.description(inputType.getDescription());

// Directives
if (inputType.hasDirectiveInstances()) {
inputObjectTypeBuilder = inputObjectTypeBuilder
.withDirectives(createGraphQLDirectives(inputType.getDirectiveInstances()));
}

// Fields
if (inputType.hasFields()) {
inputObjectTypeBuilder = inputObjectTypeBuilder
Expand Down Expand Up @@ -600,6 +625,11 @@ private GraphQLFieldDefinition createGraphQLFieldDefinitionFromOperation(String
fieldBuilder.argument(autoMapArgument.get());
}

// Directives
if (operation.hasDirectiveInstances()) {
fieldBuilder = fieldBuilder.withDirectives(createGraphQLDirectives(operation.getDirectiveInstances()));
}

GraphQLFieldDefinition graphQLFieldDefinition = fieldBuilder.build();

// DataFetcher
Expand All @@ -611,6 +641,12 @@ private GraphQLFieldDefinition createGraphQLFieldDefinitionFromOperation(String
return graphQLFieldDefinition;
}

private GraphQLDirective[] createGraphQLDirectives(Collection<DirectiveInstance> directiveInstances) {
return directiveInstances.stream()
.map(this::createGraphQLDirectiveFrom)
.toArray(GraphQLDirective[]::new);
}

private List<GraphQLFieldDefinition> createGraphQLFieldDefinitionsFromFields(Reference owner, Collection<Field> fields) {
List<GraphQLFieldDefinition> graphQLFieldDefinitions = new ArrayList<>();
for (Field field : fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void execute(JsonObject jsonInput, Map<String, Object> context, Execution
log.noGraphQLMethodsFound();
}
} catch (Throwable t) {
eventEmitter.fireOnExecuteError(finalExecutionId.toString(), t);
eventEmitter.fireOnExecuteError(smallRyeContext, t);
writer.fail(t);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import graphql.execution.DataFetcherResult;
import graphql.schema.DataFetchingEnvironment;
import io.smallrye.graphql.SmallRyeGraphQLServerMessages;
import io.smallrye.graphql.api.Context;
import io.smallrye.graphql.schema.model.Operation;
import io.smallrye.graphql.schema.model.Type;
import io.smallrye.graphql.transformation.AbstractDataFetcherException;
Expand All @@ -30,6 +31,7 @@ public AbstractAsyncDataFetcher(Operation operation, Type type) {
@Override
@SuppressWarnings("unchecked")
protected <O> O invokeAndTransform(
Context context,
DataFetchingEnvironment dfe,
DataFetcherResult.Builder<Object> resultBuilder,
Object[] transformedArguments) throws Exception {
Expand All @@ -39,7 +41,7 @@ protected <O> O invokeAndTransform(
.onItemOrFailure()
.transformToUni((result, throwable, emitter) -> {
if (throwable != null) {
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), throwable);
eventEmitter.fireOnDataFetchError(context, throwable);
if (throwable instanceof GraphQLException) {
GraphQLException graphQLException = (GraphQLException) throwable;
errorResultHelper.appendPartialResult(resultBuilder, dfe, graphQLException);
Expand Down
Loading

0 comments on commit b03a1ca

Please sign in to comment.