Skip to content

Commit

Permalink
extracted playground example file text
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfv authored and tjjfvi committed Feb 2, 2022
1 parent 297e82d commit fc972a1
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 179 deletions.
83 changes: 83 additions & 0 deletions playground/example/schema.gql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

# gql schema file

schema {
query: Query
}

type Query {
getPerson(id: ID!): Person!
getAuthor(id: ID!): Author!
getBook(id: ID!): Book!
listBooks(cursor: Cursor, filter: BookFilter): [Book!]!
}

input Cursor {
start: Int!
limit: Int!
}

type Book {
id: ID!
language: Language!
author: Author!
title: String!
description: String!
categories: [Category!]!
translation(language: Language!): Book
}

input BookFilter {
title: String
author: AuthorFilter
categories: [Category!]
}

type Author implements Person {
id: ID!
name: String!
books: [Book!]!
favoriteBook: Book!
}

type User implements Person {
id: ID!
name: String
favoriteBook: Book
}

interface Person {
id: ID!
name: String
favoriteBook: Book
}

input AuthorFilter {
name: String
}

enum Category {
Horror
SciFi
Fiction
Fantasy
NonFiction
Romance
Historical
Mystery
Childrens
Dystopian
Utopian
Religous
}


enum Language {
ab ae af ak am an ar as av ay az ba be bg bh bi bm bn bo br bs ca ce ch co cr cs cu cv cy
da de dv dz ee el en eo es et eu fa ff fi fj fo fr fy ga gd gl gn gu gv ha he hi ho hr ht
hu hy hz ia id ie ig ii ik io is it iu ja jv ka kg ki kj kk kl km kn ko kr ks ku kv kw ky
la lb lg li ln lo lt lu lv mg mh mi mk ml mn mr ms mt my na nb nd ne ng nl nn no nr nv ny
oc oj om or os pa pi pl ps pt qu rm rn ro ru rw sa sc sd se sg si sk sl sm sn so sq sr ss
st su sv sw ta te tg th ti tk tl tn to tr ts tt tw ty ug uk ur uz ve vi vo wa wo xh yi yo
za zh zu
}
68 changes: 68 additions & 0 deletions playground/example/usage.tx.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// ts file

import { gqx, Author, Book, Person } from "./gqx";

const authorFrag = Author.$(
Author.id,
Author.name,
);

const bookFrag1 = Book.$(
Book.id,
Book.title,
Book.author.name,
);

const bookFrag2 = Book.$(
Book.id,
Book.author.$(authorFrag)
);

const bookFragCombined = Book.$(
bookFrag1,
bookFrag2,
);

gqx.query.getBook({ id: "abc" }, bookFragCombined).then(book => {
book; // Hover
/*
(parameter) book: {
__typename: "Book";
id: string;
title: string;
author: {
__typename: "Author";
id: string;
name: string;
};
}
*/
})

const personFrag = Person.$(
Person.name,
// Author implements the interface Person
Author.books.id,
)

gqx.query.getPerson({ id: "foo" }, personFrag).then(person => {
person; // Hover
/*
(parameter) person: {
__typename: "Author";
name: string;
books: {
__typename: "Book";
id: string;
}[];
} | {
__typename: "User";
name: string;
}
*/
// @ts-expect-error may not exist
person.books
if(person.__typename === "Author"){
person.books[0].id // string
}
})
24 changes: 24 additions & 0 deletions playground/example/userGqx.ts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export * from "./output";

import { $$GqxReturn, $$AnyProp, $$Frag, $$Input, $$Output, $$generateObject } from "./output";

// Type of e.g. gqx.query.getBook
type GqxOut_<I extends $$AnyProp> =
<T extends $$Frag<I>>(input: $$Input<I>, frag: T)
=> Promise<$$Output<I, T>>

// HKT encoding to pass to $$generateObject
interface GqxOut extends $$GqxReturn {
return: GqxOut_<this["input"]>;
}

// Imported by codegen output
export interface $$Scalars {
String: string;
Boolean: boolean;
Float: number;
Int: number;
ID: string;
}

export const gqx = $$generateObject<GqxOut>(() => null!)
183 changes: 4 additions & 179 deletions playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { parse } from "graphql";
import { Icon } from "@mdi/react"
import { mdiClose } from "@mdi/js"

// @ts-ignore
import boilerplate from "./boilerplate.txt"

