Skip to content

Master to async await #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/book/content/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This page will give you a short introduction to the concepts in Juniper.

```toml
[dependencies]
juniper = "0.11"
juniper = "0.14.2"
```

## Schema example
Expand Down
8 changes: 8 additions & 0 deletions docs/book/content/types/objects/complex_fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ impl Person {
}
}

// Note that this syntax generates an implementation of the GraphQLType trait,
// the base impl of your struct can still be written like usual:
impl Person {
pub fn hidden_from_graphql(&self) {
// [...]
}
}

# fn main() { }
```

Expand Down
5 changes: 5 additions & 0 deletions juniper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
The previous `Value` return type was just an internal artifact of
error handling.

# [[0.14.2] 2019-12-16](https://github.com/graphql-rust/juniper/releases/tag/juniper-0.14.2)

- Fix incorrect validation with non-executed operations [#455](https://github.com/graphql-rust/juniper/issues/455)
- Correctly handle raw identifiers in field and argument names.

# [[0.14.1] 2019-10-24](https://github.com/graphql-rust/juniper/releases/tag/juniper-0.14.1)

- Fix panic when an invalid scalar is used by a client [#434](https://github.com/graphql-rust/juniper/pull/434)
Expand Down
6 changes: 3 additions & 3 deletions juniper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "juniper"
version = "0.14.1"
version = "0.14.2"
authors = [
"Magnus Hallin <mhallin@fastmail.com>",
"Christoph Herzog <chris@theduke.at>",
Expand Down Expand Up @@ -33,7 +33,7 @@ default = [
]

[dependencies]
juniper_codegen = { version = "0.14.1", path = "../juniper_codegen" }
juniper_codegen = { version = "0.14.2", path = "../juniper_codegen" }

chrono = { version = "0.4.0", optional = true }
fnv = "1.0.3"
Expand All @@ -43,7 +43,7 @@ serde = { version = "1.0.8" }
serde_derive = { version = "1.0.2" }
serde_json = { version="1.0.2", optional = true }
url = { version = "2", optional = true }
uuid = { version = "0.7", optional = true }
uuid = { version = ">= 0.7, < 0.8", optional = true }

[dev-dependencies]
bencher = "0.1.2"
Expand Down
2 changes: 2 additions & 0 deletions juniper/release.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pre-release-replacements = [
# Juniper's changelog
{file="CHANGELOG.md", search="# master", replace="# master\n\n- No changes yet\n\n# [[{{version}}] {{date}}](https://github.com/graphql-rust/juniper/releases/tag/{{crate_name}}-{{version}})"},
{file="src/lib.rs", search="docs.rs/juniper/[a-z0-9\\.-]+", replace="docs.rs/juniper/{{version}}"},
# docs
{file="../docs/book/content/quickstart.md", search="juniper = \"[^\"]+\"", replace="juniper = \"{{version}}\""},
# codegen
{file="../juniper_codegen/Cargo.toml", search="juniper = \\{ version = \"[^\"]+\"", replace="juniper = { version = \"{{version}}\""},
# Tests.
Expand Down
82 changes: 48 additions & 34 deletions juniper/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use fnv::FnvHashMap;

use crate::{
ast::{
Definition, Document, Fragment, FromInputValue, InputValue, OperationType, Selection,
ToInputValue, Type,
Definition, Document, Fragment, FromInputValue, InputValue, Operation, OperationType,
Selection, ToInputValue, Type,
},
parser::SourcePosition,
value::Value,
Expand All @@ -31,6 +31,7 @@ pub use self::look_ahead::{
Applies, ChildSelection, ConcreteLookAheadSelection, LookAheadArgument, LookAheadMethods,
LookAheadSelection, LookAheadValue,
};
use crate::parser::Spanning;

/// A type registry used to build schemas
///
Expand Down Expand Up @@ -652,9 +653,9 @@ impl<S> ExecutionError<S> {
}
}

pub fn execute_validated_query<'a, QueryT, MutationT, CtxT, S>(
document: Document<S>,
operation_name: Option<&str>,
pub fn execute_validated_query<'a, 'b, QueryT, MutationT, CtxT, S>(
document: &'b Document<S>,
operation: &'b Spanning<Operation<S>>,
root_node: &RootNode<QueryT, MutationT, S>,
variables: &Variables<S>,
context: &CtxT,
Expand All @@ -665,36 +666,14 @@ where
MutationT: GraphQLType<S, Context = CtxT>,
{
let mut fragments = vec![];
let mut operation = None;

for def in document {
for def in document.iter() {
match def {
Definition::Operation(op) => {
if operation_name.is_none() && operation.is_some() {
return Err(GraphQLError::MultipleOperationsProvided);
}

let move_op = operation_name.is_none()
|| op.item.name.as_ref().map(|s| s.item) == operation_name;

if move_op {
operation = Some(op);
}
}
Definition::Fragment(f) => fragments.push(f),
_ => (),
};
}

let op = match operation {
Some(op) => op,
None => return Err(GraphQLError::UnknownOperationName),
};

if op.item.operation_type == OperationType::Subscription {
return Err(GraphQLError::IsSubscription);
}

let default_variable_values = op.item.variable_definitions.map(|defs| {
let default_variable_values = operation.item.variable_definitions.as_ref().map(|defs| {
defs.item
.items
.iter()
Expand Down Expand Up @@ -723,7 +702,7 @@ where
final_vars = &all_vars;
}

let root_type = match op.item.operation_type {
let root_type = match operation.item.operation_type {
OperationType::Query => root_node.schema.query_type(),
OperationType::Mutation => root_node
.schema
Expand All @@ -738,16 +717,16 @@ where
.map(|f| (f.item.name.item, &f.item))
.collect(),
variables: final_vars,
current_selection_set: Some(&op.item.selection_set[..]),
current_selection_set: Some(&operation.item.selection_set[..]),
parent_selection_set: None,
current_type: root_type,
schema: &root_node.schema,
context,
errors: &errors,
field_path: FieldPath::Root(op.start),
field_path: FieldPath::Root(operation.start),
};

value = match op.item.operation_type {
value = match operation.item.operation_type {
OperationType::Query => executor.resolve_into_value(&root_node.query_info, &root_node),
OperationType::Mutation => {
executor.resolve_into_value(&root_node.mutation_info, &root_node.mutation_type)
Expand Down Expand Up @@ -882,6 +861,41 @@ where
Ok((value, errors))
}

pub fn get_operation<'a, 'b, S>(
document: &'b Document<'b, S>,
operation_name: Option<&str>,
) -> Result<&'b Spanning<Operation<'b, S>>, GraphQLError<'a>>
where
S: ScalarValue,
{
let mut operation = None;
for def in document {
match def {
Definition::Operation(op) => {
if operation_name.is_none() && operation.is_some() {
return Err(GraphQLError::MultipleOperationsProvided);
}

let move_op = operation_name.is_none()
|| op.item.name.as_ref().map(|s| s.item) == operation_name;

if move_op {
operation = Some(op);
}
}
_ => (),
};
}
let op = match operation {
Some(op) => op,
None => return Err(GraphQLError::UnknownOperationName),
};
if op.item.operation_type == OperationType::Subscription {
return Err(GraphQLError::IsSubscription);
}
Ok(op)
}

impl<'r, S> Registry<'r, S>
where
S: ScalarValue + 'r,
Expand Down
5 changes: 3 additions & 2 deletions juniper/src/executor_tests/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ mod named_operations {

#[crate::graphql_object_internal]
impl Schema {
fn a() -> &str {
fn a(p: Option<String>) -> &str {
"b"
}
}
Expand Down Expand Up @@ -1112,7 +1112,8 @@ mod named_operations {
#[test]
fn uses_named_operation_if_name_provided() {
let schema = RootNode::new(Schema, EmptyMutation::<()>::new());
let doc = r"query Example { first: a } query OtherExample { second: a }";
let doc =
r"query Example($p: String!) { first: a(p: $p) } query OtherExample { second: a }";

let vars = vec![].into_iter().collect();

Expand Down
46 changes: 0 additions & 46 deletions juniper/src/executor_tests/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,52 +830,6 @@ fn allow_non_null_lists_of_non_null_to_contain_values() {
);
}

#[test]
fn does_not_allow_invalid_types_to_be_used_as_values() {
let schema = RootNode::new(TestType, EmptyMutation::<()>::new());

let query = r#"query q($input: TestType!) { fieldWithObjectInput(input: $input) }"#;
let vars = vec![(
"value".to_owned(),
InputValue::list(vec![InputValue::scalar("A"), InputValue::scalar("B")]),
)]
.into_iter()
.collect();

let error = crate::execute(query, None, &schema, &vars, &()).unwrap_err();

assert_eq!(
error,
ValidationError(vec![RuleError::new(
r#"Variable "$input" expected value of type "TestType!" which cannot be used as an input type."#,
&[SourcePosition::new(8, 0, 8)],
),])
);
}

#[test]
fn does_not_allow_unknown_types_to_be_used_as_values() {
let schema = RootNode::new(TestType, EmptyMutation::<()>::new());

let query = r#"query q($input: UnknownType!) { fieldWithObjectInput(input: $input) }"#;
let vars = vec![(
"value".to_owned(),
InputValue::list(vec![InputValue::scalar("A"), InputValue::scalar("B")]),
)]
.into_iter()
.collect();

let error = crate::execute(query, None, &schema, &vars, &()).unwrap_err();

assert_eq!(
error,
ValidationError(vec![RuleError::new(
r#"Variable "$input" expected value of type "UnknownType!" which cannot be used as an input type."#,
&[SourcePosition::new(8, 0, 8)],
),])
);
}

#[test]
fn default_argument_when_not_provided() {
run_query(r#"{ fieldWithDefaultArgumentValue }"#, |result| {
Expand Down
8 changes: 4 additions & 4 deletions juniper/src/http/graphiql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ pub fn graphiql_source(graphql_endpoint_url: &str) -> String {
<head>
<title>GraphQL</title>
{stylesheet_source}
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/graphiql/0.10.2/graphiql.css">
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/graphiql@0.17.2/graphiql.min.css">
</head>
<body>
<div id="app"></div>
<script src="//cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.production.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.production.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/graphiql/0.11.11/graphiql.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react/16.10.2/umd/react.production.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react-dom/16.10.2/umd/react-dom.production.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/graphiql@0.17.2/graphiql.min.js"></script>
<script>var GRAPHQL_URL = '{graphql_url}';</script>
{fetcher_source}
</body>
Expand Down
6 changes: 3 additions & 3 deletions juniper/src/http/playground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub fn playground_source(graphql_endpoint_url: &str) -> String {
<meta charset=utf-8 />
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
<title>GraphQL Playground</title>
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/graphql-playground-react@1.7.11/build/static/css/index.css" />
<link rel="shortcut icon" href="//cdn.jsdelivr.net/npm/graphql-playground-react@1.7.11/build/favicon.png" />
<script src="//cdn.jsdelivr.net/npm/graphql-playground-react@1.7.11/build/static/js/middleware.js"></script>
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/css/index.css" />
<link rel="shortcut icon" href="//cdn.jsdelivr.net/npm/graphql-playground-react/build/favicon.png" />
<script src="//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/js/middleware.js"></script>

</head>

Expand Down
27 changes: 17 additions & 10 deletions juniper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Juniper has not reached 1.0 yet, thus some API instability should be expected.
[chrono]: https://crates.io/crates/chrono

*/
#![doc(html_root_url = "https://docs.rs/juniper/0.14.1")]
#![doc(html_root_url = "https://docs.rs/juniper/0.14.2")]
#![warn(missing_docs)]

#[doc(hidden)]
Expand Down Expand Up @@ -152,6 +152,7 @@ mod executor_tests;
pub use crate::util::to_camel_case;

use crate::{
executor::{execute_validated_query, get_operation},
introspection::{INTROSPECTION_QUERY, INTROSPECTION_QUERY_WITHOUT_DESCRIPTIONS},
parser::{parse_document_source, ParseError, Spanning},
validation::{validate_input_values, visit_all_rules, ValidatorContext},
Expand Down Expand Up @@ -206,25 +207,28 @@ where
MutationT: GraphQLType<S, Context = CtxT>,
{
let document = parse_document_source(document_source, &root_node.schema)?;

{
let errors = validate_input_values(variables, &document, &root_node.schema);
let mut ctx = ValidatorContext::new(&root_node.schema, &document);
visit_all_rules(&mut ctx, &document);

let errors = ctx.into_errors();
if !errors.is_empty() {
return Err(GraphQLError::ValidationError(errors));
}
}

let operation = get_operation(&document, operation_name)?;

{
let mut ctx = ValidatorContext::new(&root_node.schema, &document);
visit_all_rules(&mut ctx, &document);
let errors = validate_input_values(variables, operation, &root_node.schema);

let errors = ctx.into_errors();
if !errors.is_empty() {
return Err(GraphQLError::ValidationError(errors));
}
}

executor::execute_validated_query(document, operation_name, root_node, variables, context)
execute_validated_query(&document, operation, root_node, variables, context)
}

/// Execute a query in a provided schema
Expand All @@ -245,19 +249,22 @@ where
CtxT: Send + Sync,
{
let document = parse_document_source(document_source, &root_node.schema)?;

{
let errors = validate_input_values(variables, &document, &root_node.schema);
let mut ctx = ValidatorContext::new(&root_node.schema, &document);
visit_all_rules(&mut ctx, &document);

let errors = ctx.into_errors();
if !errors.is_empty() {
return Err(GraphQLError::ValidationError(errors));
}
}

let operation = get_operation(&document, operation_name)?;

{
let mut ctx = ValidatorContext::new(&root_node.schema, &document);
visit_all_rules(&mut ctx, &document);
let errors = validate_input_values(variables, operation, &root_node.schema);

let errors = ctx.into_errors();
if !errors.is_empty() {
return Err(GraphQLError::ValidationError(errors));
}
Expand Down
Loading