Skip to content

Commit

Permalink
Improve test output for relay-docblock
Browse files Browse the repository at this point in the history
Reviewed By: alunyov

Differential Revision: D34235419

fbshipit-source-id: e62dd06dfaaf2dbc5c1f75bb36949b0507180e6e
  • Loading branch information
captbaritone authored and facebook-github-bot committed Feb 16, 2022
1 parent e8b5596 commit 7687c7a
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 52 deletions.
16 changes: 16 additions & 0 deletions compiler/crates/relay-docblock/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ pub enum DocblockIr {
RelayResolver(RelayResolverIr),
}

impl DocblockIr {
pub fn to_sdl_string(&self) -> String {
match self {
DocblockIr::RelayResolver(relay_resolver) => relay_resolver.to_sdl_string(),
}
}
}

#[derive(Debug, PartialEq)]
pub struct RelayResolverIr {
pub field_name: WithLocation<StringKey>,
Expand All @@ -38,6 +46,14 @@ pub struct RelayResolverIr {
}

impl RelayResolverIr {
pub fn to_sdl_string(&self) -> String {
self.to_graphql_schema_ast()
.definitions
.iter()
.map(|definition| format!("{}", definition))
.collect::<Vec<String>>()
.join("\n\n")
}
pub fn to_graphql_schema_ast(&self) -> SchemaDocument {
let edge_to = self
.edge_to
Expand Down
10 changes: 7 additions & 3 deletions compiler/crates/relay-docblock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,14 @@ impl RelayResolverParser {
}
}

let field_name = self.assert_field_value(*FIELD_NAME_FIELD);
let on_type = self.assert_field_value(*ON_TYPE_FIELD);
let root_fragment = self.assert_field_value(*ROOT_FRAGMENT_FIELD);

Ok(RelayResolverIr {
field_name: self.assert_field_value(*FIELD_NAME_FIELD)?,
on_type: self.assert_field_value(*ON_TYPE_FIELD)?,
root_fragment: self.assert_field_value(*ROOT_FRAGMENT_FIELD)?,
field_name: field_name?,
on_type: on_type?,
root_fragment: root_fragment?,
edge_to: self.fields.get(&EDGE_TO_FIELD).copied(),
description: self.description,
location: ast.location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
==================================== ERROR ====================================
✖︎ Unexpected docblock field "@notValid"

/path/to/test/fixture/relay-resolver-invalid-field.invalid.js:4:56
3 │ *
4 │ * This source code is licensed under the MIT license found in the
^^^^^^^^
5 │ * LICENSE file in the root directory of this source tree.
/path/to/test/fixture/relay-resolver-invalid-field.invalid.js:7:5
6 │ * @rootFragment myRootFragment
7 │ * @notValid myRootFragment
│ ^^^^^^^^
8 │
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
✖︎ Missing docblock field "@onType"

<generated>:1:1
1 │ /**
1 │ *
│ ^
2 │ * Copyright (c) Meta Platforms, Inc. and affiliates.
2 │ * @RelayResolver
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
==================================== INPUT ====================================
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// expected-to-throw

/**
* @RelayResolver
*/
==================================== ERROR ====================================
✖︎ Missing docblock field "@fieldName"

<generated>:1:1
1 │ *
│ ^
2 │ * @RelayResolver


✖︎ Missing docblock field "@onType"

<generated>:1:1
1 │ *
│ ^
2 │ * @RelayResolver


✖︎ Missing docblock field "@rootFragment"

<generated>:1:1
1 │ *
│ ^
2 │ * @RelayResolver
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// expected-to-throw

/**
* @RelayResolver
*/
19 changes: 8 additions & 11 deletions compiler/crates/relay-docblock/tests/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,32 @@ use relay_docblock::parse_docblock_ast;
pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
let js_features = extract_graphql::extract(fixture.content);

let asts = js_features
let irs = js_features
.iter()
.enumerate()
.filter_map(|(i, source)| match source {
JavaScriptSourceFeature::GraphQL(_) => None,
JavaScriptSourceFeature::Docblock(docblock_source) => Some((i, docblock_source)),
})
.map(|(i, souce)| {
.map(|(i, source)| {
parse_docblock(
&souce.text,
&source.text,
SourceLocationKey::Embedded {
path: format!("/path/to/test/fixture/{}", fixture.file_name).intern(),
index: i as u16,
},
)
.and_then(|ast| parse_docblock_ast(&ast))
.map_err(|diagnostics| diagnostics_to_sorted_string(&source.text, &diagnostics))
})
.collect::<Result<Vec<_>, _>>()
.map_err(|diagnostics| diagnostics_to_sorted_string(fixture.content, &diagnostics))?;
.collect::<Result<Vec<_>, String>>()?;

let irs = asts
.iter()
.map(parse_docblock_ast)
.collect::<Result<Vec<_>, _>>()
.map_err(|diagnostics| diagnostics_to_sorted_string(fixture.content, &diagnostics))?
let output = irs
.iter()
.flatten()
.map(|ir| format!("{:#?}", ir))
.collect::<Vec<_>>()
.join("\n\n");

Ok(irs)
Ok(output)
}
9 changes: 8 additions & 1 deletion compiler/crates/relay-docblock/tests/parse_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<f6616b98273f569f44024de25e26a578>>
* @generated SignedSource<<04e01593c61a018d467a26e75557e993>>
*/

mod parse;
Expand Down Expand Up @@ -32,3 +32,10 @@ fn relay_resolver_missing_field_invalid() {
let expected = include_str!("parse/fixtures/relay-resolver-missing-field.invalid.expected");
test_fixture(transform_fixture, "relay-resolver-missing-field.invalid.js", "parse/fixtures/relay-resolver-missing-field.invalid.expected", input, expected);
}

#[test]
fn relay_resolver_missing_multiple_fields_invalid() {
let input = include_str!("parse/fixtures/relay-resolver-missing-multiple-fields.invalid.js");
let expected = include_str!("parse/fixtures/relay-resolver-missing-multiple-fields.invalid.expected");
test_fixture(transform_fixture, "relay-resolver-missing-multiple-fields.invalid.js", "parse/fixtures/relay-resolver-missing-multiple-fields.invalid.expected", input, expected);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

/**
* @RelayResolver
*
*
* @onType User
* @fieldName favorite_page
* @edgeTo Page
* @rootFragment myRootFragment
*
* The user's favorite page! They probably clicked something in the UI
*
* The user's favorite page! They probably clicked something in the UI
* to tell us that it was their favorite page and then we put that in a
* database or something. Then we got that info out again and put it out
* again. Anyway, I'm rambling now. Its a page that the user likes. A lot.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

/**
* @RelayResolver
*
*
* @onType User
* @fieldName favorite_page
* @edgeTo Page
* @rootFragment myRootFragment
*
* The user's favorite page! They probably clicked something in the UI
*
* The user's favorite page! They probably clicked something in the UI
* to tell us that it was their favorite page and then we put that in a
* database or something. Then we got that info out again and put it out
* again. Anyway, I'm rambling now. Its a page that the user likes. A lot.
Expand Down
34 changes: 10 additions & 24 deletions compiler/crates/relay-docblock/tests/to_schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,42 @@
*/

use common::SourceLocationKey;
// use docblock_syntax::parse_docblock;
use docblock_syntax::parse_docblock;
use extract_graphql::{self, JavaScriptSourceFeature};
use fixture_tests::Fixture;
use graphql_test_helpers::diagnostics_to_sorted_string;
use intern::string_key::Intern;
use relay_docblock::{parse_docblock_ast, DocblockIr};
use relay_docblock::parse_docblock_ast;

pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
let js_features = extract_graphql::extract(fixture.content);

let asts = js_features
let irs = js_features
.iter()
.enumerate()
.filter_map(|(i, source)| match source {
JavaScriptSourceFeature::GraphQL(_) => None,
JavaScriptSourceFeature::Docblock(docblock_source) => Some((i, docblock_source)),
})
.map(|(i, souce)| {
.map(|(i, source)| {
parse_docblock(
&souce.text,
&source.text,
SourceLocationKey::Embedded {
path: format!("/path/to/test/fixture/{}", fixture.file_name).intern(),
index: i as u16,
},
)
.and_then(|ast| parse_docblock_ast(&ast))
.map_err(|diagnostics| diagnostics_to_sorted_string(&source.text, &diagnostics))
})
.collect::<Result<Vec<_>, _>>()
.map_err(|diagnostics| diagnostics_to_sorted_string(fixture.content, &diagnostics))?;
.collect::<Result<Vec<_>, String>>()?;

let irs = asts
let output = irs
.iter()
.map(|ast| parse_docblock_ast(ast))
.collect::<Result<Vec<_>, _>>()
.map_err(|diagnostics| diagnostics_to_sorted_string(fixture.content, &diagnostics))?
.iter()
.flatten()
.map(|ir| {
let schema = match ir {
DocblockIr::RelayResolver(resolver) => resolver.to_graphql_schema_ast(),
};
schema
.definitions
.iter()
.map(|def| format!("{}", def))
.collect::<Vec<_>>()
})
.flatten()
.map(|ir| ir.to_sdl_string())
.collect::<Vec<_>>()
.join("\n\n");

Ok(irs)
Ok(output)
}

0 comments on commit 7687c7a

Please sign in to comment.