Skip to content

Commit

Permalink
feat: add support for ARGUMENT_DEFINITION (#119)
Browse files Browse the repository at this point in the history
* feat: add support for ARGUMENT_DEFINITION

* feat: add support for ARGUMENT_DEFINITION

* feat: add support for ARGUMENT_DEFINITION

* feat: add support for ARGUMENT_DEFINITION

* feat: add support for ARGUMENT_DEFINITION

* revert changes
  • Loading branch information
setchy authored Jun 9, 2023
1 parent d4195b6 commit 8bb71af
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 33 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ Abbreviates a string using ellipses for a given width

Converts the String into camelCase

- SDL: `directive @camelcase on FIELD_DEFINITION`
- SDL: `directive @camelcase on FIELD_DEFINITION | ARGUMENT_DEFINITION`

### @capitalize

Capitalize the starting letter for each word in a String

- SDL: `directive @capitalize on FIELD_DEFINITION`
- SDL: `directive @capitalize on FIELD_DEFINITION | ARGUMENT_DEFINITION`

### @lowercase

Lowercase all characters in a String

- SDL: `directive @lowercase on FIELD_DEFINITION`
- SDL: `directive @lowercase on FIELD_DEFINITION | ARGUMENT_DEFINITION`

### @prefix

Expand All @@ -62,7 +62,7 @@ which will be encoded into the ID.

Reverse the characters in a String

- SDL: `directive @reverse on FIELD_DEFINITION`
- SDL: `directive @reverse on FIELD_DEFINITION | ARGUMENT_DEFINITION`

### @suffix

Expand All @@ -74,19 +74,19 @@ Appends a suffix to a String

Invert the case of each character in a String

- SDL: `directive @swapcase on FIELD_DEFINITION`
- SDL: `directive @swapcase on FIELD_DEFINITION | ARGUMENT_DEFINITION`

### @trim

Remove any leading or trailing whitespace

- SDL: `directive @trim on FIELD_DEFINITION`
- SDL: `directive @trim on FIELD_DEFINITION | ARGUMENT_DEFINITION`

### @uppercase

Uppercase each character in a String

- SDL: `directive @uppercase on FIELD_DEFINITION`
- SDL: `directive @uppercase on FIELD_DEFINITION | ARGUMENT_DEFINITION`

## Numeric Formatters

Expand All @@ -96,16 +96,16 @@ The following schema directives support formatting `Int` or `Float` scalars

Returns the absolute value

- SDL: `directive @absolute on FIELD_DEFINITION`
- SDL: `directive @absolute on FIELD_DEFINITION | ARGUMENT_DEFINITION`

### @ceiling

Returns the ceiling value

- SDL: `directive @ceiling on FIELD_DEFINITION`
- SDL: `directive @ceiling on FIELD_DEFINITION | ARGUMENT_DEFINITION`

### @floor

Returns the floor value

- SDL: `directive @floor on FIELD_DEFINITION`
- SDL: `directive @floor on FIELD_DEFINITION | ARGUMENT_DEFINITION`
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
package io.github.setchy.dgs.formatters;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetcherFactories;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLFieldsContainer;
import graphql.schema.*;
import graphql.schema.idl.SchemaDirectiveWiring;
import graphql.schema.idl.SchemaDirectiveWiringEnvironment;

public abstract class AbstractFormatterDirective implements SchemaDirectiveWiring {

@Override
public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> env) {
GraphQLFieldDefinition field = env.getElement();
return registerDataFetcher(env);
}

@Override
public GraphQLArgument onArgument(SchemaDirectiveWiringEnvironment<GraphQLArgument> env) {
return registerDataFetcher(env);
}

private <T extends GraphQLDirectiveContainer> T registerDataFetcher(SchemaDirectiveWiringEnvironment<T> env) {
GraphQLFieldDefinition field = env.getFieldDefinition();
GraphQLFieldsContainer fieldsContainer = env.getFieldsContainer();
DataFetcher<?> originalDataFetcher = env.getFieldDataFetcher();

DataFetcher<?> dataFetcher =
DataFetcherFactories.wrapDataFetcher(originalDataFetcher, ((dataFetchingEnvironment, value) -> format(field, value)));

env.getCodeRegistry()
.dataFetcher(fieldsContainer, field, dataFetcher);
return field;
env.getCodeRegistry().dataFetcher(fieldsContainer, field, dataFetcher);
return env.getElement();
}

public abstract Object format(GraphQLFieldDefinition field, Object value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ class DirectivesIntegrationTest {
private static GraphQLSchema buildSchema() {
String sdlSpec = """
directive @absolute on FIELD_DEFINITION
directive @uppercase on FIELD_DEFINITION
directive @uppercase on FIELD_DEFINITION | ARGUMENT_DEFINITION
type Query {
helloWorld : String
hello(arg: String @uppercase) : String
helloWorldShouting : String @uppercase
negativeNumber: Int
absoluteNumber: Int @absolute
Expand All @@ -48,13 +49,15 @@ void testAbstractDirectiveCallsFormat() {
GraphQL graphql = GraphQL.newGraphQL(schema).build();

Map<String, Object> root = new HashMap<>();
root.put("hello", "world");
root.put("helloWorld", "Hello World");
root.put("helloWorldShouting", "Hello World");
root.put("negativeNumber", -100);
root.put("absoluteNumber", -100);

String query = """
query {
hello(arg: "world")
helloWorld
helloWorldShouting
negativeNumber
Expand All @@ -69,6 +72,7 @@ void testAbstractDirectiveCallsFormat() {
ExecutionResult executionResult = graphql.execute(executionInput);
Map<String, Object> data = executionResult.getData();

assertEquals("WORLD", data.get("hello"));
assertEquals("Hello World", data.get("helloWorld"));
assertEquals("HELLO WORLD", data.get("helloWorldShouting"));
assertEquals(-100, data.get("negativeNumber"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsData;
import com.netflix.graphql.dgs.DgsDataFetchingEnvironment;

@DgsComponent
public class FormattedIntegerExamplesDataFetcher {
Expand All @@ -13,7 +12,7 @@ public class FormattedIntegerExamplesDataFetcher {
@DgsData(parentType = "FormattedIntegerExamples", field = "absolute")
@DgsData(parentType = "FormattedIntegerExamples", field = "ceiling")
@DgsData(parentType = "FormattedIntegerExamples", field = "floored")
public Integer exampleData(DgsDataFetchingEnvironment dfe) {
public Integer exampleData() {
return SAMPLE_DATA;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsData;
import com.netflix.graphql.dgs.DgsDataFetchingEnvironment;

@DgsComponent
public class FormattedStringExamplesDataFetcher {
Expand All @@ -22,7 +21,7 @@ public class FormattedStringExamplesDataFetcher {
@DgsData(parentType = "FormattedStringExamples", field = "resourceId")
@DgsData(parentType = "FormattedStringExamples", field = "prefixed")
@DgsData(parentType = "FormattedStringExamples", field = "suffixed")
public String exampleData(DgsDataFetchingEnvironment dfe) {
public String exampleData() {
return SAMPLE_DATA;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public FormattedStringExamples formattedStrings() {
return new FormattedStringExamples();
}

@DgsQuery
public FormattedArgsExamples formattedArgs() {
return new FormattedArgsExamples();
}

@DgsQuery
public FormattedIntegerExamples formattedIntegers() {
return new FormattedIntegerExamples();
Expand All @@ -24,6 +29,9 @@ public FormattedFloatExamples formattedFloats() {
public static class FormattedStringExamples {
}

public static class FormattedArgsExamples {
}

public static class FormattedIntegerExamples {
}

Expand Down
40 changes: 30 additions & 10 deletions samples/src/main/resources/schema/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
# String Formatters
directive @uppercase on FIELD_DEFINITION
directive @lowercase on FIELD_DEFINITION
directive @trim on FIELD_DEFINITION
directive @capitalize on FIELD_DEFINITION
directive @reverse on FIELD_DEFINITION
directive @swapcase on FIELD_DEFINITION
directive @uppercase on FIELD_DEFINITION | ARGUMENT_DEFINITION
directive @lowercase on FIELD_DEFINITION | ARGUMENT_DEFINITION
directive @trim on FIELD_DEFINITION | ARGUMENT_DEFINITION
directive @capitalize on FIELD_DEFINITION | ARGUMENT_DEFINITION
directive @reverse on FIELD_DEFINITION | ARGUMENT_DEFINITION
directive @swapcase on FIELD_DEFINITION | ARGUMENT_DEFINITION
directive @abbreviate(width: Int!) on FIELD_DEFINITION
directive @camelcase on FIELD_DEFINITION
directive @camelcase on FIELD_DEFINITION | ARGUMENT_DEFINITION
directive @resourceId(domain: String!, subdomain: String!, systemName: String!) on FIELD_DEFINITION
directive @prefix(with: String!) on FIELD_DEFINITION
directive @suffix(with: String!) on FIELD_DEFINITION

# Numeric Formatters
directive @absolute on FIELD_DEFINITION
directive @ceiling on FIELD_DEFINITION
directive @floor on FIELD_DEFINITION
directive @absolute on FIELD_DEFINITION | ARGUMENT_DEFINITION
directive @ceiling on FIELD_DEFINITION | ARGUMENT_DEFINITION
directive @floor on FIELD_DEFINITION | ARGUMENT_DEFINITION

type Query {
formattedStrings: FormattedStringExamples
formattedArgs: FormattedArgsExamples
formattedIntegers: FormattedIntegerExamples
formattedFloats: FormattedFloatExamples
}
Expand All @@ -38,6 +39,25 @@ type FormattedStringExamples {
suffixed: String @suffix(with: "- suffix")
}

type FormattedArgsExamples {
original(arg: String!): String
uppercased(arg: String! @uppercase): String
lowercased(arg: String! @lowercase): String
trimmed(arg: String! @trim): String
trimmedUppercased(arg: String! @uppercase @trim): String
capitalized(arg: String! @capitalize): String
reversed(arg: String! @reverse): String
swapcased(arg: String! @swapcase): String
camelcased(arg: String! @camelcase): String
absoluteInt(arg: Int! @absolute): Int
ceilingInt(arg: Int! @ceiling): Int
flooredInt(arg: Int! @floor): Int
absoluteFloat(arg: Float! @absolute): Float
ceilingFloat(arg: Float! @ceiling): Float
flooredFloat(arg: Float! @floor): Float

}

type FormattedIntegerExamples {
original: Int
absolute: Int @absolute
Expand Down

0 comments on commit 8bb71af

Please sign in to comment.