GraphQLEndpoint { @POST @Consumes(MediaType.APPLICATION_JSON) public Response post( @Context UriInfo uriInfo, @Context SecurityContext securityContext, String graphQLDocument) { ElideResponse response = runner.run(uriInfo.getBaseUri().toString(), graphQLDocument, getUser.apply(securityContext)); return Response.status(response.getResponseCode()).entity(response.getBody()).build(); } @Inject  GraphQLEndpoint(
            @Named("elide") Elide elide,
            @Named("elideUserExtractionFunction") DefaultOpaqueUserFunction getUser); @POST @Consumes(MediaType.APPLICATION_JSON) Response post(
            @Context UriInfo uriInfo,
            @Context SecurityContext securityContext,
            String graphQLDocument);  }
@Test public void testValidFetch() throws JSONException { String graphQLRequest = document( selections( field( "book", selections( field("id"), field("title"), field( "authors", selection( field("name") ) ) ) ) ) ).toQuery(); String graphQLResponse = document( selection( field( "book", selections( field("id", "1"), field("title", "My first book"), field( "authors", selection( field("name", "Ricky Carmichael") ) ) ) ) ) ).toResponse(); Response response = endpoint.post(uriInfo, user1, graphQLRequestToJSON(graphQLRequest)); assert200EqualBody(response, graphQLResponse); }
@Test void testValidFetchWithVariables() throws JSONException { String graphQLRequest = document( query( "myQuery", variableDefinitions( variableDefinition("bookId", "[String]") ), selections( field( "book", arguments( argument("ids", "$bookId") ), selections( field("id"), field("title"), field( "authors", selection( field("name") ) ) ) ) ) ) ).toQuery(); String graphQLResponse = document( selection( field( "book", selections( field("id", "1"), field("title", "My first book"), field( "authors", selection( field("name", "Ricky Carmichael") ) ) ) ) ) ).toResponse(); Map<String, String> variables = new HashMap<>(); variables.put("bookId", "1"); Response response = endpoint.post(uriInfo, user1, graphQLRequestToJSON(graphQLRequest, variables)); assert200EqualBody(response, graphQLResponse); }
@Test void testCanReadRestrictedFieldWithAppropriateAccess() throws JSONException { String graphQLRequest = document( selection( field( "book", selection( field("user1SecretField") ) ) ) ).toQuery(); String graphQLResponse = document( selection( field( "book", selection( field("user1SecretField", "this is a secret for user 1 only1") ) ) ) ).toResponse(); Response response = endpoint.post(uriInfo, user1, graphQLRequestToJSON(graphQLRequest)); assert200EqualBody(response, graphQLResponse); }
@Test void testCannotReadRestrictedField() throws IOException { String graphQLRequest = document( selection( field( "book", selection( field("user1SecretField") ) ) ) ).toQuery(); Response response = endpoint.post(uriInfo, user2, graphQLRequestToJSON(graphQLRequest)); assertHasErrors(response); }
@Test void testPartialResponse() throws IOException, JSONException { String graphQLRequest = document( selections( field( "book", selection( field("user1SecretField") ) ), field( "book", selections( field("id"), field("title") ) ) ) ).toQuery(); String expectedData = document( selection( field( "book", selections( field("user1SecretField", "null", false), field("id", "1"), field("title", "My first book") ) ) ) ).toResponse(); Response response = endpoint.post(uriInfo, user2, graphQLRequestToJSON(graphQLRequest)); assertHasErrors(response); assert200DataEqual(response, expectedData); }
@Test void testCrypticErrorOnUpsert() throws IOException, JSONException { Incident incident = new Incident(); String graphQLRequest = document( selection( field( "incidents", arguments( argument("op", "UPSERT"), argument("data", incident) ), selections( field("id"), field("name") ) ) ) ).toQuery(); Response response = endpoint.post(uriInfo, user2, graphQLRequestToJSON(graphQLRequest)); JsonNode node = extract200Response(response); Iterator<JsonNode> errors = node.get("errors").elements(); assertTrue(errors.hasNext()); assertTrue(errors.next().get("message").asText().contains("No id provided, cannot persist incidents")); }
@Test void testFailedMutationAndRead() throws IOException, JSONException { Author author = new Author(); author.setId(2L); Book book = new Book(); book.setId(1); book.setTitle("my new book!"); book.setAuthors(Sets.newHashSet(author)); String graphQLRequest = document( selection( field( "book", arguments( argument("op", "UPSERT"), argument("data", book) ), selections( field("id"), field("title") ) ) ) ).toQuery(); Response response = endpoint.post(uriInfo, user2, graphQLRequestToJSON(graphQLRequest)); assertHasErrors(response); graphQLRequest = document( selection( field( "book", selections( field("id"), field("title") ) ) ) ).toQuery(); String expected = document( selection( field( "book", selections( field("id", "1"), field("title", "My first book") ) ) ) ).toResponse(); response = endpoint.post(uriInfo, user2, graphQLRequestToJSON(graphQLRequest)); assert200EqualBody(response, expected); }
@Test void testNonShareable() throws IOException, JSONException { DisallowShare noShare = new DisallowShare(); noShare.setId(1L); Author author = new Author(); author.setId(123L); author.setName("my new author"); author.setNoShare(noShare); String graphQLRequest = document( mutation( selection( field( "book", selections( field("id"), field( "authors", arguments( argument("op", "UPSERT"), argument("data", author) ), selections( field("id"), field("name"), field( "noShare", selection( field("id") ) ) ) ) ) ) ) ) ).toQuery(); Response response = endpoint.post(uriInfo, user1, graphQLRequestToJSON(graphQLRequest)); assertHasErrors(response); graphQLRequest = document( selection( field( "book", selections( field("id"), field( "authors", selections( field("id"), field("name"), field( "noShare", selection( field("id") ) ) ) ) ) ) ) ).toQuery(); String expected = document( selection( field( "book", selections( field("id", "1"), field( "authors", selections( field("id", "1"), field("name", "Ricky Carmichael"), field("noShare", "", false) ) ) ) ) ) ).toResponse(); response = endpoint.post(uriInfo, user1, graphQLRequestToJSON(graphQLRequest)); assert200EqualBody(response, expected); }
@Test void testLifeCycleHooks () throws Exception { SecurityContext user = Mockito.mock(SecurityContext.class); User principal = new User().withName("1"); Mockito.when(user.getUserPrincipal()).thenReturn(principal); Book book = new Book(); book.setId(1); book.setTitle("my new book!"); String graphQLRequest = document( mutation( selection( field( "book", arguments( argument("op", "UPSERT"), argument("data", book) ), selections( field("id"), field("title") ) ) ) ) ).toQuery(); String expected = document( selection( field( "book", selections( field("id", "1"), field("title", "my new book!") ) ) ) ).toResponse(); Response response = endpoint.post(uriInfo, user, graphQLRequestToJSON(graphQLRequest)); assert200EqualBody(response, expected); String expectedLog = "On Title Update Pre Security\nOn Title Update Pre Commit\nOn Title Update Post Commit\n"; assertEquals(principal.getLog(), expectedLog); }
@Test void testAuditLogging() throws IOException { Mockito.reset(audit); Book book = new Book(); book.setTitle("my new book!"); String graphQLRequest = document( mutation( selection( field( "book", arguments( argument("op", "UPSERT"), argument("data", book) ), selections( field("id"), field("title") ) ) ) ) ).toQuery(); endpoint.post(uriInfo, user1, graphQLRequestToJSON(graphQLRequest)); Mockito.verify(audit, Mockito.times(1)).log(Mockito.any()); Mockito.verify(audit, Mockito.times(1)).commit(Mockito.any()); Mockito.verify(audit, Mockito.times(1)).clear(); }
@Test void testSuccessfulMutation() throws JSONException { Author author = new Author(); author.setId(2L); Book book = new Book(); book.setId(123); book.setTitle("my new book!"); book.setAuthors(Sets.newHashSet(author)); String graphQLRequest = document( mutation( selection( field( "book", arguments( argument("op", "UPSERT"), argument("data", book) ), selections( field("id"), field("title"), field("user1SecretField") ) ) ) ) ).toQuery(); String expected = document( selection( field( "book", selections( field("id", "2"), field("title", "my new book!"), field("user1SecretField", "this is a secret for user 1 only1") ) ) ) ).toResponse(); Response response = endpoint.post(uriInfo, user1, graphQLRequestToJSON(graphQLRequest)); assert200EqualBody(response, expected); graphQLRequest = document( selection( field( "book", selections( field("id"), field("title"), field( "authors", selections( field("id"), field("name") ) ) ) ) ) ).toQuery(); expected = document( selection( field( "book", selections( field("id", "1"), field("title", "My first book"), field( "authors", selections( field("id", "1"), field("name", "Ricky Carmichael") ) ) ), selections( field("id", "2"), field("title", "my new book!"), field( "authors", selections( field("id", "2"), field("name", "The Silent Author") ) ) ) ) ) ).toResponse(); response = endpoint.post(uriInfo, user1, graphQLRequestToJSON(graphQLRequest)); assert200EqualBody(response, expected); }
@Test void testFailedCommitCheck() throws IOException { Book book = new Book(); book.setId(1); book.setTitle("update title"); String graphQLRequest = document( mutation( selection( field( "book", arguments( argument("op", "UPSERT"), argument("data", book) ), selections( field("id"), field("title") ) ) ) ) ).toQuery(); Response response = endpoint.post(uriInfo, user3, graphQLRequestToJSON(graphQLRequest)); assertHasErrors(response); }
@Test void testQueryAMap() throws JSONException { String graphQLRequest = document( mutation( selection( field( "book", selections( field("id"), field( "authors", selection( field("bookTitlesAndAwards {key value}") ) ) ) ) ) ) ).toQuery(); Map<String, String> first = new HashMap<>(); first.put("key", "Lost in the Data"); first.put("value", "PEN/Faulkner Award"); Map<String, String> second = new HashMap<>(); second.put("key", "Bookz"); second.put("value", "Pulitzer Prize"); String expected = document( selection( field( "book", selections( field("id", "1"), field( "authors", selection( field( "bookTitlesAndAwards", toJson(Arrays.asList(first, second)), UNQUOTED_VALUE ) ) ) ) ) ) ).toResponse(); Response response = endpoint.post(uriInfo, user1, graphQLRequestToJSON(graphQLRequest)); assert200EqualBody(response, expected); }
@Test void testQueryAMapWithBadFields() throws IOException { String graphQLRequest = document( mutation( selection( field( "book", selections( field("id"), field( "authors", selection( field("bookTitlesAndAwards {key value Bookz}") ) ) ) ) ) ) ).toQuery(); Response response = endpoint.post(uriInfo, user1, graphQLRequestToJSON(graphQLRequest)); assertHasErrors(response); }