Skip to content

Commit

Permalink
Populate all types in flatbuffer schema
Browse files Browse the repository at this point in the history
Reviewed By: ginfung

Differential Revision: D42364734

fbshipit-source-id: 31ce468b92821c8d0f90ad7319cc791a45d57c3a
  • Loading branch information
Steven Chaitoff authored and facebook-github-bot committed Jan 6, 2023
1 parent b7a6654 commit e0d62b1
Showing 1 changed file with 118 additions and 7 deletions.
125 changes: 118 additions & 7 deletions compiler/crates/schema/src/flatbuffer/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ impl Schema for SchemaWrapper {
}

fn input_objects<'a>(&'a self) -> Box<dyn Iterator<Item = &'a InputObject> + 'a> {
if self.input_objects.map.is_empty() {
if self.input_objects.map.len() < self.flatbuffer_schema().input_objects.len() {
for i in 0..self.flatbuffer_schema().input_objects.len() {
let id = InputObjectID(i.try_into().unwrap());
self.input_object(id);
Expand All @@ -410,7 +410,7 @@ impl Schema for SchemaWrapper {
}

fn enums<'a>(&'a self) -> Box<dyn Iterator<Item = &'a Enum> + 'a> {
if self.enums.map.is_empty() {
if self.enums.map.len() < self.flatbuffer_schema().enums.len() {
for i in 0..self.flatbuffer_schema().enums.len() {
let id = EnumID(i.try_into().unwrap());
self.enum_(id);
Expand All @@ -421,7 +421,7 @@ impl Schema for SchemaWrapper {
}

fn scalars<'a>(&'a self) -> Box<dyn Iterator<Item = &'a Scalar> + 'a> {
if self.scalars.map.is_empty() {
if self.scalars.map.len() < self.flatbuffer_schema().scalars.len() {
for i in 0..self.flatbuffer_schema().scalars.len() {
let id = ScalarID(i.try_into().unwrap());
self.scalar(id);
Expand All @@ -432,7 +432,7 @@ impl Schema for SchemaWrapper {
}

fn fields<'a>(&'a self) -> Box<dyn Iterator<Item = &'a Field> + 'a> {
if self.fields.map.is_empty() {
if self.fields.map.len() < self.flatbuffer_schema().fields.len() {
for i in 0..self.flatbuffer_schema().fields.len() {
let id = FieldID(i.try_into().unwrap());
self.field(id);
Expand All @@ -443,7 +443,7 @@ impl Schema for SchemaWrapper {
}

fn objects<'a>(&'a self) -> Box<dyn Iterator<Item = &'a Object> + 'a> {
if self.objects.map.is_empty() {
if self.objects.map.len() < self.flatbuffer_schema().objects.len() {
for i in 0..self.flatbuffer_schema().objects.len() {
let id = ObjectID(i.try_into().unwrap());
self.object(id);
Expand All @@ -454,7 +454,7 @@ impl Schema for SchemaWrapper {
}

fn unions<'a>(&'a self) -> Box<dyn Iterator<Item = &'a Union> + 'a> {
if self.unions.map.is_empty() {
if self.unions.map.len() < self.flatbuffer_schema().unions.len() {
for i in 0..self.flatbuffer_schema().unions.len() {
let id = UnionID(i.try_into().unwrap());
self.union(id);
Expand All @@ -465,7 +465,7 @@ impl Schema for SchemaWrapper {
}

fn interfaces<'a>(&'a self) -> Box<dyn Iterator<Item = &'a Interface> + 'a> {
if self.interfaces.map.is_empty() {
if self.interfaces.map.len() < self.flatbuffer_schema().interfaces.len() {
for i in 0..self.flatbuffer_schema().interfaces.len() {
let id = InterfaceID(i.try_into().unwrap());
self.interface(id);
Expand Down Expand Up @@ -493,3 +493,114 @@ impl<K: Hash + Eq, V> Cache<K, V> {
.or_insert_with(|| Box::leak(Box::new(f())))
}
}

#[cfg(test)]
mod tests {
use common::DiagnosticsResult;

use super::*;
use crate::build_schema;
use crate::flatbuffer::serialize::serialize_as_flatbuffer;

#[test]
fn all_scalars() -> DiagnosticsResult<()> {
let sdl = "
type Query { id: ID }
";
let sdl_schema = build_schema(sdl)?.unwrap_in_memory_impl();
let bytes = serialize_as_flatbuffer(&sdl_schema);
let fb_schema = SchemaWrapper::from_vec(bytes);

let mut scalar_names = fb_schema
.scalars()
.map(|scalar| scalar.name.item.0.lookup())
.collect::<Vec<&str>>();
scalar_names.sort();
assert_eq!(
scalar_names,
vec!["Boolean", "Float", "ID", "Int", "String"]
);

Ok(())
}

#[test]
fn all_scalars_with_lookup() -> DiagnosticsResult<()> {
let sdl = "
type Query { id: ID }
";
let sdl_schema = build_schema(sdl)?.unwrap_in_memory_impl();
let bytes = serialize_as_flatbuffer(&sdl_schema);
let fb_schema = SchemaWrapper::from_vec(bytes);

// Look up a scalar type to populate cache
let user_type = fb_schema.get_type("String".intern()).unwrap();
fb_schema.scalar(user_type.get_scalar_id().unwrap());

let mut scalar_names = fb_schema
.scalars()
.map(|scalar| scalar.name.item.0.lookup())
.collect::<Vec<&str>>();
scalar_names.sort();
assert_eq!(
scalar_names,
vec!["Boolean", "Float", "ID", "Int", "String"]
);

Ok(())
}

#[test]
fn all_types() -> DiagnosticsResult<()> {
let sdl = "
type Query { id: ID }
type User { id: ID }
type MailingAddress { id: ID }
type Country { id: ID }
";
let sdl_schema = build_schema(sdl)?.unwrap_in_memory_impl();
let bytes = serialize_as_flatbuffer(&sdl_schema);
let fb_schema = SchemaWrapper::from_vec(bytes);

let mut object_names = fb_schema
.objects()
.map(|object| object.name.item.0.lookup())
.collect::<Vec<&str>>();
object_names.sort();
assert_eq!(
object_names,
vec!["Country", "MailingAddress", "Query", "User"]
);

Ok(())
}

#[test]
fn all_types_with_lookup() -> DiagnosticsResult<()> {
let sdl = "
type Query { id: ID }
type User { id: ID }
type MailingAddress { id: ID }
type Country { id: ID }
";
let sdl_schema = build_schema(sdl)?.unwrap_in_memory_impl();
let bytes = serialize_as_flatbuffer(&sdl_schema);
let fb_schema = SchemaWrapper::from_vec(bytes);

// Look up User type to populate cache
let user_type = fb_schema.get_type("User".intern()).unwrap();
fb_schema.object(user_type.get_object_id().unwrap());

let mut object_names = fb_schema
.objects()
.map(|object| object.name.item.0.lookup())
.collect::<Vec<&str>>();
object_names.sort();
assert_eq!(
object_names,
vec!["Country", "MailingAddress", "Query", "User"]
);

Ok(())
}
}

0 comments on commit e0d62b1

Please sign in to comment.