Skip to content
Draft
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
217 changes: 191 additions & 26 deletions packages/supermassive/src/schema/definition.ts

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions packages/supermassive/src/schema/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
DirectiveDefinitionTuple,
DirectiveName,
encodeDirectiveLocation,
getDirectiveName,
getDirectiveDefinitionName,
} from "./definition";

/**
Expand Down Expand Up @@ -106,7 +106,9 @@ export function isKnownDirective(
directive: DirectiveName | DirectiveDefinitionTuple,
): boolean {
const name =
typeof directive === "string" ? directive : getDirectiveName(directive);
typeof directive === "string"
? directive
: getDirectiveDefinitionName(directive);
return (
isSpecifiedDirective(directive) ||
name === SUPERMASSIVE_SCHEMA_DIRECTIVE_NAME
Expand All @@ -116,8 +118,10 @@ export function isSpecifiedDirective(
directive: DirectiveName | DirectiveDefinitionTuple,
): boolean {
const name =
typeof directive === "string" ? directive : getDirectiveName(directive);
typeof directive === "string"
? directive
: getDirectiveDefinitionName(directive);
return specifiedDirectives.some(
(specDirective) => getDirectiveName(specDirective) === name,
(specDirective) => getDirectiveDefinitionName(specDirective) === name,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,135 @@ directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @include2(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @myRepeatableDir(name: String!) on OBJECT | INTERFACE
directive @myRepeatableDir(name: String!) repeatable on OBJECT | INTERFACE

directive @onType on OBJECT

directive @onObject(arg: String!) on OBJECT

directive @onUnion on UNION

directive @onField on FIELD

directive @onEnumValue on ENUM_VALUE

directive @onInterface on INTERFACE

directive @onScalar on SCALAR

directive @onEnum on ENUM

directive @oneOf on INPUT_OBJECT

directive @onInputObject on INPUT_OBJECT
"
`;

exports[`decodeASTSchema correctly encodes kitchen sink AST schema with directives 1`] = `
"type Foo implements Bar & Baz & Two @onType {
one: Type
two(argument: InputType!): Type
three(argument: InputType, other: String): Int
four(argument: String = "string"): String
five(argument: [String] = ["string", "string"]): String
six(argument: InputType = {key: "value"}): Type
seven(argument: Int = null): Type
eight(argument: OneOfInputType): Type
}

type AnnotatedObject @onObject(arg: "value") {
annotatedField(arg: Type = default): Type @onField
}

type UndefinedType

interface Bar implements Two @onInterface {
one: Type
four(argument: String = "string"): String
two(argument: InputType!): Type
}

interface AnnotatedInterface @onInterface {
annotatedField(arg: Type): Type @onField
}

interface UndefinedInterface

interface Baz implements Bar & Two {
one: Type
two(argument: InputType!): Type
four(argument: String = "string"): String
}

union Feed @onUnion = Story | Article | Advert

union AnnotatedUnion @onUnion = A | B

union AnnotatedUnionTwo @onUnion = A | B

union UndefinedUnion

scalar CustomScalar @onScalar

scalar AnnotatedScalar @onScalar

enum Site @onEnum {
DESKTOP
MOBILE
WEB
}

enum AnnotatedEnum @onEnum {
ANNOTATED_VALUE @onEnumValue
OTHER_VALUE
}

enum UndefinedEnum

input InputType @onInputObject {
key: String!
answer: Int = 42
other: Float = 12300
}

input OneOfInputType @oneOf {
string: String
int: Int
}

input AnnotatedInput @onInputObject {
annotatedField: Type
}

input UndefinedInput

directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @include2(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @myRepeatableDir(name: String!) repeatable on OBJECT | INTERFACE

directive @onType on OBJECT

directive @onObject(arg: String!) on OBJECT

directive @onUnion on UNION

directive @onField on FIELD

directive @onEnumValue on ENUM_VALUE

directive @onInterface on INTERFACE

directive @onScalar on SCALAR

directive @onEnum on ENUM

directive @oneOf on INPUT_OBJECT

directive @onInputObject on INPUT_OBJECT
"
`;

Expand Down Expand Up @@ -285,3 +413,189 @@ input AdvancedInput {
directive @i18n(locale: String) on QUERY
"
`;

exports[`decodeASTSchema correctly encodes swapi AST schema with directives 1`] = `
"union SearchResult = Person | Starship | Transport | Species | Vehicle | Planet | Film

enum NodeType {
Person
Starship
Transport
Species
Vehicle
Planet
Film
}

type Subscription {
emitPersons(limit: Int!, throwError: Boolean): Person
}

type Query {
node(nodeType: NodeType!, id: Int!): Node
search(search: String): [SearchResult]
person(id: Int!): Person
planet(id: Int!): Planet
film(id: Int!): Film
transport(id: Int!): Transport
starship(id: Int!): Starship
vehicle(id: Int!): Vehicle
searchPeopleByName(search: String!): [Person]
searchStarshipsByName(search: String!): [Starship]
searchTransportsByName(search: String!): [Transport]
searchSpeciesByName(search: String!): [Species]
searchVehiclesByName(search: String!): [Vehicle]
searchPlanetsByName(search: String!): [Planet]
searchFilmsByTitle(search: String! = "The Empire Strikes Back"): [Film]
allFilms: [Film]
allStarships: [Starship]
allPeople: [Person]
allPlanets: [Planet]
allSpecies: [Species]
allTransports: [Transport]
advancedDefaultInput(input: AdvancedInput! = {enumField: Transport, otherField: "Foo"}): String
multiArger(a: Int, b: String, c: AdvancedInput): String
}

interface Node {
id: Int
}

union Alive = Person | Species

type Film implements Node {
title: String!
starships: [Starship]
edited: String
vehicles: [Vehicle]
planets: [Planet]
producer: String
created: String
episode_id: Int
director: String
release_date: String
opening_crawl: String
characters: [Person]
species: [Species]
id: Int
}

type Vehicle implements Node {
id: Int
name: String
vehicle_class: String
pilots: [Person]
edited: String
consumables: String
created: String
model: String
manufacturer: String
image: String
cargo_capacity: Int
passengers: Int
max_atmosphering_speed: Int
crew: Int
length: Float
cost_in_credits: Int
}

type Person implements Node {
id: Int
edited: String
name: String
created: String
gender: String
skin_color: String
hair_color: String
height: Int
eye_color: String
mass: Int
homeworld: Planet
birth_year: String
image: String
vehicles: [Vehicle]
starships: [Starship]
films: [Film]
}

type Starship implements Node {
id: Int
films: [Film]
pilots: [Person]
MGLT: Int
starship_class: String
hyperdrive_rating: Float
edited: String
consumables: String
name: String
created: String
cargo_capacity: Int
passengers: Int
max_atmosphering_speed: Int
crew: String
length: Int
model: String
cost_in_credits: Int
manufacturer: String
image: String
}

type Planet implements Node {
id: Int
edited: String
climate: String
surface_water: String
name: String
diameter: Int
rotation_period: Int
created: String
terrain: String
gravity: String
orbital_period: Int
population: Int
residents: [Person]
films: [Film]
}

type Species implements Node {
edited: String
classification: String
name: String
designation: String
created: String
eye_colors: String
people: [Person]
skin_colors: String
language: String
hair_colors: String
homeworld: Planet
average_lifespan: Int
average_height: Int
id: Int
}

type Transport implements Node {
edited: String
consumables: String
name: String
created: String
cargo_capacity: Int
passengers: Int
max_atmosphering_speed: Int
crew: String
length: Int
model: String
cost_in_credits: Int
manufacturer: String
image: String
id: Int
}

input AdvancedInput {
enumField: NodeType!
otherField: String!
}

directive @i18n(locale: String) on QUERY
"
`;
Loading
Loading