export interface EditorProps {
Expand Down Expand Up @@ -61,33 +60,9 @@ export const App = () => {
</>
}

import userGqxTs from './example/userGqx.ts.txt'
writeTsFile("/gqx.ts", userGqxTs)
writeTsFile("/scalars.ts", `export type { $$Scalars } from "./gqx"`)
writeTsFile("/gqx.ts", `
export * from "./output";
import { $$GqxReturn, $$AnyProp, $$Frag, $$Input, $$Output, $$generateObject } from "./output";
// Type of e.g. gqx.query.getBook
type GqxOut_<I extends $$AnyProp> =
<T extends $$Frag<I>>(input: $$Input<I>, frag: T)
=> Promise<$$Output<I, T>>
// HKT encoding to pass to $$generateObject
interface GqxOut extends $$GqxReturn {
return: GqxOut_<this["input"]>;
}
// Imported by codegen output
export interface $$Scalars {
String: string;
Boolean: boolean;
Float: number;
Int: number;
ID: string;
}
export const gqx = $$generateObject<GqxOut>(() => null!)
`)

async function writeTsFile(path: string, content: string){
let monaco = await loader.init()
Expand Down Expand Up @@ -132,158 +107,8 @@ loader.init().then(monaco => monaco.editor.defineTheme("eglint", {
},
}))

const defaultGqlText = `
# gql schema file
schema {
query: Query
}
type Query {
getPerson(id: ID!): Person!
getAuthor(id: ID!): Author!
getBook(id: ID!): Book!
listBooks(cursor: Cursor, filter: BookFilter): [Book!]!
}
input Cursor {
start: Int!
limit: Int!
}
type Book {
id: ID!
language: Language!
author: Author!
title: String!
description: String!
categories: [Category!]!
translation(language: Language!): Book
}
input BookFilter {
title: String
author: AuthorFilter
categories: [Category!]
}
type Author implements Person {
id: ID!
name: String!
books: [Book!]!
favoriteBook: Book!
}
type User implements Person {
id: ID!
name: String
favoriteBook: Book
}
interface Person {
id: ID!
name: String
favoriteBook: Book
}
input AuthorFilter {
name: String
}
enum Category {
Horror
SciFi
Fiction
Fantasy
NonFiction
Romance
Historical
Mystery
Childrens
Dystopian
Utopian
Religous
}
import defaultGqlText from "./example/schema.gql.txt"

enum Language {
ab ae af ak am an ar as av ay az ba be bg bh bi bm bn bo br bs ca ce ch co cr cs cu cv cy
da de dv dz ee el en eo es et eu fa ff fi fj fo fr fy ga gd gl gn gu gv ha he hi ho hr ht
hu hy hz ia id ie ig ii ik io is it iu ja jv ka kg ki kj kk kl km kn ko kr ks ku kv kw ky
la lb lg li ln lo lt lu lv mg mh mi mk ml mn mr ms mt my na nb nd ne ng nl nn no nr nv ny
oc oj om or os pa pi pl ps pt qu rm rn ro ru rw sa sc sd se sg si sk sl sm sn so sq sr ss
st su sv sw ta te tg th ti tk tl tn to tr ts tt tw ty ug uk ur uz ve vi vo wa wo xh yi yo
za zh zu
}
`

const defaultTsText = `
import { gqx, Author, Book, Person } from "./gqx";
const authorFrag = Author.$(
Author.id,
Author.name,
);
const bookFrag1 = Book.$(
Book.id,
Book.title,
Book.author.name,
);
const bookFrag2 = Book.$(
Book.id,
Book.author.$(authorFrag)
);
const bookFragCombined = Book.$(
bookFrag1,
bookFrag2,
);
gqx.query.getBook({ id: "abc" }, bookFragCombined).then(book => {
book; // Hover
/*
(parameter) book: {
__typename: "Book";
id: string;
title: string;
author: {
__typename: "Author";
id: string;
name: string;
};
}
*/
})
const personFrag = Person.$(
Person.name,
// Author implements the interface Person
Author.books.id,
)
gqx.query.getPerson({ id: "foo" }, personFrag).then(person => {
person; // Hover
/*
(parameter) person: {
__typename: "Author";
name: string;
books: {
__typename: "Book";
id: string;
}[];
} | {
__typename: "User";
name: string;
}
*/
// @ts-expect-error may not exist
person.books
if(person.__typename === "Author"){
person.books[0].id // string
}
})
`
import defaultTsText from "./example/usage.ts.txt"

ReactDOM.render(<App/>, document.getElementById("root"))
4 changes: 4 additions & 0 deletions playground/txtImport.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "*.txt" {
const content: string;
export default content;
}

0 comments on commit fc972a1

Please sign in to comment.