Skip to content

Commit

Permalink
Improved support for built-in types and directives.
Browse files Browse the repository at this point in the history
  • Loading branch information
cutsoy committed Dec 28, 2022
1 parent 5905ce9 commit 2a151d8
Show file tree
Hide file tree
Showing 17 changed files with 212 additions and 270 deletions.
7 changes: 5 additions & 2 deletions litho-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ edition = "2021"
name = "litho"

[dependencies]
smol_str = "0.1.23"

litho-compiler = { path = "../litho-compiler" }
litho-language = { path = "../litho-language" }

ariadne = "0.1.5"
glob = "0.3.0"
smol_str = "0.1.23"

[build-dependencies]
vergen = "7.4.4"
10 changes: 10 additions & 0 deletions litho-cli/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use vergen::vergen;
use vergen::{Config, ShaKind, TimestampKind};

pub fn main() {
let mut config = Config::default();
*config.git_mut().sha_kind_mut() = ShaKind::Short;
*config.git_mut().commit_timestamp_kind_mut() = TimestampKind::DateOnly;

vergen(config).unwrap();
}
43 changes: 22 additions & 21 deletions litho-cli/src/bin/litho.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::process::ExitCode;

use ariadne::{Cache, Label, Report, ReportKind, Source};
use glob::glob;
use litho_compiler::Compiler;
use litho_compiler::{builtins, Compiler};
use litho_language::lex::{SourceId, SourceMap, Span};
use smol_str::SmolStr;

