|
| 1 | +package graphql.kickstart.tools; |
| 2 | + |
| 3 | +import graphql.ExecutionInput; |
| 4 | +import graphql.ExecutionResult; |
| 5 | +import graphql.GraphQL; |
| 6 | +import graphql.schema.GraphQLSchema; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import static org.junit.Assert.assertEquals; |
| 12 | +import static org.junit.Assert.assertTrue; |
| 13 | + |
| 14 | +public class ResolverMethodsTest { |
| 15 | + // Note: don't convert this code to Kotlin or Groovy, since it's quite important that the |
| 16 | + // resolver method is defined with an argument of primitive type, like 'boolean', not 'Boolean': |
| 17 | + // String testOmittedBoolean(boolean value1, Boolean value2) |
| 18 | + @Test |
| 19 | + public void testOmittedBooleanArgument() { |
| 20 | + // In this schema, the 'value1' argument is optional, but the Java resolver defines it as 'boolean' |
| 21 | + // Instead of failing with an error, we expect the argument to be set to the Java default (i.e. false for booleans) |
| 22 | + GraphQLSchema schema = SchemaParser.newParser() |
| 23 | + .schemaString("" + |
| 24 | + "type Query {" + |
| 25 | + " testOmittedBoolean(value1: Boolean, value2: Boolean): String" + |
| 26 | + "}") |
| 27 | + .resolvers(new Resolver()) |
| 28 | + .build() |
| 29 | + .makeExecutableSchema(); |
| 30 | + |
| 31 | + GraphQL gql = GraphQL.newGraphQL(schema).build(); |
| 32 | + |
| 33 | + ExecutionResult result = gql |
| 34 | + .execute(ExecutionInput.newExecutionInput() |
| 35 | + .query("" + |
| 36 | + "query { " + |
| 37 | + " testOmittedBoolean" + |
| 38 | + "}") |
| 39 | + .context(new Object()) |
| 40 | + .root(new Object())); |
| 41 | + |
| 42 | + assertTrue(result.getErrors().isEmpty()); |
| 43 | + assertEquals("false,null", ((Map<?, ?>) result.getData()).get("testOmittedBoolean")); |
| 44 | + } |
| 45 | + |
| 46 | + static class Resolver implements GraphQLQueryResolver { |
| 47 | + @SuppressWarnings("unused") |
| 48 | + public String testOmittedBoolean(boolean value1, Boolean value2) { |
| 49 | + return value1 + "," + value2; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments