/** * Creates a new schema metadata instance initialized with the provided additional metadata. * * @param metadata the additional custom metadata to pass to the service proxy * @return the schema metadata instance */ public static SchemaMetadata create(JsonObject metadata) { return new SchemaMetadata(metadata); }
public static SchemaDefinition createInstance(GraphQLSchema schema, SchemaMetadata metadata) { return new SchemaDefinition(schema, metadata); }
/** * Executes the GraphQL query on the GraphQL schema proxy. * <p> * On success a {@link QueryResult} is returned. While this indicates the query executor has finished processing, * the query itself might still have failed, so be sure to check {@link QueryResult#isSucceeded()} and * {@link QueryResult#getErrors()} properties on the query result afterwards. * afterwards. * * @param graphqlQuery the graphql query * @param resultHandler the result handler with the query result on success, or a failure */ @Override public void query(String graphqlQuery, Handler<AsyncResult<QueryResult>> resultHandler) { queryWithVariables(graphqlQuery, null, resultHandler); }
@Before public void setUp() { vertx = Vertx.vertx(); options = new ServiceDiscoveryOptions().setName("theDiscovery") .setAnnounceAddress("theAnnounceAddress").setUsageAddress("theUsageAddress"); discovery = ServiceDiscovery.create(vertx, options); record = new Record() .setName("theRecord") .setType(Queryable.SERVICE_TYPE) .setMetadata(new JsonObject().put("publisherId", "thePublisherId")) .setLocation(new JsonObject().put(Record.ENDPOINT, Queryable.ADDRESS_PREFIX + ".DroidQueries")) .setStatus(Status.UP); definition = SchemaDefinition.createInstance(droidsSchema, SchemaMetadata.create(new JsonObject().put("publisherId", "thePublisherId"))); consumer = ProxyHelper.registerService(Queryable.class, vertx, definition, Queryable.ADDRESS_PREFIX + ".DroidQueries"); }
protected SchemaDefinition(GraphQLSchema schema, SchemaMetadata metadata) { this.schema = schema; this.schemaMetadata = metadata == null ? SchemaMetadata.create() : metadata; this.schemaName = schemaMetadata.getSchemaName() == null || schemaMetadata.getSchemaName().isEmpty() ? schema.getQueryType().getName() : schemaMetadata.getSchemaName(); this.serviceAddress = schemaMetadata.getServiceAddress() == null || schemaMetadata.getServiceAddress().isEmpty() ? Queryable.ADDRESS_PREFIX + "." + schemaName() : schemaMetadata.getServiceAddress(); schemaMetadata.put(SchemaMetadata.METADATA_QUERIES, schema.getQueryType().getFieldDefinitions().stream() .map(GraphQLFieldDefinition::getName).collect(Collectors.toList())); schemaMetadata.put(SchemaMetadata.METADATA_MUTATIONS, !schema.isSupportingMutations() ? Collections.emptyList() : schema.getMutationType().getFieldDefinitions().stream() .map(GraphQLFieldDefinition::getName).collect(Collectors.toList())); }
Objects.requireNonNull(resultHandler, "Publication result handler cannot be null"); SchemaDefinition definition = SchemaDefinition.createInstance(schema, metadata); if (schemaRegistrar().findRegistration(options.getName(), definition.schemaName()).isPresent()) { resultHandler.handle(Future.failedFuture("Schema '" + definition.schemaName() + "' was already published to: " + options.getName())); return; metadata.put("publisherId", schemaRegistrar().getPublisherId()); ServiceDiscovery discovery = schemaRegistrar().getOrCreateDiscovery(options);
if (definition.metadata().get("publisherId") == null) { serviceConsumer = ProxyHelper.registerService( Queryable.class, vertx, definition, definition.serviceAddress()); } else { .setName(definition.schemaName()) .setMetadata(definition.metadata().toJson()) .setLocation(new JsonObject().put(Record.ENDPOINT, definition.serviceAddress()));
/** * Publishes the schema definition to the service discovery indicated by the provided schema publisher options. * * @param options the service discovery options * @param schema the GraphQL schema to publish * @param resultHandler the result handler */ default void publish(ServiceDiscoveryOptions options, GraphQLSchema schema, Handler<AsyncResult<SchemaRegistration>> resultHandler) { publish(options, schema, SchemaMetadata.create(), resultHandler); }
/** * Executes a blocking call to the GraphQL query processor and executes the query. * * @param graphqlQuery the graphql query * @param variables the variables to pass to the query * @return the graphql query result */ public QueryResult queryBlocking(String graphqlQuery, JsonObject variables) { Objects.requireNonNull(graphqlQuery, "GraphQL query cannot be null"); GraphQL graphQL = new GraphQL(schema()); ExecutionResult result; if (variables == null) { result = graphQL.execute(graphqlQuery); } else { result = graphQL.execute(graphqlQuery, (Object) null, variables.getMap()); } return convertToQueryResult(result); }
@Override public void accept(ExecutionResult result) { try { QueryResult queryResult = convertToQueryResult(result); resultHandler.handle(Future.succeededFuture(queryResult)); } catch (Exception e) { resultHandler.handle(Future.failedFuture(e)); } } });
/** * Publish a GraphQL schema for querying. * <p> * On success a {@link SchemaRegistration} is returned. It contains the message consumer of the * {@link Queryable} service proxy that supplies the published {@link SchemaDefinition}, the published service * discovery record, and the {@link ServiceDiscovery} it was published to. * <p> * Note that unless invoked from a {@link SchemaPublisher} a * client needs to keep hold of the returned {@link Record} as long as it is published. * * @param vertx the vert.x instance * @param discovery the service discovery instance * @param schema the graphql schema * @param options the schema discovery options * @param metadata the metadata to pass to the service * @param resultHandler the result handler that returns the registration */ static void publish(Vertx vertx, ServiceDiscovery discovery, GraphQLSchema schema, ServiceDiscoveryOptions options, SchemaMetadata metadata, Handler<AsyncResult<SchemaRegistration>> resultHandler) { Objects.requireNonNull(schema, "GraphQL schema cannot be null"); SchemaDefinition definition = SchemaDefinition.createInstance(schema, metadata); publish(vertx, discovery, definition, resultHandler); }
private static boolean schemaEquals(SchemaDefinition schema1, SchemaDefinition schema2) { if (schema1 == null) { return schema2 == null; } if (schema1.schema() == null) { return schema2.schema() == null; } if (schema2 != null && schema2.schema() != null) { return schema1.schema().getQueryType().getName().equals(schema2.schema().getQueryType().getName()); } return false; }
/** * Executes the GraphQL query on the GraphQL schema proxy using the provided variables. * * @param graphqlQuery the graphql query * @param resultHandler the result handler with the graphql query result on success, or a failure */ @Override public void queryWithVariables(String graphqlQuery, JsonObject variables, Handler<AsyncResult<QueryResult>> resultHandler) { try { queryNonBlocking(graphqlQuery, variables, resultHandler); } catch (RuntimeException ex) { resultHandler.handle(Future.failedFuture(ex)); } }
@Test public void should_Manage_Service_Consumers() { SchemaDefinition definition = SchemaDefinition .createInstance(droidsSchema, SchemaMetadata.create()); messageConsumers.registerServiceConsumer("DroidQueries", definition); assertEquals(1, messageConsumers.getConsumers().size()); assertTrue(messageConsumers.getConsumers().get("DroidQueries").isRegistered()); messageConsumers.registerServiceConsumer("DroidQueries", definition); assertEquals(1, messageConsumers.getConsumers().size()); messageConsumers.unregisterConsumer("DroidQueries"); assertEquals(1, messageConsumers.getConsumers().size()); messageConsumers.registerConsumer("announce", (SchemaAnnounceHandler) rh -> {}); assertEquals(2, messageConsumers.getConsumers().size()); messageConsumers.unregisterConsumer("DroidQueries"); assertEquals(1, messageConsumers.getConsumers().size()); messageConsumers.close(); assertEquals(0, messageConsumers.getConsumers().size()); }
@Test public void should_Publish_Schema_Definition_With_Metadata(TestContext context) { Async async = context.async(); SchemaMetadata metadata = SchemaMetadata.create(new JsonObject() .put("someString", "someValue").put("someObject", new JsonObject())); GraphQLService.publish(vertx, discovery, starWarsSchema, options, metadata, rh -> { context.assertTrue(rh.succeeded()); Record record = rh.result().getRecord(); context.assertEquals("someValue", record.getMetadata().getString("someString")); context.assertEquals(new JsonObject(), record.getMetadata().getJsonObject("someObject")); // Check standard metadata that is added (for future use) JsonArray queries = record.getMetadata().getJsonArray(SchemaRegistration.METADATA_QUERIES); JsonArray mutations = record.getMetadata().getJsonArray(SchemaRegistration.METADATA_MUTATIONS); context.assertEquals(new JsonArray("[ \"hero\", \"human\" ]"), queries); context.assertEquals(new JsonArray(), mutations); async.complete(); }); }
@Test public void should_Create_Query_Result_From_Failed_Execution_Result() { // given final ExecutionResult input = QUERY_RESULT_FAILURE; // when QueryResult result = new QueryResult(new QueryResult(SchemaDefinition.convertToQueryResult(input).toJson())); // then assertEquals(result.getData(), new JsonObject()); assertEquals(result, result); assertNotEquals(result, "test"); assertFalse(result.isSucceeded()); assertNotNull(result.getErrors()); assertEquals(2, result.getErrors().size()); assertEquals(result.getErrors().get(0), result.getErrors().get(0)); assertNotEquals(result.getErrors().get(0), "test"); assertEquals("ValidationError", result.getErrors().get(0).getErrorType()); assertEquals("Validation error of type UnknownType: Error1", result.getErrors().get(0).getMessage()); assertNotNull(result.getErrors().get(0).getLocations()); assertNotNull(result.getErrors().get(1).getLocations()); assertEquals(2, result.getErrors().get(1).getLocations().size()); assertEquals(1, result.getErrors().get(0).getLocations().get(0).getLine()); assertEquals(3, result.getErrors().get(1).getLocations().get(1).getColumn()); assertEquals(EXPECTED_FAILURE, result); assertEquals(EXPECTED_FAILURE.hashCode(), result.hashCode()); }
Objects.requireNonNull(graphqlQuery, "GraphQL query cannot be null"); GraphQL graphQL = new GraphQL.Builder(schema()).build();
/** * Creates a new schema metadata instance. * * @return the schema metadata instance */ public static SchemaMetadata create() { return new SchemaMetadata(new JsonObject()); }
@Test public void should_Implement_Proper_Reference_Equality() { SchemaRegistration registration1 = SchemaRegistration.create(discovery, options, record, definition, null); assertEquals(registration1, registration1); assertNotEquals(registration1, "test"); SchemaRegistration registration2 = SchemaRegistration.create(discovery, new ServiceDiscoveryOptions(options).setName("theOtherDiscovery"), record, definition, null); assertNotEquals(registration1, registration2); SchemaRegistration registration3 = SchemaRegistration.create(discovery, new ServiceDiscoveryOptions(options).setName("theOtherDiscovery"), record, definition, consumer); assertNotEquals(registration2, registration3); SchemaDefinition starwarsSchema = SchemaDefinition .createInstance(starWarsSchema, SchemaMetadata.create()); SchemaRegistration registration4 = SchemaRegistration.create(discovery, options, record, starwarsSchema, null); assertNotEquals(registration1, registration4); SchemaRegistration registration5 = SchemaRegistration. create(discovery, options, new Record(record).setName("theOtherRecord"), starwarsSchema, null); assertNotEquals(registration4, registration5); SchemaRegistration registration6 = SchemaRegistration. create(discovery, options, new Record(record).setStatus(Status.DOWN), starwarsSchema, null); assertEquals(registration4, registration6); }
@Test public void should_Create_Query_Result_From_Succeeded_Execution_Result() { // given final ExecutionResult input = QUERY_RESULT_SUCCESS; // when QueryResult result = new QueryResult(new QueryResult( new QueryResult(SchemaDefinition.convertToQueryResult(input)).toJson())); // then assertNotNull(result.getData()); assertEquals(true, result.getData().getBoolean("query-data")); assertTrue(result.isSucceeded()); assertNotNull(result.getErrors()); assertEquals(0, result.getErrors().size()); assertEquals(EXPECTED_SUCCESS, result); assertEquals(EXPECTED_SUCCESS.hashCode(), result.hashCode()); }