Expand Down Expand Up @@ -36,32 +36,33 @@ pub fn main() -> ExitCode {
let mut args = args().skip(1);

while let Some(arg) = args.next() {
inputs.extend(
glob(&arg)
.into_iter()
.flatten()
.map(IntoIterator::into_iter)
.flatten()
.map(|path| path.to_string_lossy().into_owned()),
);
match arg.as_str() {
"--version" | "-v" | "version" => {
println!(
"litho {} ({} {})",
env!("CARGO_PKG_VERSION"),
env!("VERGEN_GIT_SHA_SHORT"),
env!("VERGEN_GIT_COMMIT_DATE")
);

return ExitCode::SUCCESS;
}
arg => inputs.extend(
glob(&arg)
.into_iter()
.flatten()
.map(IntoIterator::into_iter)
.flatten()
.map(|path| path.to_string_lossy().into_owned()),
),
}
}

let mut compiler = Compiler::<SmolStr>::new();
let mut source_map = SourceMap::new();
let mut sources = Sources::default();

let std = vec![
(
"litho://std.litho.dev/inflection.graphql",
include_str!("../../std/introspection.graphql").to_owned(),
),
(
"litho://std.litho.dev/scalars.graphql",
include_str!("../../std/scalars.graphql").to_owned(),
),
];

for (path, text) in std.into_iter() {
for (path, text) in builtins().into_iter().copied() {
let source_id = source_map.get_or_insert(path.to_owned());
sources.insert(source_id, path.to_owned(), Source::from(&text));
compiler.add_document(source_id, &text, true);
Expand Down
90 changes: 0 additions & 90 deletions litho-cli/std/introspection.graphql

This file was deleted.

20 changes: 20 additions & 0 deletions litho-compiler/src/builtins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pub const fn builtins() -> &'static [(&'static str, &'static str)] {
&[
(
"litho://std.litho.dev/directives.graphql",
include_str!("../std/directives.graphql"),
),
(
"litho://std.litho.dev/introspection.graphql",
include_str!("../std/introspection.graphql"),
),
(
"litho://std.litho.dev/litho.graphql",
include_str!("../std/litho.graphql"),
),
(
"litho://std.litho.dev/scalars.graphql",
include_str!("../std/scalars.graphql"),
),
]
}
2 changes: 2 additions & 0 deletions litho-compiler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod builtins;
mod compiler;
mod dependency;
mod depgraph;

pub use builtins::builtins;
pub use compiler::Compiler;
pub use dependency::{Consumer, Dependency, Producer};
pub use depgraph::DepGraph;
44 changes: 44 additions & 0 deletions litho-compiler/std/directives.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
The `@skip` built-in directive may be provided for fields, fragment spreads, and
inline fragments, and allows for conditional exclusion as described by the `if`
argument.
"""
directive @skip(
if: Boolean!,
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

"""
The `@include` built-in directive may be provided for fields, fragment spreads,
and inline fragments, and allows for conditional inclusion during execution as
described by the `if` argument.
"""
directive @include(
if: Boolean!,
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

"""
The `@deprecated` built-in directive is used within the type system definition
language to indicate deprecated portions of a GraphQL service's schema, such as
deprecated fields on a type or deprecated enum values.
Deprecations include a reason for why it is deprecated, which is formatted using
Markdown syntax (as specified by CommonMark).
"""
directive @deprecated(
reason: String = "No longer supported",
) on
| FIELD_DEFINITION
| ARGUMENT_DEFINITION
| INPUT_FIELD_DEFINITION
| ENUM_VALUE

"""
The `@specifiedBy` built-in directive is used within the type system definition
language to provide a scalar specification URL for specifying the behavior of
custom scalar types. The URL should point to a human-readable specification of
the data format, serialization, and coercion rules. It must not appear on
built-in scalar types.
"""
directive @specifiedBy(
url: String!,
) on SCALAR
96 changes: 96 additions & 0 deletions litho-compiler/std/introspection.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
extend type Query {
__schema: __Schema!
__type(
name: String!,
): __Type
}

type __Schema {
description: String
types: [__Type!]!
queryType: __Type!
mutationType: __Type
subscriptionType: __Type
directives: [__Directive!]!
}

type __Type {
kind: __TypeKind
name: String
description: String
fields(
includeDeprecated: Boolean = false,
): [__Field!]
interfaces: [__Type!]
possibleTypes: [__Type!]
enumValues(
includeDeprecated: Boolean = false,
): [__EnumValue!]
inputFields: [__InputValue!]
ofType: __Type
specifiedByURL: String
}

enum __TypeKind {
SCALAR
OBJECT
INTERFACE
UNION
ENUM
INPUT_OBJECT
LIST
NON_NULL
}

type __Field {
name: String!
description: String
args: [__InputValue!]!
type: __Type!
isDeprecated: Boolean!
deprecationReason: String
}

type __InputValue {
name: String!
description: String
type: __Type!
defaultValue: String
}

type __EnumValue {
name: String!
description: String
isDeprecated: Boolean!
deprecationReason: String
}

type __Directive {
name: String!
description: String
locations: [__DirectiveLocation!]!
args: [__InputValue!]!
isRepeatable: Boolean!
}

enum __DirectiveLocation {
QUERY
MUTATION
SUBSCRIPTION
FIELD
FRAGMENT_DEFINITION
FRAGMENT_SPREAD
INLINE_FRAGMENT
VARIABLE_DEFINITION
SCHEMA
SCALAR
OBJECT
FIELD_DEFINITION
ARGUMENT_DEFINITION
INTERFACE
UNION
ENUM
ENUM_VALUE
INPUT_OBJECT
INPUT_FIELD_DEFINITION
}
3 changes: 3 additions & 0 deletions litho-compiler/std/litho.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
directive @litho(
url: String!,
) on SCHEMA
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
directive @litho(url: String!) on SCHEMA

"""
The Int scalar type represents a signed 32-bit numeric non-fractional value.
Response formats that support a 32-bit integer or a number type should use that
Expand Down
4 changes: 3 additions & 1 deletion litho-language/src/fmt/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ where

formatter.line()?;
self.directive.format(formatter)?;
self.at.format(formatter)?;
formatter.squeeze(|formatter| self.name.format(formatter))?;
formatter.squeeze(|formatter| self.arguments_definition.format(formatter))?;
self.repeatable.format(formatter)?;
Expand All @@ -591,6 +592,7 @@ where
if self.expands() {
formatter.indent(|formatter| {
for location in self.locations() {
formatter.line()?;
formatter.push("|")?;
location.format(formatter)?;
}
Expand All @@ -601,8 +603,8 @@ where
for (i, location) in self.locations().enumerate() {
if i != 0 {
formatter.push("|")?;
location.format(formatter)?;
}
location.format(formatter)?;
}
}

Expand Down
1 change: 0 additions & 1 deletion litho-language/src/fmt/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ where
let result = closure(self);
self.line()?;
self.shape.indent -= 4;
self.shape.blank_lines = 0;
result
}

Expand Down
Loading

0 comments on commit 2a151d8

Please sign in to comment.