Skip to content

Instantly share code, notes, and snippets.

@gc-codesnippets
Created November 19, 2018 22:01
Show Gist options
  • Save gc-codesnippets/b7c0d5aae4541f6650c968477259b611 to your computer and use it in GitHub Desktop.
Save gc-codesnippets/b7c0d5aae4541f6650c968477259b611 to your computer and use it in GitHub Desktop.
const query1 = `{
user(id: "$id") {
*
}
}`
type query1 = `{
user(id: "$id") {
*
}
}`
type Result1 = {
user: {
id: string
name: string
}
}
const query2 = `{
posts {
}
}`
type query2 = `{
posts {
}
}`
type Result2 = {
posts: {
id: string
title: string
}[]
}
function $graphql(q: query1, args?: any): Result1
function $graphql(q: query2, args?: any): Result2
function $graphql(q: string, args?: any): any
function $graphql(
q: query1 | query2 | string,
args?: any,
): Result1 | Result2 | any {
switch (q) {
case query1:
return <Result1>{ user: { id: 'xxx', name: 'xxx' } }
case query2:
return <Result2>{ posts: [{ id: 'xxx', title: 'xxx' }] }
default:
return ''
}
}
const myRes1 = $graphql(
`{
user(id: "$id") {
*
}
}`,
{ id: 'sfds' },
)
const myRes2 = $query(`{
posts {
}
}`)
const other = $graphql(`xxx`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment